


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth explanation of various windows controls such as edit boxes, buttons, list boxes, and dialogs. It covers message processing, default actions, notifications, and styles for these controls. Additionally, it discusses creating dialogs and handling messages in an application.
Typology: Study notes
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















In our previous lecture, we have viewed common dialogs and in this lecture we will learn to use them. Following are the Windows common dialog names and the functions that create these common dialogs.
Dialog Unit (DLU): A unit of horizontal or vertical distance within a dialog box. A horizontal DLU is the average width of the current dialog box font divided by 4. A vertical DLU is the average height of the current dialog-box font divided by 8. Dialogs Units have also explained in our previous lectures.
control notification message corresponding to the user's action in the high-order word of the wParam parameter.
An application should examine each notification message and respond appropriately. The following table lists each edit control notification message and the action that generates it.
Notification message User action
The user has modified text in an edit control. The system updates the display before sending this message (unlike EN_UPDATE).
EN_ERRSPACE The edit control cannot allocate enough memory to meet a specific request.
EN_HSCROLL The user has clicked the edit control's horizontal scroll bar. The system sends this message before updating the screen.
EN_KILLFOCUS The user has selected another control.
While inserting text, the user has exceeded the specified number of characters for the edit control. Insertion has been truncated. This message is also sent either when an edit control does not have the ES_AUTOHSCROLL style and the number of characters to be inserted exceeds the width of the edit control or when an edit control does not have the ES_AUTOVSCROLL style and the total number of lines to be inserted exceeds the height of the edit control.
EN_SETFOCUS The user has selected this edit control.
The user has altered the text in the edit control and the system is about to display the new text. The system sends this message after formatting the text, but before displaying it, so that the application can resize the edit control window.
The user has clicked the edit control's vertical scroll bar or has scrolled the mouse wheel over the edit control. The system sends this message before updating the screen.
In addition, the system sends a WM_CTLCOLOREDIT message to an edit control's parent window before the edit control is drawn. This message contains a handle of the edit control's display context (DC) and a handle of the child window. The parent window can use these handles to change the edit control's text and background colors.
The window procedure for the predefined edit control window class carries out default processing for all messages that the edit control procedure does not process. When the edit control procedure returns FALSE for any message, the predefined window procedure checks the messages and carries out the following default actions.
Message Default action
EM_CANUNDO Returns TRUE if the edit control operation can be undone.
EM_CHARFROMPOS Returns the character index and line index of the character nearest the specified point.
Empties the undo buffer and sets the undo flag retrieved by the EM_CANUNDO message to FALSE. The system automatically clears the undo flag whenever the edit control receives a WM_SETTEXT or EM_SETHANDLE message.
Adds or removes soft line-break characters (two carriage returns and a line feed) to the ends of wrapped lines in a multiline edit control. It is not processed by single-line edit controls.
Returns the zero-based index of the first visible character in a single-line edit control or the zero-based index of the uppermost visible line in a multiline edit control.
Returns a handle identifying the buffer containing the multiline edit control's text. It is not processed by single- line edit controls.
EM_GETLIMITTEXT Returns the current text limit, in characters.
Copies characters in a single-line edit control to a buffer and returns the number of characters copied. In a multiline edit control, retrieves a line of text from the control and returns the number of characters copied.
EM_GETLINECOUNT Returns the number of lines in the edit control.
EM_GETMARGINS Returns the widths of the left and right margins.
EM_GETMODIFY Returns a flag indicating whether the content of an edit control has been modified.
EM_GETPASSWORDCHAR Returns the character that edit controls use in conjunction with the ES_PASSWORD style.
EM_GETRECT Returns the coordinates of the formatting rectangle in an edit control.
EM_GETSEL Returns the starting and ending character positions of the current selection in the edit control.
EM_GETTHUMB Returns the position of the scroll box in the vertical scroll bar in a multiline edit control.
EM_GETWORDBREAKPROC Returns the address of the current Wordwrap function in an edit control.
EM_LINEFROMCHAR Returns the zero-based number of the line in a multiline edit control that contains a specified character index.
EM_SETMARGINS Sets the widths of the left and right margins, and redraws the edit control to reflect the new margins.
EM_SETMODIFY Sets or clears the modification flag to indicate whether the edit control has been modified.
EM_SETPASSWORDCHAR Defines the character that edit controls use in conjunction with the ES_PASSWORD style.
EM_SETREADONLY Sets or removes the read-only style (ES_READONLY) in an edit control.
Sets the formatting rectangle for the multiline edit control and redraws the window. It is not processed by single-line edit controls.
Sets the formatting rectangle for the multiline edit control but does not redraw the window. It is not processed by single-line edit controls.
EM_SETSEL Selects a range of characters in the edit control by setting the starting and ending positions to be selected.
EM_SETTABSTOPS Sets tab-stop positions in the multiline edit control. It is not processed by single-line edit controls.
EM_SETWORDBREAKPROC Replaces the default Wordwrap function with an application-defined Wordwrap function.
Removes any text that was just inserted or inserts any deleted characters and sets the selection to the inserted text. If necessary, sends the EN_UPDATE and EN_CHANGE notification messages to the parent window.
Writes a character to the single-line edit control and sends the EN_UPDATE and EN_CHANGE notification messages to the parent window. Writes a character to the multiline edit control. Handles the accelerator keys for standard functions, such as CTRL+C for copying and CTRL+V for pasting. In multiline edit controls, also processes TAB, and CTRL+TAB keystrokes to move among the controls in a dialog box and to insert tabs into multiline edit controls. Uses the MessageBeep function for illegal characters.
Clears the current selection, if any, in an edit control. If there is no current selection, deletes the character to the right of the caret. If the user presses the SHIFT key, this cuts the selection to the clipboard, or deletes the character to the left of the caret when there is no selection. If the user presses the CTRL key, this deletes the selection, or deletes to the end of the line when there is no selection.
Copies text to the clipboard unless the style is ES_PASSWORD, in which case the message returns zero.
WM_CREATE Creates the edit control and notifies the parent window with TRUE for success or –1 for failure.
WM_CUT Cuts the selection to the clipboard, or deletes the character to the left of the cursor if there is no selection.
Causes the rectangle to be redrawn in gray for single- line edit controls. Returns the enabled state for single- line and multiline edit controls.
WM_ERASEBKGND Fills the multiline edit control window with the current color of the edit control.
Returns the following values: DLGC_WANTCHARS, DLGC_HASSETSEL, and DLGC_WANTARROWS. In multiline edit controls, it also returns DLGC_WANTALLKEYS. If the user presses ALT+BACKSPACE, it also returns DLGC_WANTMESSAGE.
WM_GETFONT Returns the handle of the font being used by the control, or NULL if the control uses the system font.
WM_GETTEXT Copies the specified number of characters to a buffer and returns the number of characters copied.
Returns the length, in characters, of the text in an edit control. The length does not include the null- terminating character.
WM_HSCROLL Scrolls the text in a multiline edit control horizontally and handles scroll box movement.
WM_KEYDOWN Performs standard processing of the virtual-key codes.
Removes the keyboard focus of an edit control window, destroys the caret, hides the current selection, and notifies the parent window that the edit control has lost the focus.
Clears the current selection and selects the word under the cursor. If the SHIFT key is depressed, extends the selection to the word under the cursor.
Changes the current insertion point. If the SHIFT key is depressed, extends the selection to the position of the cursor. In multiline edit controls, also sets the timer to automatically scroll when the user holds down the mouse button outside the multiline edit control window.
Releases the mouse capture and sets the text insertion point in the single-line edit control. In a multiline edit control, also kills the timer set in the
Scrolls a multiline edit control vertically and handles scroll box movement. It is not processed by single-line edit controls.
The predefined edit control window procedure passes all other messages to the DefWindowProc function for default processing.
There are five styles of a button:
A check box consists of a square box and application-defined text (label), an icon, or a bitmap, that indicates a choice the user can make by selecting the button. Applications typically display check boxes in a group box to permit the user to choose from a set of related, but independent options. For example, an application might present a group of check boxes from which the user can select error conditions that produce warning beeps.
A check box can be one of four styles: standard, automatic, three-state, and automatic three-state, as defined by the constants BS_CHECKBOX, BS_AUTOCHECKBOX, BS_3STATE, and BS_AUTO3STATE, respectively. Each style can assume two check states: checked (a check mark inside the box) or cleared (no check mark). In addition, a three-state check box can assume an indeterminate state (a grayed box inside the check box). Repeatedly clicking a standard or automatic check box toggles it from checked to cleared and back again. Repeatedly clicking a three-state check box toggles it from checked to cleared to indeterminate and back again.
When the user clicks a check box (of any style), the check box receives the keyboard focus. The system sends the check box's parent window a WM_COMMAND message containing the BN_CLICKED notification code. The parent window doesn't acknowledge this message if it comes from an automatic check box or automatic three- state check box, because the system automatically sets the check state for those styles. But the parent window must acknowledge the message if it comes from a check box or three-state check box because the parent window is responsible for setting the check state
for those styles. Regardless of the check box style, the system automatically repaints the check box once its state is changed.
A group box is a rectangle that surrounds a set of controls, such as check boxes or radio buttons, with application-defined text (label) in its upper left corner. The sole purpose of a group box is to organize controls related by a common purpose (usually indicated by the label). The group box has only one style, defined by the constant BS_GROUPBOX. Because a group box cannot be selected, it has no check state, focus state, or push state. An application cannot send messages to a group box.
Unlike radio buttons, an owner-drawn button is painted by the application, not by the system, and has no predefined appearance or usage. Its purpose is to provide a button whose appearance and behavior are defined by the application alone. There is only one owner-drawn button style: BS_OWNERDRAW.
When the user selects an owner-drawn button, the system sends the button's parent window a WM_COMMAND message containing the BN_CLICKED notification code, just as it does for a button that is not owner-drawn. The application must respond appropriately.
A push button is a rectangle containing application-defined text (label), an icon, or a bitmap that indicates what the button does when the user selects it. A push button can be one of two styles: standard or default, as defined by the constants BS_PUSHBUTTON and BS_DEFPUSHBUTTON. A standard push button is typically used to start an operation. It receives the keyboard focus when the user clicks it. A default push button, on the other hand, is typically used to indicate the most common or default choice. It is a button that the user can select by simply pressing ENTER when a dialog box has the input focus.
When the user clicks a push button (of either style), it receives the keyboard focus. The system sends the button's parent window a WM_COMMAND message that contains the BN_CLICKED notification code. In response, the dialog box typically closes and carries out the operation indicated by the button.
The default push button cannot be a check box, a radio button, or an ownerdraw button at the same time.
left the client area of a button.
BN_CLICKED The user clicked a button.
BN_DBLCLK or BN_DOUBLECLICKED The user double-clicked a button.
BN_DISABLE A button is disabled.
BN_PUSHED or BN_HILITE The user pushed a button.
BN_KILLFOCUS The button lost the keyboard focus.
BN_PAINT The button should be painted.
BN_SETFOCUS The button gained the keyboard focus.
BN_UNPUSHED or BN_UNHILITE The button is no longer pushed.
A button sends the BN_DISABLE , BN_PUSHED , BN_KILLFOCUS , BN_PAINT , BN_SETFOCUS , and BN_UNPUSHED notification messages only if it has the BS_NOTIFY style. BN_DBLCLK notification messages are sent automatically for BS_USERBUTTON, BS_RADIOBUTTON, and BS_OWNERDRAW buttons. Other button types send BN_DBLCLK only if they have the BS_NOTIFY style. All buttons send the BN_CLICKED notification message regardless of their button styles.
For automatic buttons, the system changes the push state and paints the button. In this case, the application typically processes only the BN_CLICKED and BN_DBLCLK notification messages. For buttons that are not automatic, the application typically responds to the notification message by sending a message to change the state of the button.
When the user selects an owner-drawn button, the button sends its parent window a WM_DRAWITEM message containing the identifier of the control to be drawn and information about its dimensions and state.
The window procedure for the predefined button control window class carries out default processing for all messages that the button control procedure does not process. When the button control procedure returns FALSE for any message, the predefined window procedure checks the messages and performs the default actions listed in the following table.
Message Default action
Sends the button a WM_LBUTTONDOWN and a WM_LBUTTONUP message, and sends the parent window a BN_CLICKED notification message.
BM_GETCHECK Returns the check state of the button.
BM_GETIMAGE Returns a handle to the bitmap or icon associated with the button or NULL if the button has no bitmap or icon.
BM_GETSTATE Returns the current check state, push state, and focus state of the button.
Sets the check state for all styles of radio buttons and check boxes. If the wParam parameter is greater than zero for radio buttons, the button is given the WS_TABSTOP style.
BM_SETIMAGE Associates the specified bitmap or icon handle with the button and returns a handle to the previous bitmap or icon.
Sets the push state of the button. For owner-drawn buttons, a WM_DRAWITEM message is sent to the parent window if the state of the button has changed.
BM_SETSTYLE Sets the button style. If the low-order word of the lParam parameter is TRUE, the button is redrawn.
Checks a check box or automatic check box when the user presses the plus (+) or equal (=) keys. Clears a check box or automatic check box when the user presses the minus (–) key.
WM_ENABLE Paints the button.
Erases the background for owner-drawn buttons. The backgrounds of other buttons are erased as part of the WM_PAINT and WM_ENABLE processing.
WM_GETDLGCODE Returns values indicating the type of input processed by the default button procedure, as shown in the following table.
Button style Returns
BS_AUTOCHECKBOX DLGC_WANTCHARS | DLGC_BUTTON
BS_AUTORADIOBUTTON DLGC_RADIOBUTTON
BS_CHECKBOX DLGC_WANTCHARS | DLGC_BUTTON
BS_DEFPUSHBUTTON DLGC_DEFPUSHBUTTON
BS_GROUPBOX DLGC_STATIC
BS_PUSHBUTTON DLGC_UNDEFPUSHBUTTON
BS_RADIOBUTTON DLGC_RADIOBUTTON
Message Default action
WM_GETFONT Returns a handle to the current font.
WM_KEYDOWN Pushes the button if the user presses the SPACEBAR.
WM_KEYUP Releases the mouse capture for all cases except the TAB key.
Removes the focus rectangle from a button. For push buttons and default push buttons, the focus rectangle is invalidated. If the button has the mouse capture, the capture is released, the button is not clicked, and any push state is removed.
There are two types of list boxes: single-selection (the default) and multiple-selection. In a single-selection list box , the user can select only one item at a time. In a multiple- selection list box , the user can select more than one item at a time. To create a multiple- selection list box, specify the LBS_MULTIPLESEL or the LBS_EXTENDEDSEL style.
There are many list box styles and window styles that control the appearance and operation of a list box. These styles indicate whether list box items are sorted, arranged in multiple columns, drawn by the application, and so on. The dimensions and styles of a list box are typically defined in a dialog box template included in an application's resources.
To create a list box by using the CreateWindow or CreateWindowEx function, use the LISTBOX class, appropriate window style constants, and the following style constants to define the list box. After the control has been created, these styles cannot be modified, except as noted.
LBS_DISABLENOSCROLL: Shows a disabled vertical scroll bar for the list box when the box does not contain enough items to scroll. If you do not specify this style, the scroll bar is hidden when the list box does not contain enough items. LBS_EXTENDEDSEL: This style allows multiple items to be selected by using the SHIFT key and the mouse or special key combinations. LBS_HASSTRINGS: This style specifies that a list box contains items consisting of strings. The list box maintains the memory and addresses for the strings so that the application can use the LB_GETTEXT message to retrieve the text for a particular item. By default, all list boxes except owner-drawn list boxes have this style. You can create an owner-drawn list box either with or without this style. LBS_MULTICOLUMN: This style specifies a multi column list box that is scrolled horizontally. The LB_SETCOLUMNWIDTH message sets the width of the columns. LBS_MULTIPLESEL: Turns string selection on or off each time the user clicks or double-clicks a string in the list box. The user can select any number of strings. LBS_NODATA: This style specifies a no-data list box. Specify this style when the count of items in the list box will exceed one thousand. A no-data list box must also have the LBS_OWNERDRAWFIXED style, but must not have the LBS_SORT or LBS_HASSTRINGS style.
A no-data list box resembles an owner-drawn list box except that it contains no string or bitmap data for an item. Commands to add, insert, or delete an item always ignore any specified item data; requests to find a string within the list box
always fail. The system sends the WM_DRAWITEM message to the owner window when an item must be drawn. The itemID member of the DRAWITEMSTRUCT structure passed with the WM_DRAWITEM message specifies the line number of the item to be drawn. A no-data list box does not send a WM_DELETEITEM message.
LBS_NOINTEGRALHEIGHT: This style specifies that the size of the list box is exactly the size specified by the application when it created the list box. Normally, the system sizes a list box so that the list box does not display partial items. LBS_NOREDRAW: This style specifies that the list box's appearance is not updated when changes are made.
To change the redraw state of the control, use the WM_SETREDRAW message.
LBS_NOSEL: This style specifies that the list box contains items that can be viewed but not selected. LBS_NOTIFY: This style notifies the parent window with an input message whenever the user clicks or double-clicks a string in the list box. LBS_OWNERDRAWFIXED: This style specifies that the owner of the list box is responsible for drawing its contents and that the items in the list box are the same height. The owner window receives a WM_MEASUREITEM message when the list box is created and a WM_DRAWITEM message when a visual aspect of the list box has changed. LBS_OWNERDRAWVARIABLE: Specifies that the owner of the list box is responsible for drawing its contents and that the items in the list box are variable in height. The owner window receives a WM_MEASUREITEM message for each item in the combo box when the combo box is created and a WM_DRAWITEM message when a visual aspect of the combo box has changed. LBS_SORT Sorts strings in the list box alphabetically. LBS_STANDARD Sorts strings in the list box alphabetically. The parent window receives an input message whenever the user clicks or double-clicks a string. The list box has borders on all sides. LBS_USETABSTOPS Enables a list box to recognize and expand tab characters when drawing its strings. You can use the LB_SETTABSTOPS message to specify tab stop positions. The default tab positions are 32 dialog template units apart. Dialog template units are the device-independent units used in dialog box templates. To convert measurements from dialog template units to screen units (pixels), use the MapDialogRect function.
A list box item is often referenced by its index , an integer that represents the item's position in the list box. The index of the first item in a list box is zero; the index of the second item is one, and so on.
The following table describes how the predefined list box procedure responds to list box messages.
Message Response
Inserts a file into a directory list box filled by the DlgDirList function and retrieves the list box index of the inserted item.
LB_ADDSTRING Adds a string to a list box and returns its index.
LB_DELETESTRING Removes a string from a list box and returns the number of strings remaining in the list.
LB_DIR Adds a list of filenames to a list box and returns the index of the last filename added.
LB_FINDSTRING Returns the index of the first string in the list box that begins with a specified string..
LB_FINDSTRINGEXACT Returns the index of the string in the list box that is equal to a specified string.
LB_GETANCHORINDEX Returns the index of the item that the mouse last selected.
LB_GETCARETINDEX Returns the index of the item that has the focus rectangle.
LB_GETCOUNT Returns the number of items in the list box.
LB_GETCURSEL Returns the index of the currently selected item.
LB_GETHORIZONTALEXTENT Returns the scrollable width, in pixels, of a list box.
LB_GETITEMDATA Returns the value associated with the specified item.
LB_GETITEMHEIGHT Returns the height, in pixels, of an item in a list box.
LB_GETITEMRECT Retrieves the client coordinates of the specified list box item.
Retrieves the locale of the list box. The high-order word contains the country/region code and the low- order word contains the language identifier.
LB_GETSEL Returns the selection state of a list box item.
LB_GETSELCOUNT Returns the number of selected items in a multiple- selection list box.
Creates an array of the indexes of all selected items in a multiple-selection list box and returns the total number of selected items.
LB_GETTEXT Retrieves the string associated with a specified item and the length of the string.
LB_GETTEXTLEN Returns the length, in characters, of the string associated with a specified item.
LB_GETTOPINDEX Returns the index of the first visible item in a list box.
LB_INITSTORAGE Allocates memory for the specified number of items and their associated strings.
LB_INSERTSTRING Inserts a string at a specified index in a list box.
LB_ITEMFROMPOINT Retrieves the zero-based index of the item nearest the specified point in a list box.
LB_RESETCONTENT Removes all items from a list box.
LB_SELECTSTRING Selects the first string it finds that matches a specified prefix.
LB_SELITEMRANGE Selects a specified range of items in a list box.
Selects a specified range of items if the index of the first item in the range is less than the index of the last item in the range. Cancels the selection in the range if the index of the first item is greater than the last.
LB_SETANCHORINDEX Sets the item that the mouse last selected to a specified item.
LB_SETCARETINDEX Sets the focus rectangle to a specified list box item.
LB_SETCOLUMNWIDTH Sets the width, in pixels, of all columns in a list box.
LB_SETCOUNT Sets the number of items in a list box.
LB_SETCURSEL Selects a specified list box item.
LB_SETHORIZONTALEXTENT Sets the scrollable width, in pixels, of a list box.
LB_SETITEMDATA Associates a value with a list box item.
LB_SETITEMHEIGHT Sets the height, in pixels, of an item or items in a list box.
LB_SETLOCALE Sets the locale of a list box and returns the previous locale identifier.
LB_SETSEL Selects an item in a multiple-selection list box.
LB_SETTABSTOPS Sets the tab stops to those specified in a specified array.
LB_SETTOPINDEX Scrolls the list box so the specified item is at the top of the visible range.
The predefined list box procedure passes all other messages to DefWindowProc for default processing.