Buttons Dialog boxes and controls support communication between an application and the user. A button is a control the user can click to provide input to an application. About Buttons There are several types of buttons and one or more button styles to distinguish among buttons of the same type. The user clicks a button using the mouse or keyboard. Clicking a button typically changes its visual appearance and state (from checked to cleared, for example). The system, the button, and the application cooperate in changing the button's appearance and state. A button can send messages to its parent window, and a parent window can send messages to a button. Some buttons are painted by the system, some by the application. Buttons can be used alone or in groups and can appear with or without application-defined text (a label). They belong to the BUTTON window class. Although an application can use buttons in overlapped, pop-up, and child windows, they are designed for use in dialog boxes, where the system standardizes their behavior. If an application uses buttons outside dialog boxes, it increases the risk that the application may behave in a nonstandard fashion. Applications typically either use buttons in dialog boxes or use window subclassing to create customized buttons. For general information about controls, see Controls. For more information about dialog boxes, see Dialog Boxes. Creating a Button Outside a Dialog Box The following example shows how to use the CreateWindow function to create a default push button. hwndButton = CreateWindow( "BUTTON", // predefined class "OK", // button text WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // styles
// Size and position values are given explicitly, because // the CW_USEDEFAULT constant gives zero values for buttons. 10, // starting x position 10, // starting y position 100, // button width 100, // button height hwnd, // parent window NULL, // No menu (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL); // pointer not needed Using Buttons that are not Owner-Drawn The example in this section is the window procedure for the dialog box shown in the following illustration. The check boxes and radio buttons in the Buttons dialog box are automatic. The check boxes are three-state. The Clear Colors button is a default push button. The check boxes, radio buttons, and push buttons are defined in the header file of the application, as follows. #define IDC_BOX1 101 // first check box #define IDC_BOX2 102 // second check box #define IDC_BOX3 103 // third check box #define IDC_REDBACK 104 // top radio button #define IDC_BLUEBACK 105 // bottom radio button #define IDC_CLEARBOXES 107 // top push button #define IDC_CLEARBACK 108 // bottom push button Clicking any of these controls results in a change to boxes 1, 2, and 3. You must declare and define the following function to paint boxes 1, 2, and 3. void BoxPainter( HWND hDlg, // window handle UINT uBox, // box to paint LRESULT lState); // state of box In the following window procedure, the WM_CTLCOLORDLG message notifies the application that the dialog box is about to be drawn. If the user presses the Clear Colors button (signified by the fClearColor flag), the procedure uses the SendDlgItemMessage function to clear the check boxes and radio buttons. The BN_CLICKED notification message contains the identifiers of the button that was clicked. HBRUSH hbrRed, hbrBlue, hbrWhite; BOOL fRedBack, fBlueBack, fClearColor; // background-state flags BOOL CALLBACK ButtonProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { LRESULT lState;
case IDC_REDBACK: fRedBack = TRUE; InvalidateRect(hDlg, NULL, TRUE); break;
case IDC_BLUEBACK: fBlueBack = TRUE; InvalidateRect(hDlg, NULL, TRUE); break;
case IDC_CLEARBACK: fClearColor = TRUE; InvalidateRect(hDlg, NULL, TRUE); break;
case IDC_CLEARBOXES: BoxPainter(hDlg, 4, (LRESULT) 0); break; } }
case WM_DESTROY: DeleteObject(hbrRed); DeleteObject(hbrBlue);
// Do not delete hbrWhite, because it is a stock object.
break;
} return FALSE; // did not process a message UNREFERENCED_PARAMETER(lParam); } Using Owner-Drawn Buttons The parent window of an owner-drawn button typically responds to at least three messages for the button: WM_INITDIALOG WM_COMMAND WM_DRAWITEM It is not necessary to process the WM_MEASUREITEM message for owner-drawn buttons. When you must paint an owner-drawn button, the system sends the parent window a WM_DRAWITEM message whose lParam parameter is a pointer to a DRAWITEMSTRUCT structure. Use this structure with all owner-drawn controls to provide the application with the information it requires to paint the control. The itemAction and itemState members of the DRAWITEMSTRUCT structure define how to paint an owner-drawn button. The following example shows how to process WM_INITDIALOG, WM_DRAWITEM, and WM_COMMAND messages for owner-drawn buttons. This example demonstrates how to draw one of two bitmaps for a control, depending on whether the control is selected. You would typically use the wParam parameter of the WM_DRAWITEM message to identify the control; in this example, only one control is assumed. BOOL CALLBACK OwnDrawProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdcMem; LPDRAWITEMSTRUCT lpdis;
switch (message) { case WM_INITDIALOG:
// hinst, hbm1 and hbm2 are defined globally. hbm1 = LoadBitmap((HANDLE) hinst, "OwnBit1"); hbm2 = LoadBitmap((HANDLE) hinst, "OwnBit2"); return TRUE;
case WM_DRAWITEM: lpdis = (LPDRAWITEMSTRUCT) lParam; hdcMem = CreateCompatibleDC(lpdis->hDC);
if (lpdis->itemState & ODS_SELECTED) // if selected SelectObject(hdcMem, hbm2); else SelectObject(hdcMem, hbm1);
// Destination StretchBlt( lpdis->hDC, // destination DC lpdis->rcItem.left, // x upper left lpdis->rcItem.top, // y upper left
// The next two lines specify the width and // height. lpdis->rcItem.right - lpdis->rcItem.left, lpdis->rcItem.bottom - lpdis->rcItem.top, hdcMem, // source device context 0, 0, // x and y upper left 32, // source bitmap width 32, // source bitmap height SRCCOPY); // raster operation
DeleteDC(hdcMem); return TRUE;
case WM_COMMAND: if (wParam == IDOK || wParam == IDCANCEL) { EndDialog(hDlg, TRUE); return TRUE; } if (HIWORD(wParam) == BN_CLICKED) { switch (LOWORD(wParam)) { case IDC_OWNERDRAW:
// application-defined processing
break; } } break;
case WM_DESTROY: DeleteObject(hbm1); // delete bitmaps DeleteObject(hbm2);
break;
} return FALSE; UNREFERENCED_PARAMETER(lParam); } CheckDlgButton The CheckDlgButton function changes the check state of a button control. BOOL CheckDlgButton( HWND Ошибка! Недопустимый объект гиперссылки., // handle to dialog box int Ошибка! Недопустимый объект гиперссылки., // button identifier UINT Ошибка! Недопустимый объект гиперссылки. // check state ); Parameters hDlg [in] Handle to the dialog box that contains the button. nIDButton [in] Specifies the identifier of the button to modify. uCheck [in] Specifies the check state of the button. This parameter can be one of the following values. Value Meaning
BST_CHECKED Sets the button state to checked.
BST_INDETERMINATE Sets the button state to grayed, indicating an indeterminate state. Use this value only if the button has the BS_3STATE or BS_AUTO3STATE style.
BST_UNCHECKED Sets the button state to cleared
Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks The CheckDlgButton function sends a BM_SETCHECK message to the specified button control in the specified dialog box. Requirements Windows NT/2000: Requires Windows NT 3.1 or later.Windows 95/98: Requires Windows 95 or later.Header: Declared in Winuser.h; include Windows.h.Library: Use User32.lib. See Also Buttons Overview, Button Functions, CheckRadioButton, IsDlgButtonChecked CheckRadioButton The CheckRadioButton function adds a check mark to (checks) a specified radio button in a group and removes a check mark from (clears) all other radio buttons in the group. BOOL CheckRadioButton( HWND Ошибка! Недопустимый объект гиперссылки., // handle to dialog box int Ошибка! Недопустимый объект гиперссылки., // identifier of first button in group int Ошибка! Недопустимый объект гиперссылки., // identifier of last button in group int Ошибка! Недопустимый объект гиперссылки. // identifier of button to select ); Parameters hDlg [in] Handle to the dialog box that contains the radio button. nIDFirstButton [in] Specifies the identifier of the first radio button in the group. nIDLastButton [in] Specifies the identifier of the last radio button in the group. nIDCheckButton [in] Specifies the identifier of the radio button to select. Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks The CheckRadioButton function sends a BM_SETCHECK message to each of the radio buttons in the indicated group. Requirements Windows NT/2000: Requires Windows NT 3.1 or later.Windows 95/98: Requires Windows 95 or later.Header: Declared in Winuser.h; include Windows.h.Library: Use User32.lib. See Also Buttons Overview, Button Functions, BM_SETCHECK, CheckDlgButton, IsDlgButtonChecked IsDlgButtonChecked The IsDlgButtonChecked function determines whether a button control has a check mark next to it or whether a three-state button control is grayed, checked, or neither. UINT IsDlgButtonChecked( HWND Ошибка! Недопустимый объект гиперссылки., // handle to dialog box int Ошибка! Недопустимый объект гиперссылки. // button identifier ); Parameters hDlg [in] Handle to the dialog box that contains the button control. nIDButton [in] Specifies the identifier of the button control. Return Values The return value from a button created with the BS_AUTOCHECKBOX, BS_AUTORADIOBUTTON, BS_AUTO3STATE, BS_CHECKBOX, BS_RADIOBUTTON, or BS_3STATE style can be one of the following. Value Meaning
BST_CHECKED Button is checked.
BST_INDETERMINATE Button is grayed, indicating an indeterminate state (applies only if the button has the BS_3STATE or BS_AUTO3STATE style).
BST_UNCHECKED Button is cleared
If the button has any other style, the return value is zero. Remarks The IsDlgButtonChecked function sends a BM_GETCHECK message to the specified button control. Requirements Windows NT/2000: Requires Windows NT 3.1 or later.Windows 95/98: Requires Windows 95 or later.Header: Declared in Winuser.h; include Windows.h.Library: Use User32.lib. See Also Buttons Overview, Button Functions, CheckDlgButton Button Styles If you create a button by specifying the BUTTON class with the CreateWindow or CreateWindowEx function, you can specify a combination of the following button styles. Style Meaning
BS_3STATE Creates a button that is the same as a check box, except that the box can be grayed as well as checked or cleared. Use the grayed state to show that the state of the check box is not determined.
BS_AUTO3STATE Creates a button that is the same as a three-state check box, except that the box changes its state when the user selects it. The state cycles through checked, grayed, and cleared.
BS_AUTOCHECKBOX Creates a button that is the same as a check box, except that the check state automatically toggles between checked and cleared each time the user selects the check box.
BS_AUTORADIOBUTTON Creates a button that is the same as a radio button, except that when the user selects it, The system automatically sets the button's check state to checked and automatically sets the check state for all other buttons in the same group to cleared.
BS_CHECKBOX Creates a small, empty check box with text. By default, the text is displayed to the right of the check box. To display the text to the left of the check box, combine this flag with the BS_LEFTTEXT style (or with the equivalent BS_RIGHTBUTTON style).
BS_DEFPUSHBUTTON Creates a push button that behaves like a BS_PUSHBUTTON style button, but also has a heavy black border. If the button is in a dialog box, the user can select the button by pressing the ENTER key, even when the button does not have the input focus. This style is useful for enabling the user to quickly select the most likely (default) option.
BS_GROUPBOX Creates a rectangle in which other controls can be grouped. Any text associated with this style is displayed in the rectangle's upper left corner.
BS_LEFTTEXT Places text on the left side of the radio button or check box when combined with a radio button or check box style. Same as the BS_RIGHTBUTTON style.
BS_OWNERDRAW Creates an owner-drawn button. The owner window receives a WM_MEASUREITEM message when the button is created and a WM_DRAWITEM message when a visual aspect of the button has changed. Do not combine the BS_OWNERDRAW style with any other button styles.
BS_PUSHBUTTON Creates a push button that posts a WM_COMMAND message to the owner window when the user selects the button.
BS_RADIOBUTTON Creates a small circle with text. By default, the text is displayed to the right of the circle. To display the text to the left of the circle, combine this flag with the BS_LEFTTEXT style (or with the equivalent BS_RIGHTBUTTON style). Use radio buttons for groups of related, but mutually exclusive choices.
BS_USERBUTTON Obsolete, but provided for compatibility with 16-bit versions of Windows. Win32-based applications should use BS_OWNERDRAW instead.
BS_BITMAP Specifies that the button displays a bitmap.
BS_BOTTOM Places text at the bottom of the button rectangle.
BS_CENTER Centers text horizontally in the button rectangle.
BS_ICON Specifies that the button displays an icon.
BS_FLAT Specifies that the button is two-dimensional; it does not use the default shading to create a 3-D image.
BS_LEFT Left-justifies the text in the button rectangle. However, if the button is a check box or radio button that does not have the BS_RIGHTBUTTON style, the text is left justified on the right side of the check box or radio button.
BS_MULTILINE Wraps the button text to multiple lines if the text string is too long to fit on a single line in the button rectangle.
BS_NOTIFY Enables a button to send BN_KILLFOCUS and BN_SETFOCUS notification messages to its parent window. Note that buttons send the BN_CLICKED notification message regardless of whether it has this style. To get BN_DBLCLK notification messages, the button must have the BS_RADIOBUTTON or BS_OWNERDRAW style.
BS_PUSHLIKE Makes a button (such as a check box, three-state check box, or radio button) look and act like a push button. The button looks raised when it isn't pushed or checked, and sunken when it is pushed or checked.
BS_RIGHT Right-justifies text in the button rectangle. However, if the button is a check box or radio button that does not have the BS_RIGHTBUTTON style, the text is right justified on the right side of the check box or radio button.
BS_RIGHTBUTTON Positions a radio button's circle or a check box's square on the right side of the button rectangle. Same as the BS_LEFTTEXT style.
BS_TEXT Specifies that the button displays text.
BS_TOP Places text at the top of the button rectangle.
BS_VCENTER Places text in the middle (vertically) of the button rectangle.