Creating Wizard Pages
As far as your application's resources go, you create wizard pages exactly as you createproperty sheet pages - by creating dialog boxes and changing the dialog box styles. The
dialog titles - Page 1 of 3, Page 2 of 3, and Page 3 of 3 - are hardcoded onto each dialog
box. You associate each dialog box resource with an object of the CPropertyPage class.
Then, to take control of the pages in your wizard and keep track of what users are
doing with the wizard, you override the OnSetActive(), OnWizardBack(),
OnWizardNext(), and OnWizardFinish() functions of your property page classes. Read on
to see how to do this.
Displaying a Wizard
The File, Wizard command is caught by CWizView's OnFileWizard() function. It's very
similar to the OnPropSheet() function in the Property Sheet demo, as you can see from
Listing 12.3. The first difference is the call to SetWizardMode() before the call to
DoModal(). This function call tells MFC that it should display the property sheet as a
wizard rather than as a conventional property sheet. The only other difference is that
users arrange for property sheet changes to be accepted by clicking Finish, not OK, so
this code checks for ID_WIZFINISH rather than IDOK as a return from DoModal().
Listing 12.3 CWizView::OnFileWizard()
void CWizView::OnFileWizard()
{
CWizSheet wizSheet("Sample Wizard", this, 0);
wizSheet.m_page1.m_edit = m_edit;
wizSheet.m_page2.m_check = m_check;
wizSheet.SetWizardMode();
int result = wizSheet.DoModal();
if (result == ID_WIZFINISH)
{
m_edit = wizSheet.m_page1.m_edit;
m_check = wizSheet.m_page2.m_check;
Invalidate();
}
}
Setting the Wizard's Buttons
MFC automatically calls the OnSetActive() member function immediately upon
displaying a specific page of the wizard. So, when the program displays Page 1 of the
wizard, the CPage1 class's OnSetActive() function is called. You add code to this
function that makes the wizard behave as you want. CPage1::OnSetActive() looks like
Listing 12.4.
Listing 12.4 CPage1::OnSetActive()
BOOL CPage1::OnSetActive()
{
CPropertySheet* parent = (CPropertySheet*)GetParent();
parent->SetWizardButtons(PSWIZB_NEXT);
return CPropertyPage::OnSetActive();
}
OnSetActive() first gets a pointer to the wizard's property sheet window, which is the
page's parent window. Then the program calls the wizard's SetWizardButtons()
function, which determines the state of the wizard's buttons. SetWizardButtons() takes
a single argument, which is a set of flags indicating how the page should display its
buttons. These flags are PSWIZB_BACK, PSWIZB_NEXT, PSWIZB_FINISH, and
PSWIZB_DISABLEDFINISH. Because the call to SetWizardButtons() in Listing 12.4
includes only the PSWIZB_NEXT flag, only the Next button in the page will be
enabled.
Because the CPage2 class represents Page 2 of the wizard, its call to
SetWizardButtons() enables the Back and Next buttons by combining the appropriate
flags with the bitwise OR operator (|), like this:
parent->SetWizardButtons(PSWIZB_BACK | PSWIZB_NEXT);
Because Page 3 of the wizard is the last page, the CPage3 class calls
SetWizardButtons() like this:
parent->SetWizardButtons(PSWIZB_BACK | PSWIZB_FINISH);
This set of flags enables the Back button and provides a Finish button instead of a Next
button.
No comments:
Post a Comment