MFC - 屬性表



屬性表,也稱為選項卡對話方塊,是一個包含屬性頁的對話方塊。每個屬性頁都基於一個對話方塊模板資源,幷包含控制元件。它包含在一個頁面中,頂部有一個選項卡。選項卡命名頁面並指示其用途。使用者可以在屬性表中單擊一個選項卡以選擇一組控制元件。

要建立屬性頁,讓我們透過建立一個基於對話方塊的 MFC 專案來檢視一個簡單的示例。

MFC Project

建立專案後,我們需要新增一些屬性頁。

Visual Studio 透過顯示“新增資源”對話方塊、展開“對話方塊”節點並選擇其中一個 IDD_PROPPAGE_X 項,使建立屬性頁的資源變得容易。

步驟 1 - 在解決方案資源管理器中右鍵單擊您的專案,然後選擇“新增”→“資源”。

IDD Propage Larg

步驟 2 - 選擇 IDD_PROPPAGE_LARGE 並單擊“新建”。

IDD Propage Larg New

步驟 3 - 讓我們將此屬性頁的 ID 和標題分別更改為IDD_PROPPAGE_1屬性頁 1,如上所示。

步驟 4 - 在設計器視窗中右鍵單擊屬性頁。

Propage in Designer Window

步驟 5 - 選擇“新增類”選項。

Propage Add Class Option

步驟 6 - 輸入類名,並從基類下拉列表中選擇 CPropertyPage。

步驟 7 - 單擊“完成”繼續。

步驟 8 - 透過遵循上述步驟,再新增一個 ID 為 IDD_PROPPAGE_2、標題為屬性頁 2 的屬性頁。

步驟 9 - 您現在可以看到建立了兩個屬性頁。要實現其功能,我們需要一個屬性表。

屬性表將屬性頁組合在一起並將其作為實體。

要建立屬性表,請按照以下步驟操作 -

步驟 1 - 右鍵單擊您的專案,然後選擇“新增”>“類”選單選項。

Create Property Sheet

步驟 2 - 從左側窗格中選擇“Visual C++”→“MFC”,從模板窗格中選擇“MFC 類”,然後單擊“新增”。

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
廣告