11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7. Common Dialog Boxes<br />

}<br />

}<br />

return lpFontfn(hDlg, iMsg, wParam, lParam);<br />

The code listed above shows how to trap WM_INITDIALOG message and attach the window handle to a<br />

variable declared outside the hook function. Also, the default hook procedure is called to let other messages<br />

be processed normally. Here, lpFontfn is a global pointer that stores the address of the hook procedure.<br />

Sample Implementation<br />

Sample 7.9\CDB demonstrates how to implement modeless common dialog boxes. It is based on<br />

sampele 7.8\CDB, with two new commands added to the application: Color Dialog Box | Modeless and<br />

Font Dialog Box | Modeless, both of which can be used to invoke modeless common dialog boxs. For the<br />

former command, the user can select a color and switch back to the main window to see the effect without<br />

dismissing the dialog box. The IDs of the two commands are ID_COLORDIALOGBOX_MODELESS and<br />

ID_FONTDIALOGBOX_MODELESS, and their WM_COMMAND message handlers are CCDBDoc::<br />

OnColordialogboxModeless() and CCDBDoc::OnFontdialogboxModeless() respectively.<br />

A dummy dialog box is added to the application, whose resource ID is IDD_DIALOG_DUMMY. It will be<br />

used as the parent window of the common dialog boxes. Since this window is always hidden after being<br />

invoked, it does not matter what controls are included in the dialog template. The class that will be used to<br />

implement this dialog box is MCDummyDlg.<br />

Two new variables are declared in class CCDBDoc for implementing modeless color dialog box:<br />

class CCDBDoc : public CDocument<br />

{<br />

protected:<br />

CCDBDoc();<br />

DECLARE_DYNCREATE(CCDBDoc)<br />

……<br />

}<br />

MCDummyDlg *m_pColorDmDlg;<br />

CColorDialog *m_pColorDlg;<br />

Here, m_pColorDmDlg will be used to create dummy window, and m_pColorDlg will be used to create<br />

color dialog box.<br />

At the beginning of file “CDBDoc. Cpp”, a global hook procedure and a pointer are declared:<br />

UINT CALLBACK ColorHook(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);<br />

UINT (CALLBACK *lpColorfn)(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);<br />

Function ColorHook is the custom hook procedure, and pointer lpColorfn will be used to store the<br />

address of the default hook procedure.<br />

In function CCDBDoc::OnColordialogboxModeless(), first we need to initialize m_pColorDlg and<br />

m_pColorDmDlg, then create the dummy window:<br />

……<br />

void CCDBDoc::OnColordialogboxModeless()<br />

{<br />

m_pColorDlg=new MCMLColorDlg();<br />

m_pColorDmDlg=new MCDummyDlg();<br />

m_pColorDmDlg->Create(IDD_DIALOG_DUMMY);<br />

m_pColorDmDlg->ShowWindow(SW_HIDE);<br />

Function CDialog::Create() is called to create a modeless dialog box, and function CWnd::<br />

ShowWindow(…) (using parameter SW_HIDE) is called to hide this window.<br />

Before the color dialog box is created, we must make some changes to structure CHOOSECOLOR:<br />

……<br />

m_pColorDlg->m_cc.lpCustColors=rgbColors;<br />

195

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!