How to Use MFC Tab Control in Developing VC++ Applications
2015-03-02 22:10
1856 查看
In this article, I will show you how to use a VC++ MFC Tab Control in a dialog application.
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 .
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/17/7c307b43669cf4004fba481b790cb132)
2-Drag a Tab Control onto the Test dialog.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/17/a257e043e7db918270f300fc81075c69)
3-Change the ID as IDC_MyTab.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/17/9dfb8db303dd1b12f24a59d40380ec13)
4-Name the Tab as m_MyTab in the class wizard.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/17/bec409bdac2c27bb8f048c3fe2afd7cd)
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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/17/7ee2b157a7ee948dd4eed6e306e2056e)
Set style as “child”, border as “none” in the attributes.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/17/ec31c1f433e937cca35f3348ddb3cbc1)
6-Create the class of ChildDlg and name it as ChildDlg.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/17/a7b8050053eab0957ab04d12135df4bf)
7-Include the head file of ChildDlg in TestDlg.h
8-Create two pages for m_MyTab in OnInitialDlg() of CTestDlg.
9-Create two dialogs in CTestDlg.h file.
10-Create two dialogs’ map in OnInitialDlg() of CTestDlg.
11-Define the display boundary of dialogs and move the dialogs into the tab.
12-Initiate the display of dialog.
The whole code added in the OnInitialDlg() as follows:
13-Add TCN_SELCHANGE event for tab control.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/17/e93c9fd39cdc476d2e4b5b99927b8133)
Add code as follows:
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
4000
had change the attribute to “False”, all works.
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 .
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
4000
had change the attribute to “False”, all works.
相关文章推荐
- How to use ComboBox Control in VC++ - 在VC中如何使用 ComboBox 控件
- How to use toolbar control on dialog using CToolBar class in VC++ - VC++ 中用CToolBar 类如何使用ToolBar控件
- How to use a TControlCanvas in a component
- How to use binary mode for stdin/stdout in VC
- How To Use Git Source Control with Xcode in iOS 7
- How to use App.Config in WPF applications
- Know How And When To Use System.Message_Level To Control Messages In Oracle Forms
- How To Use WordBasic Functions in an MFC Automation Client for Word 97, Word 2000, Word 2002, or Word 2003
- How to use AlertDialog.Builder in Android applications
- How to create MFC applications that do not have a menu bar in Visual C++(MFC单文档和多文档程序中去掉菜单栏)(转)
- How to enumerate the system fonts in the combobox control using VC++ - 用VC++如何在ComboBox控件中枚举系统字体
- How to use ValidationSummary control in asp.net
- How to start a second thread in an MFC-based ActiveX control to fire events in Visual C++
- how to use javascript to control the usercotrol in the asp.net
- How To Use Git Source Control with Xcode in iOS 6
- Applications Programming in Smalltalk-80(TM):How to use Model-View-Controller (MVC)
- how to write your annotation types and make use of built-in annotations to control their behavior
- How to use For loop in CruiseControl.net
- (在做报文编辑器时右键菜单老不出来)This article explains how to display a context menu for a tree control in MFC
- How to keep Z-order of a control in MFC dialog?