|
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.
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);
}
| Last news from Greatis Software |
 |
|
Nostalgia .Net |
|
.Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes. More » |
| Recommended software for developers |
 |
|
Ultimate Pack |
|
Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price. More » |
 |
|
Form Designer .Net |
|
Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro! More » |
 |
|
Print Suite .Net |
|
Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available More » |
 |
|
Gradient Controls .Net |
|
Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available More » |
 |
|
Greatis iGrid |
|
iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors. More » |
All the contacts and projectsDmitry Vasiliev (just.dmitry)
Related LinksSoftware for Visual Studio .NET developers Software for Delphi and C++ Builder developers Software for Visual Basic 6 developers Delphi Tips&Tricks MegaDetailed.NET More Online Helps Win32 Programmer's Reference Win32 Multimedia Programmer's Reference OLE Programmer's Reference Microsoft Windows Pen API Programmer's Reference Microsoft Windows Sockets 2 Reference Microsoft Windows Telephony API (TAPI) Programmer's Reference Unix Manual Pages
|