22.1 DIALOGS
(CONTINUE
FROM PREVIOUS
LECTURE)
2
22.2 COMMAND
DIALOG
PROCEDURE
2
22.3 CHOOSE
COLOR
DIALOG
2
22.4 OUR OWN
DEFINED FUNCTION SHOWCHOOSECOLORDIALOG
3
22.5 COMMAND
DIALOG
PROCEDURE
(DRAWING)
4
22.6 THE
ABOUT
BOX
(MAIN
WINDOW
PROCEDURE)
4
22.7 LIST
BOX
MESSAGES
ERROR!
BOOKMARK NOT DEFINED.
22.8 ABOUT
BOX
DIALOG
PROCEDURE
5
SUMMARY
6
EXERCISES
6
Using Common Dialogs and Windows Controls
2
22.1 Dialogs (Continue
from the Previous Lecture)
In this lecture, we will discuss more about the dialog boxes and
their commands
implementations.
22.2 Command Dialog Procedure
case WM_CTLCOLORSTATIC:
switch(GetDlgCtrlID((HWND)lParam))
{
case IDC_STATIC_TEXT_COLOR:
if(hBrush) // if some brush was created before
DeleteObject(hBrush);
hBrush = CreateSolidBrush(textColour); // create a brush
return (BOOL)hBrush;
break;
case IDC_STATIC_BRUSH_COLOR:
if(hBrush) // if some brush was created before
DeleteObject(hBrush);
hBrush = CreateSolidBrush(brushColour); // create a brush
return (BOOL)hBrush;
break;
default:
return FALSE; // perform default message handling
22.3 Choose Color Dialog
ChooseColor(&chooseclr);
typedef struct {
DWORD lStructSize;
HWND hwndOwner;
HWND hInstance;
COLORREF rgbResult;
COLORREF * lpCustColors;
DWORD Flags; CC_RGBINIT | CC_FULLOPEN | CC_ANYCOLOR
LPARAM lCustData;
LPCCHOOKPROC lpfnHook;
LPCTSTR lpTemplateName;
} CHOOSECOLOR, *LPCHOOSECOLOR; return Val???
Using Common Dialogs and Windows Controls
3
case WM_CTLCOLORSTATIC:
switch(GetDlgCtrlID((HWND)lParam))
{
case IDC_BUTTON_BRUSH_COLOR:
if(ShowChooseColorDialog(hDlg, brushColour, &brushColour))
{
GetClientRect(GetDlgItem(hDlg, IDC_STATIC_BRUSH_COLOR),
&rect);
InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_BRUSH_COLOR),
&rect, TRUE);
}
Break;
22.4 Our own defined function ShowChooseColorDialog
BOOL ShowChooseColorDialog(HWND Owner, COLORREF initClr,
LPCOLORREF
chosenClr)
{
CHOOSECOLOR cc;
static COLORREF customColors[16];
memset(&cc, 0, sizeof(cc));
cc.lStructSize = sizeof(CHOOSECOLOR);
cc.hwndOwner = hwndOwner;
cc.rgbResult = initialColor;
cc.lpCustColors = customColors;
cc.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ANYCOLOR;
if(ChooseColor(&cc)) // OK pressed
{
*chosenColor = cc.rgbResult;
return TRUE;
}
return FALSE;
}
Continue from Command Dialog Procedure:
case IDC_BUTTON_BRUSH_COLOR:
if(ShowChooseColorDialog(hDlg, brushColour, &brushColour))
Using Common Dialogs and Windows Controls
4
{
// REPAINT CONTROL: send WM_CTLCOLORSTATIC druing repainting
GetClientRect(GetDlgItem(hDlg, IDC_STATIC_BRUSH_COLOR), &rect);
InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_BRUSH_COLOR), &rect,
TRUE);
}
Break;
22.5 Command Dialog Procedure (Drawing)
case IDC_BUTTON_DRAW:
hDC = GetDC(GetParent(hDlg));
if(IsDlgButtonChecked(hDlg, IDC_RADIO_RECTANGLE) ==
BST_CHECKED)
{
hOwnerBrush = CreateHatchBrush(HS_BDIAGONAL, brushColour);
hOldBrush = SelectObject(hDC, hOwnerBrush);
Rectangle(hDC, 10, 10, 200, 200);
SelectObject(hDC, hOldBrush); // restore old selection
DeleteObject(hOwnerBrush);
}
22.6 The About Box (Main
Window Procedure)
Now create a Modal Dialog box on the about event.
case ID_HELP_ABOUT:
DialogBox(hAppInstance, MAKEINTRESOURCE(IDD_DIALOG_ABOUT),
hWnd, aboutDialogProc);
Using Common Dialogs and Windows Controls
5
22.7 About Box Dialog Procedure
LPTSTR strings[5][2] = {{"Application", "Lecture 22"},
{"Author", "M. Shahid Sarfraz"},
{"Institution", "Virtual University"},
{"Year", "2003"},
{"Copyright", "2003 Virtual University"}};
case WM_INITDIALOG:
for(i=0; i<5; ++i)
{
index = SendDlgItemMessage(hDlg,IDC_LIST_ABOUT, LB_ADDSTRING, 0,
(LPARAM)strings[i][0]);
SendDlgItemMessage(hDlg, IDC_LIST_ABOUT, LB_SE
TITEMDATA,index,(LPARAM)strings[i][1])
}
// set current selection to 0
SendDlgItemMessage(hDlg, IDC_LIST_ABOUT, LB_SETCURSEL, 0, 0);
//Check notification messaqges in about dialog box
LPTSTR str;
case WM_COMMAND:
wNotificationCode = HIWORD(wParam);
wID = LOWORD(wParam);
Using Common Dialogs and Windows Controls
6
switch(wID)
{
case IDC_LIST_ABOUT:
if(wNotificationCode == LBN_SELCHANGE)
{
index = SendDlgItemMessage(hDlg, wID, LB_GETCURSEL, 0, 0);
str = (LPTSTR)SendDlgItemMessage(hDlg, IDC_LIST_ABOUT,
LB_GETITEMDATA, index, 0);
SetDlgItemText(hDlg, IDC_STATIC_ABOUT, str);
}
}
Summary
We have been studying dialogs from previous two lectures. In
this lecture, we
have implemented some of the command implementation of dialog
boxes. Common
dialogs are very much useful in windows. Using common dialogs,
you can show user to
choose colors, files and printer, etc. Dialog resources are easy
to use and easier to handle.
Controls can be displayed on the dialogs. Dialogs by default set
the font and dimensions
of the controls. Dialogs are used in many areas like
configuration of hardware devices,
internet connections, properties and database configurations.
Another important dialogs
are called property sheets. This property sheet enables you to
select any category from
the tabs.
Exercises
1. Create a Medical Store data base form. This form should be a
Modal/Modeless
dialog box containing all the controls needed for the medical
store keeper to enter
data. This form should handle all the controls notification
messages. Save the
data, entered in a dialog box controls, in a file.
2. Create Owner draw combo box, which has green selection
rectangle and white text instead of blue (default) selection rectangle. |