在VC++编程技术中使用Tab Control控件
小职 2018-07-05 来源 : 阅读 1244 评论 0

摘要:本文主要向大家介绍了在VC++编程技术中使用Tab Control控件,通过具体的内容向大家展示,希望对大家学习VC++编程技术有所帮助。

本文主要向大家介绍了在VC++编程技术中使用Tab Control控件,通过具体的内容向大家展示,希望对大家学习VC++编程技术有所帮助。

系统环境:Windows 7
软件环境:Visual Studio 2008 SP1
本次目的:在模态或非模态对话框中使用Tab Control控件,及引申在单/多文档中使用

 

查阅MSDN文档,对于创建Tab Control控件,MSDN上说明如下:

To use CTabCtrl directly in a dialog box

1.     In the dialog editor, add a Tab Control to your dialog template resource. Specify its control ID.

2.     Use the Add Member Variable Wizard to add a member variable of type CTabCtrl with the Control property. You can use this member to call CTabCtrl member functions.

3.     Map handler functions in the dialog class for any tab control notification messages you need to handle. For more information, see Mapping Messages to Functions.

4.     In OnInitDialog, set the styles for the CTabCtrl .

To use CTabCtrl in a nondialog window

1.     Define the control in the view or window class.

2.     Call the control's Create member function, possibly in OnInitialUpdate, possibly as early as the parent window's OnCreate handler function (if you're subclassing the control). Set the styles for the control.

对于直接使用 CTabCtrl 在对话框内

1.          在对话框编辑区内,添加一个 Tab Control 控件到资源模板里面,设置它的控制 ID

2.          使用添加成员变量向导,为控件添加一个 CTabCtrl 类型的成员变量,你可以使用这个变量调用 CTabCtrl 的成员函数

3.          对话框类的映射处理功能可以处理任何你需要处理的标签控件消息。有关更多信息,请参阅消息映射函数。

4.          在 OnInitDialog() 函数里面,设置 CTabCtrl 的风格。

对于在非对话框窗口使用 CTabCtrl

1.          定义在视图或窗口类的控件。

2.          调用控件的创建成员函数,可能在 OnInitialUpdate 中,可能在父窗口的 OnCreate 处理函数早期(如果你是子类的控件)。设置控件的风格。

 

下面介绍在对话框中添加 Tab Control 控件,工程不一定是要 MFC 基于对话框形式,单文档视图类派生自 CFormView 的工程也行,或是任何工程弹出的对话框 ( 如登录界面等等 ) 都行 ( 个人没有都去实验,但理论上应该可以,看了下面就知道。 )

1.        首先在对话框资源上添加一个 Tab Control 控件 ID 为 IDC_LOGIN_TAB ,根据需要修改其属性,然后为其添加成员变量 m_tab ,类型为 CTabCtrl

2.        需要几个选项卡,则在对话框资源添加几个对话框,在这里我添加两个选项卡,则要添加两个对话框,其 ID 分别为 IDD_TAB1_DIALOG , IDD_TAB2_DIALOG 它们的属性 style 为 Child, Border 为 None ,其他的再根据自己调整。然后分别为其添加对应的基于 CDialog 类 CLoginTab1 , CLoginTab2

3.        在主对话框添加子对话框头文件,然后再添加三个成员变量,为子对话框添加实例。在 OnInitDialog() 函数初始化 Tab Control 控件显示,若对话框没有OnInitDialog() 函数,则重载这个函数,具体如下:

[cpp] view plain copy

1. //主对话框头文件  

2. class LoginUser : public CDialog  

3. {•••  

4. protected:  

5.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持  

6.     virtual BOOL OnInitDialog();            //没有这个,则在这里重载  

7.     DECLARE_MESSAGE_MAP()  

8. public:  

9.     CTabCtrl m_tab;  

10.     CLoginTab1 m_login_tab1;  

11.     CLoginTab2 m_login_tab2;  

12. •••  

13. }  

 

[cpp] view plain copy

1. //主对话框实现文件  

2. BOOL LoginUser::OnInitDialog()  

3. {  

4.     CDialog::OnInitDialog();  

5.     m_tab.InsertItem(0,_T("系统登录"));  

6.     m_tab.InsertItem(1,_T("服务器设置"));  

7.     m_login_tab1.Create(IDD_TAB1_DIALOG,GetDlgItem(IDC_LOGIN_TAB));  

8.     m_login_tab2.Create(IDD_TAB2_DIALOG,GetDlgItem(IDC_LOGIN_TAB));  

9.     //获得IDC_TABTEST客户区大小  

10. CRect rs;  

11. m_tab.GetClientRect(&rs);  

12. //调整子对话框在父窗口中的位置,根据实际修改  

13. rs.top+=25;  

14. rs.bottom-=60;  

15. rs.left+=1;  

16. rs.right-=10;  

17.       

18. //设置子对话框尺寸并移动到指定位置  

19. m_login_tab1.MoveWindow(&rs);  

20. m_login_tab2.MoveWindow(&rs);  

21. //分别设置隐藏和显示  

22. m_login_tab1.ShowWindow(true);  

23. m_login_tab2.ShowWindow(false);  

24.       

25. //设置默认的选项卡  

26. m_tab.SetCurSel(0);  

27. return TRUE;  

28. }  

4.        响应选项卡切换事件消息,右键 Tab Control 控件,添加事件处理程序,选择 TCN_SELCHANGE 事件,在弹出的编辑区域,填入以下代码:

[cpp] view plain copy

1. void LoginUser::OnTcnSelchangeLoginTab(NMHDR *pNMHDR, LRESULT *pResult)  

2. {  

3.     // TODO: 在此添加控件通知处理程序代码  

4.     int CurSel = m_tab.GetCurSel();  

5.     switch(CurSel)  

6.     {  

7.     case 0:  

8.         m_login_tab1.ShowWindow(true);  

9.         m_login_tab2.ShowWindow(false);  

10.         break;  

11.     case 1:  

12.         m_login_tab1.ShowWindow(false);  

13.         m_login_tab2.ShowWindow(true);  

14.         break;  

15.     default: ;  

16.     }  

17.     *pResult = 0;  

18. }  


接下来,来看看非模态对话框的实现,因为Tab Control控件要在对话框的OnInitDialog () 函数初始化,而MSDN上说:

After the dialog box and all of its controls are created but just before the dialog box (of either type) appears on the screen, the dialog object's OnInitDialog member function is called. For a modal dialog box, this occurs during the DoModal call. For a modeless dialog box, OnInitDialogis called when Create is called. You typically override OnInitDialog to initialize the dialog box's controls, such as setting the initial text of an edit box. You must call the OnInitDialog member function of the base class, CDialog , from your OnInitDialog override.

非模态对话框是在Create的时候一起调用初始化函数的,OnInitDialog()我们仍可以重载这个函数,来实现以上功能。对于单文档/多文档可以看前面的MSDN说明,这里不再详细写。本文若有错误,请指出。

以上就介绍了VC/MFC的学习,希望对VC/MFC有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言VC/MFC频道!

本文由 @小职 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程