MFC implements wizard functionality with paging technology in a single dialog box

Source: Internet
Author: User

The so-called Wizard function, in the Win32 program is often seen in the installation program or program in the Setup Wizard, the wizard can guide users to accomplish something, without the user to face a lot of complex set of content. At the same time, the main function is the reality of a single dialog in the paging technology, if one time in the dialog box to render the entire content, then this dialog box to be very large and less friendly. So, how to implement paging in a dialog box is also what you need to know.


I. BASIC OBJECTIVES

The following is a simple example to illustrate this problem, in a wizard, there are three pages, each page has a dialog box. And at the bottom of each page, there is a previous page and the next page of the button, click the "OK" button, can be the user input content realistic.


Second, the production process

1, the first to create a new MFC project, based on the dialog box, static DLL, how to create a new can see me before the "MFC" Student information management, to achieve the list control node additions and deletions (click the Open link), and then create a new Three dialog box, dialog box style settings as shown, Note that the style of the dialog box is set to the style of the inner dialog, without the border, the style is child, and in each dialog box, put an edit box, and add some irrelevant static text ID is which dialog box, of course, you can also make beautiful.


2, after each dialog box, according to Ctrl+w set up the Class Wizard, their class name is Cpage1,cpage2,..., after the completion of the Class Wizard, switch to the Member Variable tab, for the inside of the edit box new member variable, double-click Idc_edit1 can, In addition to the name of the member variable, in fact, the variable type and so do not need to modify, are named M_edit1, then you can according to which dialog box to distinguish


3. Then set the main dialog box, for example, drag the two buttons to the bottom right, then drag the two buttons to the lower left, one is the back button, the other is the Next button, where the "back" button is disabled at first.


4, the main dialog box by default has established a class function. We now want to create a new member variable for the three newly created dialog boxes in the class function of the main dialog box so that they can be manipulated in the main dialog box. As shown in. In addition to the new CPage1 m_p1,cpage2 m_p2 ... , create an int nsel, which is used to record the variables that turn to page number.


5, such as, after the final new all variables, can be in the ClassView view, clearly see, below we want to initialize this Nsel, here is not like Java, not VC, a nsel=0 is over.


6, we need to click Cpagingdlg::cpagingdlg (cwnd* pparent/*=null*/): CDialog (Cpagingdlg::idd, pparent) for Nsel initialization, otherwise the project will error, unable to start.


7, after all the preparation work is done, we can finally go to the main dialog box of the bool Cpagingdlg::oninitdialog () function to write the code.


8, the BOOL Cpagingdlg::oninitdialog () function writes the following code, the core of the whole function is when this dialog is initialized, how to put the generated three dialog box into the main dialog box, and how to put it, to put the sense of no vainly disobey. At the same time, after putting the dialog box 1 realistic, the default dialog box is not displayed, you must call the function.

BOOL Cpagingdlg::oninitdialog () {cdialog::oninitdialog ();//Generate three dialog boxes M_p1 first. Create (idd_dialog1,this); m_p2. Create (idd_dialog2,this); m_p3. Create (Idd_dialog3,this); CRect Rect,rt; GetClientRect (rect);//Find the top coordinate of the main dialog box//find the coordinates getdlgitem (IDOK)->getwindowrect (RT) of the "OK" button IDOK the main dialog box; ScreenToClient (RT);//The size of the generated dialog box, the head is in the head of the main dialog box, and the bottom is at the top of the OK button Rect.bottom = rt.top;m_p1. MoveWindow (rect); m_p2. MoveWindow (rect); m_p3. MoveWindow (rect); m_p1. ShowWindow (sw_show);//Set The icon for this dialog.  The framework does this automatically//when the  application ' s main window was not a Dialogseticon (M_hicon, TRUE);//Se T Big Iconseticon (M_hicon, FALSE);//Set small icon//todo:add extra initialization herereturn TRUE;  Return TRUE  Unless you set the focus to a control}
9, then is the paging function, we like, in the main dialog box to create a new page function void Cpagingdlg::selectpage (), of course, you can hand-written, but we want to play Win32 graphics programming!


This function is as follows:

void Cpagingdlg::selectpage () {//Put Dialog 1, dialog Box 2, dialog 3 into an array, the variable type of this array is rather strange, no why, anyway, everyone uses it. Among them m_p1,2,.. Yes, just what we just established in the main function is the member variable of the three dialog box cpage1,2,3 cwnd* ps[] = {&m_p1,&m_p2,&m_p3};//sizeof (PS)/sizeof (Ps[0]) Equivalent to the Ps.length,ps array length, but C + + here does not encapsulate this method, there is no way, can only write! for (int i=0;i<sizeof (PS)/sizeof (ps[0]), i++)//See what the global variable M_sel is at this time, show the M_sel page, the others are hidden Ps[i]->showwindow (i==m_sel Sw_show:sw_hide);//after which, if you turn to the end, disable the next page button, if the first page disables the previous button, other cases show that//enablewindow (1) Can turn this button on, EnableWindow (0) Disable the button Wherein, here the 1 and 0 pass M_sel whether is 2 with the GetDlgItem Judgment (Idc_button2)->enablewindow (m_sel!=2); GetDlgItem (Idc_button1)->enablewindow (m_sel!=0);}

Such a paging algorithm, →_→ is not the operating system virtual memory which paging, do not think crooked, I have not so cow, in fact, can also be applied to the JavaScript tab, not like I before in the "JavaScript" original Eco-compatible IE6 label page (Click the open link) to write a tab so complex. Get a chance to write JavaScript's tag page code shorter and sexier.

10, after the core step is completed, Button1 's message mapping function, that is, the "Back" button becomes simple

void Cpagingdlg::onbutton1 () {//Todo:add your control notification handler code herem_sel--; Selectpage ();}

11, the Next button is also the same, is to put this page backward, call the above paging function can be

void Cpagingdlg::onbutton2 () {//Todo:add your control notification handler code herem_sel++; Selectpage ();}

12, finally, is the "OK" button, IDOK the message map function, it to get the information in the three-page dialog box.

void Cpagingdlg::onok () {CString cs;//this remembers UpdateData (), the contents of the memory, that is, the user input content, updated to the program variable inside M_P1. UpdateData (); m_p2. UpdateData (); m_p3. UpdateData (); cs+= "1th page entered:";//This represents the dialog box 1, that is, the 1th page of the dialog box, very simple cs+=m_p1.m_edit1;cs+= "2nd page entered:"; cs+=m_p2.m_edit1;cs+= " The 3rd page entered: "; cs+=m_p3.m_edit1;//the whole string, the pop-up window will be AfxMessageBox (CS);}


MFC implements wizard functionality with paging technology in a single dialog box

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.