

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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.
Dialog boxes and controls support communication between applications and their users. An edit control is a rectangular control window typically used in a dialog box to permit the user to enter and edit text by typing on the keyboard.
An edit control is selected and receives the input focus when a user clicks the mouse inside it or presses the TAB key. After it is selected, the edit control displays its text (if any) and a flashing caret that indicates the insertion point. The user can then enter text, move the insertion point, or select text to be edited by using the keyboard or the mouse. An edit control can send notification messages to its parent window in the form of WM_COMMAND messages. A parent window can send messages to an edit control in a dialog box by calling the SendDlgItemMessage function.
The system provides both single-line edit controls (sometimes called SLEs) and multiline edit controls (sometimes called MLEs). Edit controls belong to the EDIT window class.
A combo box is a control that combines much of the functionality of an edit control and a list box. In a combo box, the edit control displays the current selection and the list box presents options a user can select. Many developers use the dialog boxes provided in the common dialog box library (Comdlg32.dll) to perform tasks that otherwise might require customized edit controls.
The user makes editing requests by using the keyboard and mouse. The system sends each request to the edit control's parent window in the form of a WM_COMMAND message. The message includes the edit control identifier in the low-order word of the wParam parameter, the handle of the edit control in the lParam parameter, and an edit
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.
This message is the reverse of the EM_LINEINDEX message. It is not processed by single-line edit controls.
Returns the character of a line in a multiline edit control. This message is the reverse of the EM_LINEFROMCHAR message. It is not processed by single-line edit controls.
Returns the length, in characters, of a single-line edit control. In a multiline edit control, returns the length, in characters, of a specified line.
Scrolls the text vertically in a single-line edit control or horizontally in a multiline edit control (when the control has the ES_LEFT style). The lParam parameter specifies the number of lines to scroll vertically, starting from the current line. The wParam parameter specifies the number of characters to scroll horizontally, starting from the current character.
EM_POSFROMCHAR Returns the client coordinates of the specified character.
Replaces the current selection with the text in an application-supplied buffer, sends the parent window EN_UPDATE and EN_CHANGE messages, and updates the undo buffer.
Scrolls the text vertically in a multiline edit control. This message is equivalent to sending a WM_VSCROLL message to the edit control. It is not processed by single- line edit controls.
EM_SCROLLCARET Scrolls the caret into view in an edit control.
EM_SETFONT Unsupported.
Sets a handle to the memory used as a text buffer, empties the undo buffer, resets the scroll positions to zero, and redraws the window.
Sets the maximum number of characters the user may enter in the edit control.
Windows NT/2000/XP: For single-line edit controls, this value is either 0x7FFFFFFE or the value of the wParam parameter, whichever is smaller. For multiline edit controls, this value is either –1 or the value of the wParam parameter, whichever is smaller.
Windows 95/98/Me: For single-line edit controls, this value is either 0x7FFE or the value of the wParam parameter, whichever is smaller. For multiline edit controls, this value is either 0xFFFF or the value of the wParam parameter, whichever is smaller.
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
WM_LBUTTONDOWN message.
Changes the current selection in the single-line edit control, if the mouse button is down. In a multiline edit controls, also sets the timer to automatically scroll if the user holds down the mouse button outside the multiline edit control window.
Pointer to the CREATESTRUCT structure for the window. This message is sent to the WM_CREATE message when a window is first created.
Frees all memory associated with the edit control window, including the text buffer, undo buffer, tab-stop buffer, and highlight brush.
Erases the background, fills the window with the current color of the edit control window, draws the border (if any), sets the font and draws any text, and shows the text-insertion caret.
WM_PASTE Pastes text from the clipboard into the edit control window at the caret position.
Sets the keyboard focus of an edit control window (shows the current selection, if it was hidden, and creates the caret).
WM_SETFONT Sets the font and optionally redraws the edit control.
Copies text to the single-line edit control, notifies the parent window when there is insufficient memory, empties the undo buffer, and sends the EN_UPDATE and EN_CHANGE notification messages to the parent window. In multiline edit controls, also rewraps the lines (if necessary) and sets the scroll positions.
Changes the size of the edit control window and ensures that the minimum size accommodates the height and width of a character.
WM_SYSCHAR Returns TRUE if the user presses ALT+BACKSPACE; otherwise takes no action.
WM_SYSKEYDOWN Undoes the last action if the user presses ALT+BACKSPACE; otherwise takes no action.
Scrolls the text in the edit control window if the user holds down the mouse button outside the multiline edit control window.
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.
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.
A radio button consists of a round button and application-defined text (a label), an icon, or a bitmap that indicates a choice the user can make by selecting the button. An application typically uses radio buttons in a group box to permit the user to choose from a set of related, but mutually exclusive options. For example, the application might present a group of radio buttons from which the user can select a format preference for text selected in the client area. The user could select a left-aligned, right-aligned, or centered format by selecting the corresponding radio button. Typically, the user can select only one option at a time from a set of radio buttons.
A radio button can be one of two styles: standard or automatic, as defined by the constants BS_RADIOBUTTON and BS_AUTORADIOBUTTON. Each style can assume two check states: checked (a dot in the button) or cleared (no dot in the button). Repeatedly selecting a radio button (standard or automatic) toggles it from checked to cleared and back again.
When the user selects either state, the radio button receives the keyboard focus. The system sends the button'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 radio button because the system automatically sets the check state for that style. But the parent window should acknowledge the message if it comes from a radio button because the parent window is responsible for setting the check state for that style. Regardless of the radio button style, the system automatically repaints the button as its state changes.
When the user selects an automatic radio button, the system automatically sets the check state of all other radio buttons within the same group to clear. The same behavior is available for standard radio buttons by using the WS_GROUP style, as discussed in Dialog Boxes.
When the user clicks a button, its state changes, and the button sends notification messages to its parent window. For example, a push button control sends the BN_CLICKED notification message whenever the user chooses the button. In all cases (except for BCN_HOTITEMCHANGE), the low-order word of the wParam parameter contains the control identifier, the high-order word of wParam contains the notification code, and the lParam parameter contains the control window handle.
Both the message and the parent window's response depend on the type, style, and current state of the button. Following are the button notification messages an application should monitor and process.
Message Description
BCN_HOTITEMCHANGE Microsoft® Windows® XP: The mouse entered or
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.
Sends a BN_DBLCLK notification message to the parent window for radio buttons and owner-drawn buttons. For other buttons, a double-click is processed as a WM_LBUTTONDOWN message.
WM_LBUTTONDOWN Highlights the button if the position of the mouse cursor is within the button's client rectangle.
WM_LBUTTONUP Releases the mouse capture if the button had the mouse capture.
Performs the same action as WM_LBUTTONDOWN , if the button has the mouse capture. Otherwise, no action is performed.
WM_NCCREATE Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
WM_NCHITTEST Returns HTTRANSPARENT, if the button control is a group box.
WM_PAINT Draws the button according to its style and current state.
Draws a focus rectangle on the button getting the focus. For radio buttons and automatic radio buttons, the parent window is sent a BN_CLICKED notification message.
WM_SETFONT Sets a new font and optionally updates the window.
Sets the text of the button. In the case of a group box, the message paints over the preexisting text before repainting the group box with the new text.
WM_SYSKEYUP Releases the mouse capture for all cases except the TAB key.
The predefined window procedure passes all other messages to the DefWindowProc function for default processing.
List box items can be represented by text strings, bitmaps, or both. If the list box is not large enough to display all the list box items at once, the list box provides a scroll bar. The user scrolls through the list box items, and applies or removes selection status as necessary. Selection style of a list box item or its visual appearance can be changed in Operating system metrics. When the user selects or deselects an item, the system sends a notification message to the parent window of the list box.
A dialog box procedure is responsible for initializing and monitoring its child windows, including any list boxes. The dialog box procedure communicates with the list box by sending messages to it and by processing the notification messages sent by 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.
This style specifies that the owner of the list box receives WM_VKEYTOITEM messages whenever the user presses a key and the list box has the input focus. This enables an application to perform special processing on the keyboard input.
Note: the description of the controls including list box has been taken from Microsoft Help Desk.
When an event occurs in a list box, the list box sends a notification message to the dialog box procedure of the owner window. List box notification messages are sent when a user selects, double-clicks, or cancels a list box item; when the list box receives or loses the keyboard focus; and when the system cannot allocate enough memory for a list box request. A notification message is sent as a WM_COMMAND message in which the low- order word of the wParam parameter contains the list box identifier, the high-order word of wParam contains the notification message, and the lParam parameter contains the control window handle.
A dialog box procedure is not required to process these messages; the default window procedure processes them.
An application should monitor and process the following list box notification messages.
Notification message Description
LBN_DBLCLK The user double-clicks an item in the list box.
LBN_ERRSPACE The list box cannot allocate enough memory to fulfill a request.
LBN_KILLFOCUS The list box loses the keyboard focus.
LBN_SELCANCEL The user cancels the selection of an item in the list box.
LBN_SELCHANGE The selection in a list box is about to change.
LBN_SETFOCUS The list box receives the keyboard focus.
A dialog box procedure can send messages to a list box to add, delete, examine, and change list box items. For example, a dialog box procedure could send an LB_ADDSTRING message to a list box to add an item, and an LB_GETSEL message to determine whether the item is selected. Other messages set and retrieve information about the size, appearance, and behavior of the list box. For example, the LB_SETHORIZONTALEXTENT message sets the scrollable width of a list box. A dialog box procedure can send any message to a list box by using the SendMessage or SendDlgItemMessage function.
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.
The following components will be used in our application
Dialog box is designed in resource edit provided by Visual Studio
Choose color is built resource in windows.