VS2010/MFC dialog box: Creating and Displaying Wizard dialogs

Source: Internet
Author: User

wizard dialog box creation and display

This section shows you how to Create a wizard dialog box.

Still based on the example of the previous "addition Calculator", in which the wizard dialog box is added, we can use it to illustrate the usage of the addition calculator, step by step to guide the user operation, which is a more common use.

The addition calculator can be divided into three steps: input summand, input addend, point "Calculate" button.

Detailed Description of the wizard dialog box creation steps:

1. Create a Property Page Dialog resource

Based on the method described in Create Dialog template and modify dialog properties, right-click on the Dialog node of the Resource View and select Insert Dialog in the right-click menu to create the first dialog box template, the ID property of the dialog box is set to Idd_summand The _page,caption property changes to Addend page, the style property selects "Child" in the drop-down list, and the border property selects "Thin" in the drop-down list.

Delete the "OK" and "Cancel" buttons, add a static text box as described in adding Controls to the dialog box, and modify the Caption property of the static text box to "Enter double type summand first."

Follow the steps above to continue adding the second and third dialog resource. The ID of the second dialog template is set to the Idd_addend_page,caption property to "Addend page", a static text box is added, caption is set to "continue to enter double type Addend", and the other properties are the same as the first dialog box. The ID of the third dialog template is set to Idd_add_page,caption property to calculated page, the Caption property of the static text box is changed to "Last Press the calculation button", and the other properties are the same as the first dialog box.

2. Create a property page class

Follow the methods in Create dialog class and add control variables, right-click on the first dialog template, select "Add class" in the right-click menu, Pop-Up Class wizard dialog box, and enter the class name "Csummandpage" in the "Class name" edit box, unlike the previous one, Because the property page class should inherit from the CPropertyPage class, modify the "Base class" option below and select "CPropertyPage" in the drop-down list.

Because it is the first property page, it should have a "next" button, where to add it? As mentioned in the overloaded function of the CPropertyPage class, the OnSetActive function is used to handle the message that the property page is switched to the currently active page, so we can set it in the OnSetActive function.

How about overloading the OnSetActive function? We can find the "csummandpage" node in "Class View", right-click popup right-click menu, select "Properties", then VS2010 on the right panel will display the dialog box's property list, the property list on the toolbar has a tip message " Overrides "button, press it, listing the overloaded function in the list below, find" onsetactive ", point to the right of the blank list item appears down arrow, and then click the arrow appears below the" <add>onsetactive "option, Selecting it will automatically add the function onsetactive in the Csummandpage class.

We simply add the relevant code in the OnSetActive function body to achieve the effect of adding the "Next" button. The new function body is as follows:

C + + code
1 BOOL csummandpage::onsetactive ()2 {   3     //Todo:add your specialized code here and/or call the base class4   5     //get the parent window, which is the property sheet CPropertySheet class6cpropertysheet* Psheet = (cpropertysheet*) GetParent (); 7     //Set property sheet only the Next button8Psheet->setwizardbuttons (Pswizb_next); 9   Ten     returncpropertypage::onsetactive ();  One}

The property page classes Caddendpage and Caddpage are also added separately for the second and third dialog boxes. However, the property page of the second dialog box does not need to overload the OnSetActive function. The third dialog is the last dialog, so you do not need the Next button and should be replaced with the Finish button, so you also need to overload the OnSetActive function to set the Finish button. The onsetactive after overloading is as follows:

C + + code
BOOL caddpage::onsetactive ()   {       //  Todo:add your specialized code here and/or call the base class         //  get parent window, That is, the attribute   table CPropertySheet class    cpropertysheet* psheet = (cpropertysheet*) GetParent       (); // Set property sheet only "Finish" button       Psheet->setfinishtext (_t (" completed "));          return cpropertypage::onsetactive ();   }  

The preceding code snippet adds a _t to the string "done", because the default Unicode character set is used when the project is created, and if "done" is not _t, it is an ASCII string. _t is actually a macro, when the project's character set is selected as Unicode, the string is converted to a Unicode string, and an ASCII string is selected as Muli-byte. We can right-click on the addition root node of the Solution Explorer and select "Properties" on the right-click menu to bring up the project's property dialog box, Configuration properties-> The character set in the list on the right of general displays the selected character set.

That's it. The Finish button on the Third property page we want to do some processing, we overload the Onwizardfinish function, the method with the OnSetActive function. The Onwizardfinish function after overloading is as follows:

C + + code
BOOL caddpage::onwizardfinish ()   {       //  Todo:add your specialized code here and/or call the base class         //  Hint Wizard complete       MessageBox (_t ("The Usage Instructions Wizard has finished reading!") "));          return cpropertypage::onwizardfinish ();   }  

3. Create a property sheet class

After the property page resource and property page classes have been created, you cannot generate a wizard dialog box, and we also need a property sheet class to accommodate these property pages.

Right-click on the root node "addition" in the Solution Explorer view, select Add->class in the right-click menu, Pop Up the "Add Class" dialog, then select "MFC Class" in the middle area, click the "Add" button, Pop-Up another Class wizard dialog box, set class name to Caddsheet,base class select "CPropertySheet", click "Finish" button, so that the attribute table class is built.

Next, the header files for the three property page classes are included in the newly generated AddSheet.h:

#include "SummandPage.h"
#include "AddendPage.h"
#include "AddPage.h"

Then add the private variable in the AddSheet.h:

Csummandpage M_summandpage;
Caddendpage M_addendpage;
Caddpage M_addpage;

The two constructors of Caddsheet are then modified in the AddSheet.cpp file as:

C + + code
 Caddsheet::caddsheet (UINT nidcaption, Cwnd* pParentWnd, UINT iselectpage): Cprope Rtysheet (Nidcaption, pParentWnd, iselectpage) {  add three property pages to the property sheet  AddPage (&m_summandpage);       AddPage ( &m_addendpage);   AddPage ( &m_addpage);       } caddsheet::caddsheet (LPCTSTR pszcaption, CWnd  * pParentWnd, UINT Iselectpage) : CPropertySheet (Pszcaption, pParentWnd, iselectpage) { //  add three property pages to the property sheet  AddPage (&m_summandpage);       AddPage ( &m_addendpage);   AddPage ( &m_addpage); }  

4. Display the wizard dialog box

We add a button to the addition Calculator dialog box and click it to open the wizard dialog. This button has the ID set to the Idc_instruct_button,caption property set to "Instructions for use."

Add the Cadditiondlg handler for the Idc_instruct_button button to the Onbnclickedinstructbutton class, as described in adding a message handler function to the control. Then include the Caddsheet header file in the AdditionDlg.cpp file: #include "AddSheet.h". Finally, the Onbnclickedinstructbutton function is modified as follows:

C + + code
void Cadditiondlg::onbnclickedinstructbutton ()   {       //  Todo:add your control         notification handler codehere//  Create property sheet Object   /c8>    caddsheet Sheet (_t (""));        // Set Properties dialog Box for Wizard dialog box        Sheet. SetWizardMode ();        // Open Modal Wizard dialog box        Sheet. DoModal ();   }  

The wizard dialog box is complete and can be displayed on the Add Calculator dialog by clicking the "Use description" button. Let's take a look at the effect:

Just be addend page effect, click on it on the "next" button to continue to display the following two pages.

Is the wizard dialog box not as complex as it once thought? You can imagine, make more complex changes, and achieve more complete functionality.

VS2010/MFC dialog box: Creating and Displaying Wizard dialogs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.