Saturday, 9 March 2013

Responding to the Wizard's Buttons


Responding to the Wizard's Buttons

In the simplest case, MFC takes care of everything that needs to be done in order to flip
from one wizard page to the next. That is, when users click a button, MFC springs into
action and performs the Back, Next, Finish, or Cancel command. However, you'll often
want to perform some action of your own when users click a button. For example, you
may want to verify that the information that users entered into the currently
displayed page is correct. If there is a problem with the data, you can force users to fix
it before moving on.

To respond to the wizard's buttons, you override the OnWizardBack(), OnWizardNext(),
and OnWizardFinish() member functions. Use the Message Maps tab of ClassWizard to do
this; you'll find the names of these functions in the Messages window when a property
page class is selected in the Class Name box. When users click a wizard button, MFC
calls the matching function which does whatever is needed to process that page. An
example is the way the wizard in the Wizard Demo application won't let you leave Page
2 until you've checked the check box. This is accomplished by overriding the functions
shown in Listing 12.5.
Listing 12.5 Responding to Wizard Buttons
LRESULT CPage2::OnWizardBack()
{
CButton *checkBox = (CButton*)GetDlgItem(IDC_CHECK1);
if (!checkBox->GetCheck())
{
MessageBox("You must check the box.");
return -1;
}
return CPropertyPage::OnWizardBack();
}
LRESULT CPage2::OnWizardNext()
{
UpdateData();
if (!m_check)
{
MessageBox("You must check the box.");
return -1;
}
return CPropertyPage::OnWizardNext();
}
These functions demonstrate two ways to examine the check box on Page 2.
OnWizardBack() gets a pointer to the page's check box by calling the GetDlgItem()
function. With the pointer in hand, the program can call the check box class's
GetCheck() function, which returns a 1 if the check box is checked. OnWizardNext()
calls UpdateData() to fill all the CPage2 member variables with values from the dialog
box controls and then looks at m_check. In both functions, if the box isn't checked, the
program displays a message box and returns -1 from the function. Returning -1 tells MFC
to ignore the button click and not change pages. As you can see, it is simple to arrange
for different conditions to leave the page in the Back or Next direction.

No comments:

Post a Comment