📄 creating_a_wizard.shtml.htm
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<TITLE>PropertySheet - Creating a Wizard</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><td>
</tr>
</table>
<CENTER>
<H3>
<FONT COLOR="#AOAO99">Creating a Wizard</FONT></H3></CENTER>
<HR>
<P>This article was contributed by <A HREF="mailto:ppearson@broad.demon.co.uk">Peter Pearson</A>.
<p>A wizard is a very useful user interface concept. It allows an application to easily
guide a user through a complete task. The purpose of a wizard is to collect information on
a task in a structured way, while being an organised user interface, and not displaying
large numbers of dialogs. </p>
<p>A wizard achieves this by creating multiple page dialogs that the user steps through
using the navigation buttons at the bottom of the dialog.</p>
<p><img src="creating_a_wizard1.gif" tppabs="http://www.codeguru.com/propertysheet/creating_a_wizard1.gif" width="477" height="384" alt="creating_a_wizard1.gif (8742 bytes)"></p>
<p>Examples of wizards are the AppWizard in Visual C++ and the Install New Hardware wizard
in Windows.</p>
<p>In MFC, wizards are very easy to implement. MFC provides the CPropertySheet and
CPropertyPage classes for creating property sheets, and wizards are implemented in the
same fashion, but with some minor modifications. The easiest way to implement wizards is
to use the Insert Components and Controls option on the Project menu of Visual C++, and
select Property Sheet. This displays a wizard, which guides you through the process of
creating the wizard. In step 1, select the Wizard options button. Click Next, and then
Next again. This then displays a wizard page with the current classes in your program.
Select the class you want to call the wizard from and click Next. This wizard page asks
you how many pages you want in your wizard.</p>
<p>Select the number you want, from 1 to 9, and click Next. A list box is then displayed
with the names of the wizard and its property pages. Click Change to change the selected
one, and change the selected information, click OK, then Finish.</p>
<p>Close the insert component dialog box, and open the Resource View, and expand the
Dialog item. You should see that more dialogs have been created, depending on the number
of pages you wanted in your wizard. They are probably called IDD_PROPPAGE1 or something
like that. You can rename them, but you must also change their names in the wizard page
header (.h) file.</p>
<p>To call the wizard from your program open the code for the command to call it from and
use:</p>
<p><em> CWizard().DoModal();</em></p>
<p>Change CWizard to your wizard's main class name.</p>
<p>Compile and run your program. Select the command that calls your wizard. You should see
your wizard appear, but it will look very basic. You can add controls to the dialogs just
like other dialogs, and use them just like on dialogs.</p>
<p>To control the navigation buttons at the bottom of the wizard pages, you have to use
the features provided by CPropertyPage, using these messages:</p>
<p> <em>OnSetActive</em> is called when the page becomes active</p>
<p> <em>OnKillActive</em> is called when the page is no longer active</p>
<p> <em>OnWizardNext</em> is called when the wizard’s Next button
is pressed</p>
<p> <em>OnWizardBack</em> is called when the wizard’s Back button
is pressed</p>
<p> <em>OnWizardFinish </em> is called when the wizard’s Finish
button is pressed</p>
<p>The OnSetActive() message is the message that plays the main role in adding the button
functionality. Once a page is notified that it is active, it must then call
SetWizardButtons() in the parent CWizardDialog object to change the button states.</p>
<p>Use ClassWizard to add a OnSetActive() handler to each CPage class in your wizard.</p>
<p>In the first page's OnSetActive() function, add this code:</p>
<p><em> CWizard* pParent = (CWizard*)GetParent();</em></p>
<p><em> ASSERT_KINDOF(CWizard, pParent);</em></p>
<p><em> pParent->SetWizardButtons(PSWIZB_NEXT);</em></p>
<p> </p>
<p>This code gets a pointer to the parent CWizard and calls SetWizardButtons() to only
enable the Next button, and disable the Back button as it is the first wizard page.</p>
<p><em> </em></p>
<p>Replace CWizard with your wizard’s main class name, and add an include to the
wizard header file.</p>
<p>In the second page's OnSetActive() function, add this code:</p>
<p><em> CWizard* pParent = (CWizard*)GetParent();</em></p>
<p><em> ASSERT_KINDOF(CWizard, pParent);</em></p>
<p><em> pParent->SetWizardButtons(PSWIZB_BACK | PSWIZB_NEXT);</em></p>
<p><em> </em></p>
<p>This code gets a pointer to the parent CWizard and calls SetWizardButtons() to enable
both the Next button and the Back button.</p>
<p><em> </em></p>
<p>In the third page's OnSetActive() function, add this code:</p>
<p><em> CWizard* pParent = (CWizard*)GetParent();</em></p>
<p><em> ASSERT_KINDOF(CWizard, pParent);</em></p>
<p><em> pParent->SetWizardButtons(PSWIZB_BACK | PSWIZB_FINISH);</em></p>
<p><em> </em></p>
<p>This code gets a pointer to the parent CWizard and calls SetWizardButtons() to enable
the Next button, and to display the Finish button instead of the Next button as it is the
last page.</p>
<p> </p>
<p>The SetWizardButtons() function can have the following flags:</p>
<p> <em>SetWizardButtons(PSWIZB_NEXT)</em> Disables the Back button and
enables the Next button.</p>
<p align="left"> <em>SetWizardButtons(PSWIZB_BACK)</em> Disables the
Next button and enables the Back button.</p>
<p> <em>SetWizardButtons(PSWIZB_FINISH)</em> Disables the Back button,
and replaces the Next button with an enabled Finish button</p>
<p> <em>SetWizardButtons(PSWIZB_BACK | PSWIZB_NEXT)</em> Enables both
the Back and Next Buttons</p>
<p> <em>SetWizardButtons(PSWIZB_BACK | PSWIZB_FINISH)</em> Enables both
the Back and Finish Buttons</p>
<p> <em>SetWizardButtons(PSWIZB_BACK | PSWIZB_DISABLEDFINISH)</em>
Enables the Back and displays a disabled Finish button instead of the Next button.</p>
<p>The PSWIZB_NEXT and PSWIZB_FINISH flags cannot be used together, because they both use
the same button position.</p>
<p>To find out what button was pressed in the wizard when you display the wizard, use:</p>
<p> If( CWizardDialog().DoModal() == ID_WIZFINISH )</p>
<p> {</p>
<p> // Your Code</p>
<p> }</p>
<p>This code will process the code placed where // Your Code is if the Finish button is
pressed on your wizard.</p>
<p>The titles of the dialogs which make up your wizard will be used as the page titles
when the wizard is run.</font></p>
<p><font>For example, if the dialog of the wizard's first step has a
title "Wizard Step 1", then your wizard's title for the first wizard page will
also be "Wizard Step 1"</font></p>
<p><font>The dialog below shows the finished wizard's step 1, with
controls placed on the dialog, and the title changed.</font></p>
<p><img src="creating_a_wizard2.gif" tppabs="http://www.codeguru.com/propertysheet/creating_a_wizard2.gif" width="441" height="248" alt="creating_a_wizard2.gif (4177 bytes)"></p>
<P>Posted on: 3/15/98
<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="../index.htm" tppabs="http://www.codeguru.com/">Goto HomePage</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© 1997 Zafir Anjum</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
<CENTER><FONT SIZE=-2>3007</FONT></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -