如何在基于MFC对话框的应用程序中使用CTabCtrl?

如何在基于MFC对话框的应用程序中使用CTabCtrl?

问题描述:

我需要做一些我希望很简单的事情-创建一个具有2个选项卡的选项卡控件,这意味着我的应用程序有2种操作模式.用户单击Tab1时,将显示一些按钮和文本框,而单击Tab2时,将显示其他输入方法.我注意到在MFC中使用了CTabCtrl类来添加标签. 但是,一旦使用UI设计器添加了选项卡ctrl,就无法指定将使用属性窗口指定多少个选项卡.在网上搜索时,我发现了一些示例,但所有示例都需要您从CtabCtrl中派生,创建2个或更多 child 对话框等并编写自己的自定义类.我的问题是,由于我想做一些基本的事情,为什么我不能使用熟悉的添加事件"处理程序/添加成员变量"向导来完成此操作,然后再处理应用程序类中的所有其他内容?当然,默认的CTabCtrl类 可以做一些有用的事情而无需从中继承吗?

I need to do something which i expected to be was simple - create a tab control which has 2 tabs, implying 2 modes of operation for my app. When user clicks on Tab1, he'll be presented with some buttons and textboxes, and when he clicks Tab2, some other input method. I noticed that there was a CTabCtrl class thats used in MFC to add tabs. However, once I added the tab ctrl using the UI designer, I couldn't specify how many tabs there'll be using property window. Searching on the net, I found some examples but all of them required you to derive from CtabCtrl , create 2 or more child dialogs etc and to write your own custom class. My question is, since I want to do something so basic, why couldn't I do it using the familiar Add Event handler/Add member variable wizard and then handle everything else inside my app's class ? Surely, the default CTabCtrl class can do something useful without needing to derive from it ?

MFC选项卡控件是win32选项卡控件的一个非常薄的包装器,该控件的工作方式与您所描述的差不多.它是一个窗口,可使用选项卡在子窗口之间进行切换.碰巧的是,在直接win32中,这是最有效的工作方式.如果要执行比在各个窗口之间切换要复杂的操作,则可以使用子对话框来执行此操作. MFC并不能为您提供很多帮助,但是从CTabCtrl派生并使用子对话框确实不是一件很难的事,尽管如果您习惯于WinForms进行选项卡控件的方式,那确实没必要.

The MFC tab control is a pretty thin wrapper over the win32 tab control, which works in pretty much the way you describe. It is a window, which provides switching between child windows using tabs. As it happens, in straight win32 this is the most useful way for it to work. If you want to do something more sophisticated than switching between individual windows, you do this by using child dialogs. MFC doesn't do a great deal to help you, but deriving from CTabCtrl and using child dialogs is really not very difficult to do, although if you're used to the way WinForms does tab controls it does seem unnecessary.

如果您希望选项卡控件位于对话框的根部,而对话框旁边没有其他控件,则可能需要查看CPropertySheet(

If you want the tab control at the root of the dialog, with no other controls along side it, you might want to look at CPropertySheet (http://msdn.microsoft.com/en-us/library/d3fkt014(VS.80).aspx) which is probably simpler to use. Unless you want to use any of the wizard functionality you don't even need to derive from it - you just create a couple of child dialog classes, then in the place where you want to create the property sheet, make an object, add the pages to it and invoke it.