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

}<br />

Function CPropertySheet::AddPage(…) has only one parameter, it is a pointer to CPropertyPage type<br />

object.<br />

These are the necessary steps for implementing property sheet. For each property page, we can also<br />

add message handlers for the controls, the procedure of which is the same with that of a standalone dialog<br />

box.<br />

By default, the property sheet will be implemented in “tab” mode: there will be a tab control in the<br />

property sheet, which can be used to select property pages. The property sheet can also be implemented in<br />

“wizard” mode, in which case tab control will be replaced by two buttons (labeled with “Previous” and<br />

“Next”). In this mode, the pages can only be selected sequentially through button clickings.<br />

To enable wizard mode, all we need to do is calling function CPropertySheet::<br />

SetWizardMode()after all the pages have been added. For example, if we want to enable wizard mode in<br />

the sample, we should implement the constructor of CDBDlg as follows:<br />

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

{<br />

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

//}}AFX_DATA_INIT<br />

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);<br />

AddPage(&m_page1);<br />

AddPage(&m_page2);<br />

AddPage(&m_page3);<br />

SetWizardMode();<br />

}<br />

Sample 6.2-2\DB is the same with sample 6.2-1\DB, except that the property sheet is implemented in<br />

wizard mode.<br />

If we need to implement a property sheet dialog box in an SDI or MDI application, most of the steps<br />

are still the same. We can start by creating a new CPropertySheet based class, then adding dialog<br />

templates and CPropertyPage based classes, using them to declare new variables in CPropertySheet<br />

derived class, calling function CPropertySheet::AddPage(…) in its constructor. We can call function<br />

CPropertySheet::DoModal() at anytime to invoke the property sheet.<br />

6.3 Modeless Property Sheet<br />

Because property sheet is very similar to dialog box, implementation of modeless property sheet is also<br />

similar to that of modeless dialog box: when invoking the property sheet dialog box, instead of calling<br />

function CPropertySheet::DoModal(), we need to call CPropertySheet::Create(…) and CWnd::<br />

ShwoWindow(…). We can use exactly the same method discussed in section 6.1 to implement modeless<br />

property sheet.<br />

Sample 6.3\DB demonstrates how to implement modeless property sheet. It is a standard SDI<br />

application generated by Application Wizard. A command Property Sheet | Modeless is added to<br />

mainframe menu IDR_MAINFRAME, whose ID is ID_PROPERTYSHEET_MODELESS. A WM_COMMAND message<br />

handler is also added for this command, and the corresponding function is<br />

CDBDoc::OnPropertysheetModeless().<br />

A new class CMLPropertySheet is defined to implement modeless property sheet, whose base class is<br />

CPropertySheet. Like what we did in sample 6.1\DB, function DoModeless() is declared in class<br />

CMLPropertySheet, which can be used to invoke the property sheet.<br />

Three dialog box templates IDD_DIALOG_PAGE1, IDD_DIALOG_PAGE2 and IDD_DIALOG_PAGE3 are created<br />

to implement property pages. Also three new classes CPage1, CPage2 and CPage3 are derived from<br />

CPropertyPage. A CWnd type pointer m_pParentWnd and three other member variables declared by CPage1,<br />

CPage2 and CPage3 are added to class CMLPropertySheet. The following is the modified class:<br />

class CMLPropertySheet : public CPropertySheet<br />

{<br />

DECLARE_DYNAMIC(CMLPropertySheet)<br />

145

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

Saved successfully!

Ooh no, something went wrong!