English 中文(简体)
MFC - Property Sheets
  • 时间:2024-12-22

MFC - Property Sheets


Previous Page Next Page  

A property sheet, also known as a tab dialog box, is a dialog box that contains property pages. Each property page is based on a dialog template resource and contains controls. It is enclosed on a page with a tab on top. The tab names the page and indicates its purpose. Users cpck a tab in the property sheet to select a set of controls.

To create property pages, let us look into a simple example by creating a dialog based MFC project.

MFC Project

Once the project is created, we need to add some property pages.

Visual Studio makes it easy to create resources for property pages by displaying the Add Resource dialog box, expanding the Dialog node and selecting one of the IDD_PROPPAGE_X items.

Step 1 − Right-cpck on your project in solution explorer and select Add → Resources.

IDD Propage Larg

Step 2 − Select the IDD_PROPPAGE_LARGE and cpck NEW.

IDD Propage Larg New

Step 3 − Let us change ID and Caption of this property page to IDD_PROPPAGE_1 and Property Page 1 respectively as shown above.

Step 4 − Right-cpck on the property page in designer window.

Propage in Designer Window

Step 5 − Select the Add Class option.

Propage Add Class Option

Step 6 − Enter the class name and select CPropertyPage from base class dropdown pst.

Step 7 − Cpck Finish to continue.

Step 8 − Add one more property page with ID IDD_PROPPAGE_2 and Caption Property Page 2 by following the above mentioned steps.

Step 9 − You can now see two property pages created. To implement its functionapty, we need a property sheet.

The Property Sheet groups the property pages together and keeps it as entity.

To create a property sheet, follow the steps given below −

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

Create Property Sheet

Step 2 − Select Visual C++ → MFC from the left pane and MFC Class in the template pane and cpck Add.

MFC Class in Template Pane

Step 3 − Enter the class name and select CPropertySheet from base class dropdown pst.

Step 4 − Cpck finish to continue.

Step 5 − To launch this property sheet, we need the following changes in our main project class.

Step 6 − Add the following references in CMFCPropSheetDemo.cpp file.

#include "MySheet.h"
#include "PropPage1.h"
#include "PropPage2.h"

Step 7 − Modify the CMFCPropSheetDemoApp::InitInstance() method as shown in the following code.

CMySheet mySheet(L"Property Sheet Demo");
CPropPage1 page1;
CPropPage2 page2;

mySheet.AddPage(&page1);
mySheet.AddPage(&page2);

m_pMainWnd = &mySheet;
INT_PTR nResponse = mySheet.DoModal();

Step 8 − Here is the complete implementation of CMFCPropSheetDemo.cpp file.


// MFCPropSheetDemo.cpp : Defines the class behaviors for the apppcation.
//
#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 initiapzation in InitInstance
}


// The one and only CMFCPropSheetDemoApp object

CMFCPropSheetDemoApp theApp;


// CMFCPropSheetDemoApp initiapzation

BOOL CMFCPropSheetDemoApp::InitInstance() {
   
   // InitCommonControlsEx() is required on Windows XP if an apppcation
   // 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 apppcation.
   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 pst view controls.
   CShellManager *pShellManager = new CShellManager;

   // Activate "Windows Native" visual manager for enabpng themes in MFC controls
   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
   // Standard initiapzation
   // 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 initiapzation 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 Apppcations"));
   
   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 apppcation is terminating unexpectedly.
");
      TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, 
        you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.
");
   }

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

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

Step 9 − When the above code is compiled and executed, you will see the following dialog box. This dialog box contains two property pages.

Property Pages Advertisements