Add the attribute page (zz) to the dialog box)

Source: Internet
Author: User

Add attribute page in the dialog box
Author: Huang chenliang

Download allSource code

When a dialog box-basedProgramWhen there are quite a few controls, you will surely think of using property pages to classify and place these controls. This article discusses several possible implementations of this method.

Solution 1

For examples of this solution, see SourceCodeProperty1 part of the package file

Place a tab Control on the dialog box, and then place the required control on the dialog box (in this example, two buttons are placed to try to display one in each label ). Then, use Class Wizard to create a control variable for the tab Control, which belongs to the ctabctrl class, and create corresponding control classes for other controls. In the initial function of the Main Dialog Box, add the following code to cproperty1dlg: oninitdialog:

 
// In this example, two tags are inserted. In actual use, you can insert the required number of tags in a loop. After running, m_tab.insertitem (0, _ T ("tab1") is selected by default ")); m_tab.insertitem (1, _ T ("tab2"); // hide the control that is not the first tag, leaving only the control m_button2.showwindow (sw_hide) you want );

Use classwizard to process the tcn_selchange message of tab control. In the message processing function, cwnd: showwindow is used to display and hide corresponding controls.

Void cproperty1dlg: onselchangetab1 (nmhdr * pnmhdr, lresult * presult) {// getcursel returns the index number of the selected tag (based on 0) int sel = upper (); Switch (SEL) {Case 0: m_button1.showwindow (sw_show); m_button2.showwindow (sw_hide); break; Case 1: lower (sw_show); m_button1.showwindow (sw_hide ); break;} * presult = 0 ;}

After doing so, the controls on the interface will be displayed in different labels, but this solution also has many disadvantages.

All controls are still in the same dialog box. It is inconvenient to edit them using the dialog box editor.
In order to be able to classify display controls, you must use classwizard to create a control variable for each control so that the showwindow function of the cwnd base class of each control variable can be used to display and hide the control variables. Sometimes, to use the DDX and DDV mechanisms for data exchange, you also need to create some variables that store values, which makes the entire dialog box class very large and difficult to operate.
Of course, you can also use arrays to store those control variables or value variables, but this is not the best. Sometimes some irrelevant control variables are put into an array, accessing the control through an array index with no actual meaning can cause trouble in programming. It is best to classify all the controls and place them in the dialog box class. These dialogs appear as subdialogs in the main dialog box. Yes. Now let's look at solution 2.

Solution 2

For an example of this solution, see the property2 section in the source code package file.
In this solution, I will use the ready-made cpropertysheet and cpropertypage classes in MFC to distribute controls to various dialog box classes.

First, add two (or more) dialog box resources. Modify the resource attributes of each dialog box and change the caption attribute of the dialog box to the text you want to display on the tab. Change the style attribute of the dialog box to child and the border attribute to thin. select only the title bar check box and remove other check boxes. Then you can add the controls to be separately displayed in these dialog boxes.

Create a dialog box class for the preceding dialog box resources, which is inherited from cpropertypage. In this way, the sub-Dialog Box classes are ready, and the Main Dialog Box class can be directly used as the cpropertysheet class. Use the following code:

 
Cpropertysheet sheet ("property page dialog box"); cpage1 page1; cpage2 page2; // Add sub-dialog box as a property page sheet. addpage (& page1); sheet. addpage (& page2); // generate a modal dialog box. You can also use the Create method to generate a non-modal dialog box (see msdn for details. domodal ();

In this case, the dialog box is as follows:

However, someone may ask how to place other controls in the main dialog box? If you use cpropertysheet directly, it is not possible, but don't forget that we can inherit our own classes from the cpropertysheet class! Let's take a look at solution 3.

Solution 3

For an example of this solution, see the property3 section in the source code package file.

The first step is to create the subdialog box classes to be displayed on the property page. The creation steps are the same as solution 2 and are inherited from cpropertypage.

This time, we will inherit our class from the cpropertysheet class (assuming the class name is cmysheet ). We need to put a button control here. Now, add a member variable m_button of the cbutton class to cmysheet.

In the oninitdialog () function of the cmysheet class, write as follows:

Bool bresult = cpropertysheet: oninitdiult (); // get the size of the property page crect rectwnd; getwindowrect (rectwnd); // adjust the width of the dialog box setwindowpos (null, 0, 0, rectwnd. width () + 100, rectwnd. height (), swp_nomove | swp_nozorder | swp_noactivate); crect rectbutton (rectwnd. width () + 25, 25, rectwnd. width () + 75, 75); // use a program to create a button m_button.create ("button", bs_pushbutton, crect (rectwnd. width (), 25, rectwnd. width () + 75, 50), this, 1); // display the button m_button.showwindow (sw_show); centerwindow (); Return bresult;

The effect is as follows:

Solution 3 although you can add controls in the main dialog box, it is also troublesome. The added controls can only be on the right or bottom of the property page. It is cumbersome to use programs to generate controls, and the location and size are not easy to control. There are other ways to add a property page in the dialog box and add controls in the Main Dialog Box at will? There are still. Let's take a look at solution 4.

Solution 4

For an example of this solution, see the property4 section in the source code package file.

This time we will not inherit our class from cpropertysheet, or use it directly. The subdialog classes on each property page are still required. The creation method is the same as the preceding two schemes.

First, create a dialog box-based project. You can add some required controls to edit an existing main dialog box, but you have to leave some space to place the property pages.

Add a member variable (m_sheet) of the cpropertysheet class to the Main Dialog Box class to represent the entire property page. Add instances of subdialog classes as member variables (m_page1, m_page2 ......).

In the oninitdialog () function of the Main Dialog Box class, add:

 
// Add a label. The label name is determined by the title bar of each subdialog box m_sheet.addpage (& m_page1); m_sheet.addpage (& m_page2); // create an attribute page m_sheet.create (this, ws_child | ws_visible, ws_ex_controlparent); rect; m_sheet.getwindowrect (& rect); int width = rect. right-rect. left; int Height = rect. bottom-rect. top; // adjust the size and position of the property page m_sheet.setwindowpos (null, 20, 50, 0, 0, swp_nosize | swp_nozorder | swp_noactivate );

The effect is as follows:

This solution allows you to add necessary controls in the main dialog box, and the controls on the property page are also scattered in various subdialog box classes, which is very convenient to use.

However, this also has some drawbacks: The Main Dialog Box cannot process the tag messages on the property page, that is, the Main Dialog Box cannot be notified when a tag is clicked. (I may be at a limited level. Theoretically, I should be able to solve this problem)

Solution 5

For an example of this solution, see section 5 of the source code package file.

This time, we still need to use tab control and inherit the class (ctabsheet) from the ctabctrl control class. (This method is from codeguru.Article, I will make some modifications to make it easier to use)

First, I will introduce how to use ctabsheet.

You must first create a subdialog box class. This subdialog box class should not be inherited from cpropertypage, but directly from cdialog. In addition, the attributes of the sub-Dialog Box resources should be set to: style, border, and none.

In the main dialog box, add a tab control and adjust the position and size as appropriate. Use classwizard to create a ctabsheet control variable for this tab control.

Add oninitdialog () in the Main Dialog Box:

 
M_sheet.addpage ("tab1", & m_page1, idd_dialog1); m_sheet.addpage ("tab2", & m_page2, idd_dialog2); m_sheet.show ();

In this way, you can create a perfect property page on the dialog box. The effect is exactly the same.

Next I will talk about the details of the ctabsheet class.

Ctabsheet is inherited from ctabctrl and is used as the control class for tab control. There is a member variable in the class to record the pointer cdialog * m_ppages [maxpage] for each subdialog box; maxpage is the maximum value of the labels that can be loaded by the class.

Class has an addpage method, used to record the sub-Dialog Box pointer and the ID of the used dialog box resource.

Bool ctabsheet: addpage (lpctstr title, cdialog * pdialog, uint ID) {If (maxpage = m_nnumofpages) return false; // Save the total number of subdialogs m_nnumofpages ++; // record the sub-Dialog Box pointer, resource ID, the text m_ppages [m_nNumOfPages-1] = pdialog; m_idd [m_nNumOfPages-1] = ID; m_title [m_nNumOfPages-1] = title; return true ;}

After adding subdialogs to addpage, you must call the show method of ctabsheet to generate tags and subdialogs.

 
Void ctabsheet: Show () {// use cdialog: Create to create a subdialog box, and use ctabctrl: insertitem to add the corresponding label for (INT I = 0; I <m_nnumofpages; I ++) {m_ppages [I]-> Create (m_idd [I], this); insertitem (I, m_title [I]);} // because the first label is selected by default when the dialog box is displayed, the first subdialog box should be displayed. Other subdialogs hide m_ppages [0]-> showwindow (sw_show ); for (I = 1; I <m_nnumofpages; I ++) m_ppages [I]-> showwindow (sw_hide); setrect ();}

After the tag and subdialog box are generated, call ctabsheet: setrect to calculate and adjust the size of the attribute page.

Void ctabsheet: setrect () {crect tabrect, itemrect; int NX, NY, nxc, NYC; // The getclientrect (& tabrect); getitemrect (0, & itemrect); // calculate the position and size of each subdialog box relative to tab control. Nx = itemrect. left; ny = itemrect. bottom + 1; nxc = tabrect. right-itemRect.left-2; NYC = tabrect. bottom-nY-2; // use the calculated data to adjust the sub-Dialog Box m_ppages [0]-> setwindowpos (& wndtop, NX, NY, nxc, NYC, swp_showwindow ); for (INT ncount = 1; ncount <m_nnumofpages; ncount ++) m_ppages [ncount]-> setwindowpos (& wndtop, NX, NY, nxc, NYC, swp_hidewindow );}

After you click the tab bar, the corresponding subdialog box is displayed, and the displayed subdialog box is hidden. Therefore, classwizard is used to process wm_lbuttondown messages.

 
Void ctabsheet: onlbuttondown (uint nflags, cpoint point) {ctabctrl: onlbuttondown (nflags, point); // determine whether other labels if (m_ncurrentpage! = Getcurfocus () {// hide the original subdialog box m_ppages [m_ncurrentpage]-> showwindow (sw_hide); m_ncurrentpage = getcurfocus (); // display the subdialog box m_ppages [m_ncurrentpage]-> showwindow (sw_show);} corresponding to the current tag );}}

In this way, the ctabsheet class can be used to easily place its own property pages in the dialog box, and controls are scattered in each subdialog box class, in line with the idea of object encapsulation. In addition, you can use classwizard to easily generate a message ing to process the tab control message. For example, you can process the tcn_selchange message to perform some actions when the tag is switched.

This article uses five methods to add attribute pages in the dialog box. Of course, there should be other methods. If you have a limited level, don't forget to tell me if you have a better method.

Author Email: hcl-hcl@21cn.com

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.