Use tab control in MFC

Source: Internet
Author: User

To be honest, when I first saw tab control, I thought it was very simple. When I used it, I found that I was wrong.

You still need some skills to make good use of it. After searching for materials on the Internet and my own exploration, I recorded some key points here.

The Running Effect of tab control is a bit like property sheet, but there are some differences between the two. In my understanding, the property sheet is mainly used in the dialog box to manage data categories. Tab control has a wider application scope. It can be used in both dialog boxes and views. In addition to managing Configuration data, you can also plan the software organization, for example, you can use it to switch between different views.

Of course, this is not without cost. The programming of tab control is much more complicated than the property sheet.

I didn't understand how to use different pages in tab control at first. Just like the property page, Tab control does not provide a convenient mechanism for you to do this easily. Fortunately, VC is the best ~ This can be done through a work und.

No worries.Code.

Assume that I have an SDIProgramThe view is form view. You want to add a tab control on it, which contains two pages. Now let's take a look at what to do.

First of all, you must add a tab control resource, and then use Class Wizard to add a control variable to the view.

Next, create two dialog box resources. Don't forget to change style to child and border to none. Then you can add other controls.

Then, use the Class Wizard to create two classes for the two dialogs, such as cpage1 and cpage2.

Then, add the two dialog box objects to the view header file. At the same time, a variable int m_curseltab is added to indicate which page is about to be switched.

To avoid the program enumeration of the tab index when you switch the tab, you can use arrays to do this.

You need to associate cpage1, cpage2, and Tab control in the initialization function of view, save the page address, set the initial Page, and so on.

Void ctab_testview: oninitialupdate ()
{
Cformview: oninitialupdate ();
Getparentframe ()-> recalclayout ();
Resizeparenttofit ();

// Add two pages for tab Control
M_tab.insertitem (0, _ T ("first "));
M_tab.insertitem (1, _ T ("second "));

// Create two dialog boxes
M_page1.create (idd_dialog1, & m_tab );
M_page2.create (idd_dialog2, & m_tab );

// Set the display range in the tab.
Crect RC;
M_tab.getclientrect (RC );
RC. Top + = 20;
RC. Bottom-= 8;
RC. Left + = 8;
RC. Right-= 8;

M_page1.movewindow (& rc );
M_page2.movewindow (& rc );

// Save the object pointer of the dialog box
Pdialog [0] = & m_page1;
Pdialog [1] = & m_page2;

// Display the initial Page
Pdialog [0]-> showwindow (sw_show );
Pdialog [1]-> showwindow (sw_hide );

// Save the current selection
M_curseltab = 0;
}

Note that a cdialog pointer array is used for saving. The size of the array is the number of tab control pages, the array subscript corresponds to the index of each page (this facilitates quick access ).

When switching, You need to respond to related messages.

Void ctab_testview: onselchangetab1 (nmhdr * pnmhdr, lresult * presult)
{
// Todo: add your control notification handler code here

Pdialog [m_curseltab]-> showwindow (sw_hide );
M_curseltab = m_tab.getcursel ();
Pdialog [m_curseltab]-> showwindow (sw_show );

* Presult = 0;
}

First, we will hide the current page, get a new page index, and then display the relevant page. This is much easier than enumeration.

Another interesting point is the application of the DDX/DDV mechanism. You can use the DDX/DDV mechanism to obtain the data of each tab control page. However, you need to explicitly call multiple pages because they are multiple pages.

Void ctab_testview: onbutton1 ()
{
// Todo: add your control notification handler code here
M_page1.updatedata ();
M_page2.updatedata ();
Cstring str1 = m_page1.m_str1;
Cstring str2 = m_page2.m_str2;
Afxmessagebox (str1 );
Afxmessagebox (str2 );
}

After these steps, we can use the powerful function of tab control.

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.