属性表(Property Sheets),也称为选项卡对话框,是包含属性页的对话框,每个属性页均基于对话框模板资源,并包含控件。它包含在页面上,顶部带有选项卡。该选项卡为页面命名并指示其用途。
要创建属性页,让无涯教程通过创建一个基于对话框的MFC项目来研究一个简单的示例。
创建项目后,需要添加一些属性页。
通过显示"Add Resource"对话框,展开"Dialog"节点并选择IDD_PROPPAGE_X项之一,Visual Studio可以轻松地为属性页创建资源。
步骤1 -在Solution Explorer中右键单击您的项目,然后选择Add→Resource。
步骤2 - 选择IDD_PROPPAGE_LARGE,然后单击"New"。
步骤3 - 将此属性页的ID和标题分别更改为 IDD_PROPPAGE_1 和属性页1 ,如上所示。
步骤4 - 在设计器窗口的属性页上单击鼠标右键。
步骤5 - 选择添加类别选项。
步骤6 - 输入类名称,然后从基类下拉列表中选择CPropertyPage。
步骤7 - 单击完成以继续。
步骤8 - 按照上述步骤,再添加一个ID为IDD_PROPPAGE_2的属性页和Caption属性页2。
步骤9 - 现在,您可以看到创建了两个属性页。要实现其功能,无涯教程需要一个属性表。
要创建属性表,请遵循以下步骤-
步骤1-右键单击您的项目,然后选择Add>Class 菜单选项。
步骤2 - 在左侧窗格中选择Visual C ++→MFC,在模板窗格中选择MFC Class,然后单击添加。
步骤3 - 输入类名称,然后从基类下拉列表中选择CPropertySheet。
步骤4 - 单击完成以继续。
步骤5 - 要启动此属性表,需要在主项目类中进行以下更改。
步骤6 - 在CMFCPropSheetDemo.cpp文件中添加以下引用。
#include "MySheet.h" #include "PropPage1.h" #include "PropPage2.h"
步骤7 - 修改CMFCPropSheetDemoApp::InitInstance()方法,如以下代码所示。
CMySheet mySheet(L"Property Sheet Demo"); CPropPage1 page1; CPropPage2 page2; mySheet.AddPage(&page1); mySheet.AddPage(&page2); m_pMainWnd = &mySheet; INT_PTR nResponse = mySheet.DoModal();
步骤8 - 这是CMFCPropSheetDemo.cpp文件的完整实现。
//MFCPropSheetDemo.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "MFCPropSheetDemo.h" #include "MFCPropSheetDemoDlg.h" #include "MySheet.h" #include "PropPage1.h" #include "PropPage2.h" #ifdef _DEBUG #define new DEBUG_NEW #endif //CMFCPropSheetDemoApp BEGIN_MESSAGE_MAP(CMFCPropSheetDemoApp, CWinApp) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) END_MESSAGE_MAP() //CMFCPropSheetDemoApp construction CMFCPropSheetDemoApp::CMFCPropSheetDemoApp() { //支持重启管理器 m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART; //TODO: add construction code here } //唯一的 CMFCPropSheetDemoApp 对象 CMFCPropSheetDemoApp theApp; //CMFCPropSheetDemoApp 初始化 BOOL CMFCPropSheetDemoApp::InitInstance() { //如果应用程序在 Windows XP 上需要 InitCommonControlsEx() //manifest 指定使用 ComCtl32.dll 版本 6 或更高版本来启用 //视觉风格。否则,任何窗口创建都会失败。 INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); //将此设置为包括您要使用的所有常见控件类 //在您的应用程序中。 InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); CWinApp::InitInstance(); AfxEnableControlContainer(); //创建Shell管理器,以防对话框包含 //任何Shell视图或外壳列表视图控件。 CShellManager *pShellManager = new CShellManager; //激活“Windows Native”可视化管理器以在 MFC 控件中启用主题 CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows)); //标准初始化 //如果您不使用这些功能并希望减小尺寸 //您的最终可执行文件,您应该从以下内容中删除 //您不需要的特定初始化例程 //更改存储我们设置的注册表项 //TODO: You should modify this string to be something appropriate //例如您的公司或组织的名称 SetRegistryKey(_T("Local AppWizard-Generated Applications")); CMySheet mySheet(L"Property Sheet Demo"); CPropPage1 page1; CPropPage2 page2; mySheet.AddPage(&page1); mySheet.AddPage(&page2); m_pMainWnd = &mySheet; INT_PTR nResponse = mySheet.DoModal(); if (nResponse == IDOK) { //TODO: Place code here to handle when the dialog is //dismissed with OK }else if (nResponse == IDCANCEL) { //TODO: Place code here to handle when the dialog is //dismissed with Cancel }else if (nResponse == -1) { TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n"); TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n"); } //删除上面创建的 shell 管理器。 if (pShellManager != NULL) { delete pShellManager; } //由于对话框已经关闭,返回 FALSE 以便我们退出应用程序,而不是启动应用程序的消息泵。 return FALSE; }
步骤9 - 编译并执行上述代码后,您将看到以下对话框。此对话框包含两个属性页。