英文链接源链接 [12248]
contenteditable="true"

MFC - Property Sheets 介绍

属性表,也称为options卡对话框,是包含属性页的对话框。每个属性页均基于对话框模板资源,并包含控件。它包含在页面上,顶部带有options卡。该options卡为页面命名并指示其用途。用户单击属性表中的options卡以选择一组控件。

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

MFC Project

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

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

步骤1 -在解决方案资源管理器中右键单击您的项目,然后选择添加→资源。

IDD Propage Larg

第2步-选择IDD_PROPPAGE_LARGE,然后单击"新建"。

IDD Propage Larg New

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

第4步-在设计器窗口的属性页上单击鼠标右键。

Propage in Designer Window

步骤5 -选择添加类别options。

Propage Add Class Option

步骤6 -输入类名称,然后从基类下拉列表中选择CPropertyPage。

步骤7 -单击完成以继续。

步骤8 -按照上述步骤,再添加一个ID为IDD_PROPPAGE_2的属性页和Caption属性页2。

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

属性表将属性页分组在一起,并将其保留为实体。

要创建属性表,请遵循以下步骤-

Step 1 − Right-click on your project and select Add > Class menu options.

Create Property Sheet

步骤2 -在左侧窗格中选择Visual C ++→MFC,在模板窗格中选择MFC Class,然后单击添加。

MFC Class in Template Pane

第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 -编译并执行上述代码后,您将看到以下对话框。此对话框包含两个属性页。

Property Pages
点我分享笔记