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 6. Dialog Box<br />

Both versions of this function have two parameters, the first of which is the template ID (either an<br />

integer ID or a string ID), the second is a CWnd type pointer which specifies the parent window of the dialog<br />

box.<br />

Function CWnd::ShowWindow(…) has the following format:<br />

BOOL CWnd::ShowWindow(int nCmdShow);<br />

It has only one parameter, which can be set to SW_HIDE, SW_MINIMIZE, SW_SHOW... and so on to display<br />

the window in different styles.<br />

For example, if class CMyDialog is derived from CDialog, and the dialog template ID is IDD_DIALOG,<br />

we can declare a variable m_dlg in any class (for example, CDocument) then do the following in a member<br />

function to implement a modeless dialog box:<br />

m_dlg.Create(IDD_DIALOG);<br />

m_dlg.ShowWindow(SW_SHOW);<br />

Sample<br />

Sample 6.1\DB demonstrates how to implement modeless dialog box. It is a standard SDI application<br />

created by Application Wizard. To make the modeless creation procedure simpler, a member function<br />

DoModeless() is implemented in the derived class so that it can be used just like function CDialog::<br />

DoModal().<br />

Please note that when the user clicks “OK” or “Cancel” button to dismiss the dialog box, the window<br />

will become hidden rather than being destroyed. The window will be destroyed only when the variable goes<br />

out of scope (e.g. when we use delete keyword to release the buffers if they are allocated by new key word,<br />

or after function returns if the variable is declared locally). So even after the dialog box is closed by<br />

clicking “OK” or “Cancel” button, it still can be restored by calling function CWind::ShowWindow(…) using<br />

SW_SHOW parameter.<br />

In the sample application, a dialog template IDD_DIALOG_MODELESS is prepared for modeless dialog box<br />

implementation. A new class CMLDialog is derived from CDialog, and a CWnd type pointer m_pParent along<br />

with a member function DoModeless() are declared in the class:<br />

……<br />

……<br />

class CMLDialog : public CDialog<br />

{<br />

public:<br />

CMLDialog(CWnd* pParent = NULL);<br />

void DoModeless();<br />

protected:<br />

CWnd *m_pParent;<br />

};<br />

The constructor of CMLDialog is modified as follows:<br />

CMLDialog::CMLDialog(CWnd* pParent /*=NULL*/)<br />

: CDialog(CMLDialog::IDD, pParent)<br />

{<br />

//{{AFX_DATA_INIT(CMLDialog)<br />

//}}AFX_DATA_INIT<br />

m_pParent=pParent;<br />

}<br />

When we implement a modal dialog box using class CDialog, the parent window needs to be specified<br />

only in the constructor. When calling CDialog::Create(…) to implement a modeless dialog box, we need<br />

to specify the parent window again even if it has been passed to the constructor. To let function<br />

DoModeless() has the same format with function CDialog::DoModal(), we store the pointer to the parent<br />

window in variable CMLDialog::m_pParent so that it can be used later in function DoModeless(). In the<br />

sample, function CMLDialog::DoModeless() is implemented as follows:<br />

142

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

Saved successfully!

Ooh no, something went wrong!