Implementing Common Dialogs and Windows Controls: A Windows Programming Tutorial, Study notes of Windows Programming

A tutorial on implementing common dialogs and windows controls in windows programming. It covers the use of command dialog procedures, choose color dialog, and creating custom functions. The document also includes examples of repainting controls and creating an about box.

Typology: Study notes

2011/2012

Uploaded on 08/07/2012

anishay
anishay 🇮🇳

4.2

(25)

118 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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???
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Implementing Common Dialogs and Windows Controls: A Windows Programming Tutorial and more Study notes Windows Programming in PDF only on Docsity!

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???

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))

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

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.