Using Buttons that Are Not Owner-Drawn

The example in this section is the window procedure for a dialog box, as shown in the following illustration.

pics/WIN3200000037.gif

The check boxes and radio buttons in the Buttons dialog box are automatic. The check boxes are three-state. The Clear colors push 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 IDB_BOX1 101 // first check box

#define IDB_BOX2 102 // second check box

#define IDB_BOX3 103 // third check box

#define IDB_REDBACK 104 // top radio button

#define IDB_BLUEBACK 105 // bottom radio button

#define IDB_CLEARBOXES 107 // top push button

#define IDB_CLEARBACK 108 // bottom push button

HBRUSH hbrRed, hbrBlue, hbrWhite;

BOOL fRedBack, fBlueBack, fClearColor; // background-state flags

Note that it is not necessary to define IDOK, the identifier for the OK push button.

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 buttons.

LRESULT APIENTRY ButtonProc(hDlg, message, wParam, lParam)

HWND hDlg; // window handle of dialog box

UINT message; // type of message

UINT wParam; // message-specific information

LONG 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

IDB_BOX1, // button identifier

BM_SETCHECK, // message

0, // check state unchecked)

0); // must be zero

SendDlgItemMessage(hDlg, IDB_BOX2,

BM_SETCHECK, 0, 0);

SendDlgItemMessage(hDlg, IDB_BOX3,

BM_SETCHECK, 0, 0);

SendDlgItemMessage(hDlg, IDB_REDBACK,

BM_SETCHECK, 0, 0);

SendDlgItemMessage(hDlg, IDB_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 IDB_BOX1:

// Retrieve the state of the

// check box.

lState = SendDlgItemMessage(

hDlg, IDB_BOX1, BM_GETSTATE,

0, 0);

// The box-painting function is

// application defined.

BoxPainter(

hDlg, // window handle

1, // box to paint

lState); // state of box

break;

case IDB_BOX2:

lState = SendDlgItemMessage(

hDlg, IDB_BOX2, BM_GETSTATE,

0, 0);

BoxPainter(hDlg, 2, lState);

break;

case IDB_BOX3:

lState = SendDlgItemMessage(

hDlg, IDB_BOX3, BM_GETSTATE,

0, 0);

BoxPainter(hDlg, 3, lState);

break;

case IDB_REDBACK:

fRedBack = TRUE;

InvalidateRect(hDlg, NULL,

TRUE);

break;

case IDB_BLUEBACK:

fBlueBack = TRUE;

InvalidateRect(hDlg, NULL,

TRUE);

break;

case IDB_CLEARBACK:

fClearColor = TRUE;

InvalidateRect(hDlg, NULL,

TRUE);

break;

case IDB_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);

}

Software for developers
Delphi Components
.Net Components
Software for Android Developers
More information resources
MegaDetailed.Net
Unix Manual Pages
Delphi Examples
Databases for Amazon shops developers
Amazon Categories Database
Browse Nodes Database