Button Controls

A button control is a small window used for simple yes/no, on/off type of input. The following are some of the most commonly used types of button controls:

Push button

Default push button

Check box

Radio button

Owner-draw button

Group box

Push Buttons

A push button is a button that the user can select to carry out a specific action. The button contains text that indicates what that button does. When the user clicks a push button, the application normally carries out the associated action immediately. For example, if the user clicks the Cancel button in a dialog box, the application immediately removes the dialog box and cancels the user's changes to the dialog (if any).

To create a button control, specify Button as the control's window class, and specify the button style(s) in the dwStyle parameter. For example, the following call to the CreateWindow function creates a push-button control with the label Cancel:

HWND hCancelButton;

.

.

.

hCancelButton = CreateWindow(

“Button”, “Cancel”,

BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,

20,40, 80,20, hWnd, IDCANCEL, hInstance, NULL);

Because this example specifies the WS_VISIBLE style, Windows displays the control after creating it. The control ID is IDCANCEL. This constant is defined in the WINDOWS.H file and is intended to be used with Cancel push buttons.

Default Push Buttons

A default push button typically lets the user signal the completion of some activity, such as filling in an edit control with a filename. A default push-button control, as with other button controls, responds to both mouse and keyboard input. If the user moves the cursor into the control and clicks it, the button sends a BN_CLICKED notification message to the parent window. The button does not have to have the input focus in order to respond to mouse input. It does, however, require the focus in order to respond to keyboard input. To let the user use the keyboard, use the SetFocus function to give the input focus to the button. The user can then press the SPACEBAR to direct the button to send a BN_CLICKED notification message to the parent window.

Creating a default push-button control is similar to creating a push-button control. Specify Button as the control's window class, and specify the button style(s) in the dwStyle parameter. For example, the following call to the CreateWindow function creates a default push-button control with the label OK:

HWND hDefButton;

.

.

.

hDefButton = CreateWindow(

“Button”, “OK”,

BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE,

20,40, 80,20, hWnd, IDOK, hInstance, NULL);

This example specifies the WS_VISIBLE style, so Windows displays the control after creating it. The control ID is IDOK. This constant is defined in the WINDOWS.H file and is intended to be used with default push buttons, such as this OK button.

Check Boxes

A check box typically lets the user select an option to use in the current task. By convention, within a group of check boxes, the user can select more than one option. (To present options that are mutually exclusive, use radio buttons instead of check boxes.)

For example, you might present a group of check boxes that lets the user select font properties for the next output operation. The user could choose both bold and italic by checking both the Bold and the Italic check boxes.

To create a check-box control, use the BS_CHECKBOX style, as in the following example:

#define IDC_ITALIC 201

HWND hCheckBox;

.

.

.

hCheckBox = CreateWindow("Button", “Italic”,

BS_CHECKBOX | WS_CHILD | WS_VISIBLE,

20,40, 80,20, hWnd, IDC_ITALIC, hInstance, NULL);

In this example, the check-box label is Italic and the control ID is IDC_ITALIC.

A check box responds to mouse and keyboard input much as a push-button control would. That is, it sends a notification message to the parent window when the user clicks the control or presses the SPACEBAR. However, a check box can display a check (an “X”) in its box to show that it is currently on (it has been selected).

To tell a control to display a check, send the control the BM_SETCHECK message. You can also test to see if the check box has a check by sending the control the BM_GETCHECK message. For example, to place a check in the check box, use the following function:

SendMessage(hCheckBox, BM_SETCHECK, 1, 0L);

This means you can place or remove a check in the check box whenever you want; for example, when the parent window function receives a BN_CLICKED notification message. Windows also provides a BS_AUTOCHECKBOX style that automatically toggles its state (places or removes a check) each time the user clicks it.

Radio Buttons

Radio-button controls work in much the same way as check boxes. However, radio buttons are usually used in groups and represent mutually exclusive options. For example, you might use a group of radio buttons to let the user specify text justification (right-justified, left-justified, or centered). The radio buttons would let the user select only one type of justification at a time.

Create a radio-button control as you would any button control. Specify Button as the control's window class, and specify the button style(s) in the dwStyle parameter. For example, the following call to the CreateWindow function creates a radio-button control with the label Right:

HWND HRightJustifyButton

#define IDC_RIGHTJUST

.

.

.

hRightJustifyButton = CreateWindow("Button", “Right”,

BS_RADIOBUTTON | WS_CHILD | WS_VISIBLE,

20,40, 80,20, hWnd, IDC_RIGHTJUST, hInstance, NULL);

As with a check box, you must send a BM_SETCHECK message to the radio button to display a “check” (actually, a solid circle) in the button when the user selects that button. Also, since radio buttons represent mutually exclusive choices, you should also send the BM_SETCHECK message to the previously checked radio button (if any) to clear its check. You can determine which radio button in a group is checked by sending the BM_GETCHECK message to each button.

In a dialog box, you can create radio buttons with the BS_AUTORADIOBUTTON style. When all the radio buttons in a group box have the BS_AUTORADIOBUTTON style, Windows automatically removes the check from the previously checked button when the user selects a different radio button.

You can also use the CheckRadioButton function to check a radio button and remove the check from other buttons in a dialog box. When you call CheckRadioButton, you specify the IDs of the first and last buttons in a range of buttons and the ID of the radio button (within that range) that is to be checked. Windows removes the check from all the buttons in the specified range and then checks the appropriate radio button. For example, in a group of buttons representing types of text justification, you would call CheckRadioButton to check the Right button, as in the following example:

CheckRadioButton(hDlg, ID_RIGHTLEFTJUST, ID_LEFTJUST,

ID_RIGHTJUST)

In this example, CheckRadioButton would check the radio button identified by ID_RIGHTJUST and remove the check from all the other buttons whose IDs fall within the range specified by ID_RIGHTLEFTJUST and ID_LEFTJUST.

Owner-Draw Buttons

An owner-draw button is similar to other button styles, except that the application is responsible for maintaining the button's appearance, including whether the button has focus, is disabled, or is selected. Windows simply notifies your application when the button has been clicked.

To create an owner-draw button, use the BS_OWNERDRAW style, as shown in the following example:

hMyOwnButton = CreateWindow("Button", NULL,

BS_OWNERDRAW | WS_CHILD | WS_VISIBLE,

20, 40, 30, 12, hWnd, ID_MYBUTTON,

hInstance, NULL);

Whenever the button needs to be drawn, Windows sends the WM_DRAWITEM message to the window that owns the button. The lParam parameter of the WM_DRAWITEM message contains a pointer to a DRAWITEMSTRUCT data structure. This structure contains, among other information, the control ID, a value specifying the type of drawing action required, a value indicating the state of the button, a bounding rectangle for the button, and a handle to the device context of the button.

In response to the WM_DRAWITEM message, your application must perform the following actions before returning from processing the message:

1.Determine the type of drawing that is needed. To do so, examine the itemAction field of the DRAWITEMSTRUCT data structure.

2.Draw the button appropriately, using the rectangle and device context obtained from the DRAWITEMSTRUCT data structure.

3.Restore all GDI objects selected for the button's device context.

For example, if the button has lost input focus, Windows sets the itemAction field of the DRAWITEMSTRUCT data structure to ODA_FOCUS, but not the ODS_FOCUS bit in the itemState field. This is your application's cue to redraw the button so that it no longer appears to have focus.

Group Boxes

Group boxes are rectangles that enclose two or more related buttons or other controls. You can send the WM_SETTEXT message to the group box to place a caption in the upper-left corner of the box. Group boxes do not respond to user input; that is, they do not generate notification messages.