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 uncheck 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; 
 
    switch (message) 
    { 
        case WM_INITDIALOG: 
            hbrRed = CreateSolidBrush(RGB(255, 0, 0)); 
            hbrBlue = CreateSolidBrush(RGB(0, 0, 255)); 
            hbrWhite = GetStockObject(WHITE_BRUSH); 
            return TRUE; 
 
        case WM_CTLCOLORDLG: 
            if (fRedBack) 
            { 
                fRedBack = FALSE; 
                return (LRESULT) hbrRed; 
            } 
            else if (fBlueBack) 
            { 
                fBlueBack = FALSE; 
                return (LRESULT) hbrBlue; 
            } 
            else if (fClearColor) 
            { 
                fClearColor = FALSE; 
 
                // Uncheck all check boxes and radio buttons. 
                SendDlgItemMessage(hDlg,  // window handle 
                    IDC_BOX1,             // button identifier 
                    BM_SETCHECK,          // message 
                    0,                    // check state unchecked) 
                   0);                   // must be zero 
                SendDlgItemMessage(hDlg, IDC_BOX2, BM_SETCHECK, 0, 0); 
                SendDlgItemMessage(hDlg, IDC_BOX3, BM_SETCHECK, 0, 0); 
                SendDlgItemMessage(hDlg,IDC_REDBACK,BM_SETCHECK,0,0);
                SendDlgItemMessage(hDlg,IDC_BLUEBACK,BM_SETCHECK,0,0);
            } 
            return (LRESULT) hbrWhite; 
 
        case WM_COMMAND: 
                if (wParam == IDOK) 
                { 
                    EndDialog(hDlg, TRUE); 
                    return TRUE; 
                } 
 
                if (HIWORD(wParam) == BN_CLICKED) 
                { 
                    switch (LOWORD(wParam)) 
                    { 
                        case IDC_BOX1: 
 
                            // Retrieve the state of the check box. 
                            lState = SendDlgItemMessage( 
                                hDlg, IDC_BOX1, BM_GETSTATE, 
                                0, 0); 
 
                            BoxPainter(hDlg, 1, lState); 
                            break; 
 
                        case IDC_BOX2: 
                            lState = SendDlgItemMessage( 
                                hDlg, IDC_BOX2, BM_GETSTATE, 0, 0); 
                            BoxPainter(hDlg, 2, lState); 
                            break; 
 
                        case IDC_BOX3: 
                            lState = SendDlgItemMessage( 
                                hDlg, IDC_BOX3, BM_GETSTATE, 0, 0); 
                            BoxPainter(hDlg, 3, lState); 
                            break; 
 
                        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); 
}