How to Use MFC Tab Control in Developing VC++ Applications

In this article, I will show you how to use a VC++ MFC Tab Control in a dialog application.

Introduction

One of the most aggravating things about using a tab control is having to use property sheets or embedding child dialogs into the tab control. Both approaches work, but cause the program to have more code than I think is necessary. So I decided to create a very simple way to use tab control class. This method is very easy to use, and requires very little coding, and should save you a lot of time! It supports as many tabs as you need right out of the box, and can create as many controls as you need as well!

Example

This example is based on Visual Studio 2010.

1-First, Let’s create a new MFC project named Test and save it. This project is based on dialog, so you must choose this option. The Integrated Developing Environment will create a dialog named Test automatically, and three classes(CAboutDlg, CTestApp, CTestDlg) are built .

The initial interface

2-Drag a Tab Control onto the Test dialog.
这里写图片描述

3-Change the ID as IDC_MyTab.
这里写图片描述

4-Name the Tab as m_MyTab in the class wizard.
这里写图片描述

5-Insert another dialog named ChildDlg and drag four Static Texts and Edit Controls with any names. In the next steps, we will insert this dialog into the tab control and display it.
这里写图片描述

Set style as “child”, border as “none” in the attributes.
这里写图片描述

6-Create the class of ChildDlg and name it as ChildDlg.
这里写图片描述

7-Include the head file of ChildDlg in TestDlg.h

// TestDlg.h : 头文件
//

#pragma once
#include "ChildDlg.h"

8-Create two pages for m_MyTab in OnInitialDlg() of CTestDlg.

m_MyTab.InsertItem(0,_T("Velocity"));
m_MyTab.InsertItem(1,_T("Temperature"));

9-Create two dialogs in CTestDlg.h file.

ChildDlg m_Dialog1;
ChildDlg m_Dialog2;

10-Create two dialogs’ map in OnInitialDlg() of CTestDlg.

m_Dialog1.Create(IDD_ChildDlg, &m_MyTab);
m_Dialog2.Create(IDD_ChildDlg, &m_MyTab);

11-Define the display boundary of dialogs and move the dialogs into the tab.

CRect rc;
m_MyTab.GetClientRect(rc);
rc.top += 20;
rc.bottom -= 10;
rc.left += 10;
rc.right -= 10;
m_Dialog1.MoveWindow(&rc);
m_Dialog2.MoveWindow(&rc);

12-Initiate the display of dialog.

m_Dialog1.ShowWindow(TRUE);
m_Dialog2.ShowWindow(FALSE);
m_MyTab.SetCurSel(0);

The whole code added in the OnInitialDlg() as follows:

    m_MyTab.InsertItem(0,_T("Velocity"));
    m_MyTab.InsertItem(1,_T("Temperature"));

    m_Dialog1.Create(IDD_ChildDlg, &m_MyTab);
    m_Dialog2.Create(IDD_ChildDlg, &m_MyTab);

    CRect rc;
    m_MyTab.GetClientRect(&rc);
    rc.top += 20;
    rc.bottom -= 10;
    rc.left += 10;
    rc.right -= 10;
    m_Dialog1.MoveWindow(&rc);
    m_Dialog2.MoveWindow(&rc);

    m_Dialog1.ShowWindow(TRUE);
    m_Dialog2.ShowWindow(FALSE);
    m_MyTab.SetCurSel(0);

13-Add TCN_SELCHANGE event for tab control.
这里写图片描述

Add code as follows:

void CTestDlg::OnTcnSelchangingMytab(NMHDR *pNMHDR, LRESULT *pResult)
{
    int CurSel = m_MyTab.GetCurSel();
    switch(CurSel)
    {
        case 0:
            m_Dialog1.ShowWindow(true);
            m_Dialog2.ShowWindow(false);
            break;
        case 1:
            m_Dialog1.ShowWindow(false);
            m_Dialog2.ShowWindow(true);
            break;
        default:
            ;
    }   
    *pResult = 0;
}

Till now, the application works perfectly. But when you try to edit the editbox of the childdlg, bugs appears: the editboxs can’t be edited anyway. This bug may not appear in you application, but it confused me this whole afternoon. I searched many ways on Internet and try to solve it, including a lot of codes, but all failed. Finally, before dinner, I happen to find the “disabled” attribute of the childdlg is “True”. It means that the childdlg can never be edit anyway…… So after I had change the attribute to “False”, all works.
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值