MFC - 地产表

属性表,也称为选项卡对话框,是一个包含属性页的对话框.每个属性页面都基于对话框模板资源并包含控件.它被封闭在一个页面上,顶部有一个标签.选项卡命名页面并指明其用途.用户单击属性表中的选项卡以选择一组控件.

要创建属性页,让我们通过创建基于对话框的MFC项目来查看一个简单示例.

MFC Project

创建项目后,我们需要添加一些属性页.

通过显示"添加资源"对话框,展开"对话"节点并选择其中一个IDD_PROPPAGE_X项,Visual Studio可以轻松地为属性页创建资源.

第1步 : 在解决方案资源管理器中右键单击您的项目,然后选择Add → 资源.

IDD Propage Larg

第2步 : 选择IDD_PROPPAGE_LARGE并单击NEW.

IDD Propage Larg New

第3步 : 如上所示,我们将此属性页的ID和标题分别更改为 IDD_PROPPAGE_1 属性页.

步骤4 : 在设计器窗口中右键单击属性页.

设计器窗口中的支柱

第5步 : 选择Add Class选项.

Propage Add Class Option

第6步 : 输入班级名称,然后从基类下拉列表中选择CPropertyPage.

第7步 : 单击"完成"继续.

步骤8 : 按照上述步骤添加另一个ID为IDD_PROPPAGE_2和Caption Property Page 2的属性页.

步骤9 : 您现在可以看到创建了两个属性页.为了实现其功能,我们需要一个属性表.

属性表将属性页组合在一起并将其保持为实体.

创建一个属性表,按照下面给出的步骤和减号;

步骤1 : 右键单击您的项目,然后选择添加>类菜单选项.

创建属性表

步骤2 : 选择Visual C ++ → 左侧窗格中的MFC和模板窗格中的MFC类,然后单击添加.

模板窗格中的MFC类

第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() {

   // support Restart Manager
   m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
   // TODO: add construction code here,
   // Place all significant initialization in InitInstance
}


// The one and only CMFCPropSheetDemoApp object

CMFCPropSheetDemoApp theApp;


// CMFCPropSheetDemoApp initialization

BOOL CMFCPropSheetDemoApp::InitInstance() {
   
   // InitCommonControlsEx() is required on Windows XP if an application
   // manifest specifies use of ComCtl32.dll version 6 or later to enable
   // visual styles. Otherwise, any window creation will fail.
   INITCOMMONCONTROLSEX InitCtrls;
   InitCtrls.dwSize = sizeof(InitCtrls);
   // Set this to include all the common control classes you want to use
   // in your application.
   InitCtrls.dwICC = ICC_WIN95_CLASSES;
   InitCommonControlsEx(&InitCtrls);
   
   CWinApp::InitInstance();
   
   
   AfxEnableControlContainer();
   
   // Create the shell manager, in case the dialog contains
   // any shell tree view or shell list view controls.
   CShellManager *pShellManager = new CShellManager;

   // Activate "Windows Native" visual manager for enabling themes in MFC controls
   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
   // Standard initialization
   // If you are not using these features and wish to reduce the size
   // of your final executable, you should remove from the following
   // the specific initialization routines you do not need
   // Change the registry key under which our settings are stored
   // TODO: You should modify this string to be something appropriate
   // such as the name of your company or organization
   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");
   }

   // Delete the shell manager created above.
   if (pShellManager != NULL) {
      delete pShellManager;
   }

   // Since the dialog has been closed, return FALSE so that we exit the
   // application, rather than start the application's message pump.
   return FALSE;
}

第9步 : 编译并执行上述代码时,您将看到以下对话框.此对话框包含两个属性页.

属性页