VC Study Notes: attribute form and Wizard
Skyseraph nov.9th 2010 hqu
Email-zgzhaobo@gmail.com QQ-452728574
Latest modified Date: nov.9th 2010 hqu
Before 0
An Attribute form consists of one or more attribute pages.
Cpropertypage () |
Derived from the cwnd class-c1_target-cobject |
|
Cpropertysheet () |
Derived from cdialog class-cwnd-c1_target-cobject |
|
1. Create an attribute page
① Insert three property pages: Resource idd_propertypage_large, ID: idd_prop1/2/3, caption: page1/2/3, respectively. Insert a property page and dialog box.
② Create a property page form
Property page 1 |
Three single-choice buttons: User Occupation Selection and user Location Selection in a list box |
|
Property page 2 |
Four multiple selection boxes indicate user interest Selection |
|
Property page 3 |
A combo box indicates salary level selection |
Size adjustment tips; sort attributes are selected by default. after removal, the strings in the combox list are arranged in the order of addition. |
③ Associate the three property pages with the property page class cprop1/2/3, and the base class is cpropertypage.
2. Create an attribute form
Idea: Create a cpropertysheet object, create a cpropertypage object for each attribute page in this object, call the addpage function to add each attribute page, call domodule/create display/create Modal/non-modal attribute form
① Create an attribute form object: Create a class cpropsheet and use cpropertysheet as the base class.
② Add the property page to the header file of the Property Form class cpropsheet,CodeAs follows: [two constructors are added]
Cpropsheet: cpropsheet (uint nidcaption, cwnd * pparentwnd, uint iselectpage)
: Cpropertysheet (nidcaption, pparentwnd, iselectpage)
{
Addpage (& m_prop1 );
Addpage (& m_prop2 );
Addpage (& m_prop3 );
}
Cpropsheet: cpropsheet (lpctstr pszcaption, cwnd * pparentwnd, uint iselectpage)
: Cpropertysheet (pszcaption, pparentwnd, iselectpage)
{
Addpage (& m_prop1 );
Addpage (& m_prop2 );
Addpage (& m_prop3 );
}
The header file of the attribute form class must contain
# Include "prop1.h"
# Include "prop2.h"
# Include "prop3.h"
③ Display the property form: add the menu item idm_propertysheet and the message response function [View Class], include the property form Class header file in the view header file, and then add the Code:
Void ctest_dlgview: onpropertysheet ()
{
Cpropsheet propsheet ("attribute form ");
Propsheet. domodal ();
}
3. Create a wizard
① Creation Wizard:
Void ctest_dlgview: onpropertysheet ()
{
Cpropsheet propsheet ("attribute form ");
Propsheet. setwizardmode (); // change the property page to a wizard
/* However, the wizard has special Buttons such as "previous", "Next", and "complete. Therefore, you must modify the corresponding values for the classes that represent the three property pages to ensure that the previous step does not appear on the first page and the last page ends. */
Propsheet. domodal ();
}
② Improvement: Modification of buttons in the Wizard dialog box
Button modification in the Wizard dialog box |
Setwizardbuttons provided by the cpropertysheet class |
Setwizardbuttons function call |
Generally, the property page needs to call setwizardbuttons in the virtual function onsetactive, and the virtual function will be overwritten in the three property pages. |
Code:
Bool cprop1: onsetactive ()
{
(Cpropertysheet *) getparent ()-> setwizardbuttons (pswizb_next );
// Void setwizardbuttons (DWORD dwflags); enables or disables the back, next, or finish button in a wizard property sheet.
Return cpropertypage: onsetactive ();
}
Bool cprop2: onsetactive ()
{
(Cpropertysheet *) getparent ()-> setwizardbuttons (pswizb_back | pswizb_next );
Return cpropertypage: onsetactive ();
}
Bool cprop3: onsetactive ()
{
(Cpropertysheet *) getparent ()-> setwizardbuttons (pswizb_back | pswizb_finish );
Return cpropertypage: onsetactive ();
}
4. Attribute page processing
4.1 first page:
① It is the variable m_occupation [value category, int type] associated with the single-choice button control on property page 1, and initialized to-1 in its constructor. The first single-choice button on property page 1 needs to be checked on group!
② Rewrite the virtual function onwizardnext to process the next button response on the property page. This function returns 0 to go to the next step. If-1 is returned, the property page is forbidden to change.
Lresult cprop1: onwizardnext ()
{
Updatedata (); // the data exchange between the control and member variables is implemented through dodateexchange,ProgramIf you do not call this function directly, you must use updatedata to call it.
If (m_occupation =-1)
{
MessageBox ("Please input your carrer! ");
Return-1;
}
Return cpropertypage: onwizardnext ();
}
③ Determine the working location of the list box: First, you need to add some locations/resources to the list box before the display of the property page, completed in wm_initdialog
Bool cprop1: oninitdialog ()
{
Cpropertypage: oninitdialog ();
// Todo: add additional initialization here
(Clistbox *) getdlgitem (idc_list1)-> addstring ("Shang Hai ");
(Clistbox *) getdlgitem (idc_list1)-> addstring ("Beijing ");
(Clistbox *) getdlgitem (idc_list1)-> addstring ("Xiamen ");
Return true; // return true unless you set the focus to a control
// Exception: the OCX attribute page should return false.
}
④ It is the management variable m_workwork [value category, cstring type] of the list box control, and initialized as _ T ("") in its constructor, and then modified ②
Lresult cprop1: onwizardnext ()
{
Updatedata (); // the data exchange between the control and the member variable is implemented through dodateexchange. The program does not directly call this function and needs to call it through updatedata.
If (m_occupation =-1)
{
MessageBox ("Please input your carrer! ");
Return-1;
}
If (m_workaddr = "")
{
MessageBox ("Please choose your city! ");
Return-1;
}
Return cpropertypage: onwizardnext ();
}
4.2 Second page:
① Associate variables with four multiple-choice controls: bool type, m_basketrball, m_music, m_billards, m_dance
② Rewrite the virtual function onwizardnext to process the next button response on the property page.
Lresult cprop2: onwizardnext ()
{
// Todo: Add dedicated code and/or call the base class here
Updatedata ();
If (m_dance | m_billards | m_basketrball | m_music)
{
Return cpropertypage: onwizardnext (); // flexibility
}
Else
{
MessageBox ("Please choose your interests! ");
Return-1;
}
}
4.3 Third page:
① Add resources for the combo box control, same as 4.2 ③, code:
Bool cprop3: oninitdialog ()
{
Cpropertypage: oninitdialog ();
// Todo: add additional initialization here
(Ccombobox *) getdlgitem (idc_combo2)-> setcursel (0); // initial options
(Ccombobox *) getdlgitem (idc_combo2)-> addstring ("<= 5000 ");
(Ccombobox *) getdlgitem (idc_combo2)-> addstring ("5000 ~ 8000 ");
(Ccombobox *) getdlgitem (idc_combo2)-> addstring ("8000 ~ 12000 ");
(Ccombobox *) getdlgitem (idc_combo2)-> addstring ("> 12000 ");
Return true; // return true unless you set the focus to a control
// Exception: the OCX attribute page should return false.
}
② Cancel the sort attribute
5. Accept the selection made in the User Wizard.
5.1 process the third property page
① Add member variable m_strsalary for the third property page, cstring type
② Rewrite the onwizardfinish function.
Bool cprop3: onwizardfinish ()
{
// Todo: Add dedicated code and/or call the base class here
Int index;
Index = (ccombobox *) getdlgitem (idc_combo2)-> getcursel (); // obtain the index value
(Ccombobox *) getdlgitem (idc_combo2)-> getlbtext (index, m_strsalary); // obtain a string from the specified position in the combo box
Return cpropertypage: onwizardfinish ();
}
5.2 View class Processing
① To accept the selection in the User Wizard and add member variables for the class:
Initialize in its constructor:
M_ioccupation =-1;
M_strworkaddr = "";
M_strsalary = "";
Memset (m_blike, 0, sizeof (m_blike ));
② Output the User Wizard selection to the window
Void ctest_dlgview: onpropertysheet ()
{
Cpropsheet propsheet ("attribute form ");
Propsheet. setwizardmode (); // change the property page to a wizard
/* However, the wizard has special Buttons such as "previous", "Next", and "complete. Therefore
You must modify the corresponding values for the classes that represent the three property pages to ensure that the previous step does not appear on the first page and the last page ends. */
// Propsheet. domodal ();
If (id_wizfinish = propsheet. domodal ())
// Generally, the return value of the domodal function of the cpropertysheet class is idok or idcancel. However, if the attribute form has been created as a wizard, the return value is id_wizfinish or idcancel.
{
M_ioccupation = propsheet. m_prop1.m_occupation;
M_strworkaddr = propsheet. m_prop1.m_workaddr;
M_blike [0] = propsheet. m_prop2.m_music;
M_blike [1] = propsheet. m_prop2.m_basketrball;
M_blike [2] = propsheet. m_prop2.m_billards;
M_blike [3] = propsheet. m_prop2.m_dance;
M_strsalary = propsheet. m_prop3.m_strsalary;
Invalidate (); // indicates that the view is invalid, causing repainting. ondraw is automatically called.
}
}
③ Complete output in ondraw
Void ctest_dlgview: ondraw (CDC */* PDC */)
{
Ctest_dlgdoc * pdoc = getdocument ();
Assert_valid (pdoc );
If (! Pdoc)
Return;
// Todo: add the drawing code for the local data here
Cclientdc DC (this); // obtain the customer zone memory DC
Cfont font;
Font. createpointfont (300, "文 ");
Cfont * poldfont;
Poldfont = Dc. SelectObject (& font );
Cstring strtemp;
Strtemp = "Your Career :";
Switch (m_ioccupation)
{
Case 0:
Strtemp + = "engineer ";
Break;
Case 1:
Strtemp + = "CEO ";
Break;
Case 2:
Strtemp + = "CTO ";
Break;
Default:
Break;
}
DC. textout (0, 0, strtemp );
Strtemp = "Your Work Location :";
Strtemp + = m_strworkaddr;
Textmetric TM;
DC. gettextmetrics (& TM); // get the current text height
DC. textout (0, TM. tmheight, strtemp );
Strtemp = "Your hobbies :";
If (m_blike [0])
{
Strtemp + = "basketball ";
}
If (m_blike [1])
{
Strtemp + = "billiards ";
}
If (m_blike [2])
{
Strtemp + = "Music ";
}
If (m_blike [3])
{
Strtemp + = "dance ";
}
DC. textout (0, TM. tmheight * 2, strtemp );
Strtemp = "your salary :";
Strtemp + = m_strsalary;
DC. textout (0, TM. tmheight * 3, strtemp );
DC. SelectObject (poldfont );
}
Author: skyseraph
Email/Gtalk: zgzhaobo@gmail.com QQ: 452728574
From: http://www.cnblogs.com/skyseraph/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Please respect the author's Labor achievements.