- MFC教程
- MFC-首页
- MFC - 概述
- MFC - 环境设置
- MFC - VC++ 项目
- MFC - 入门
- MFC - Windows 基础知识
- MFC - 对话框
- MFC - Windows 资源
- MFC - 属性表
- MFC - 窗口布局
- MFC-控制管理
- MFC-Windows 控件
- MFC - 消息和事件
- MFC-Activex 控件
- MFC-文件系统
- MFC——标准I/O
- MFC - 文档视图
- MFC-字符串
- MFC-卡雷
- MFC - 链接列表
- MFC - 数据库类
- MFC-序列化
- MFC——多线程
- MFC-- 互联网编程
- MFC-GDI
- MFC - 库
- MFC 有用资源
- MFC - 快速指南
- MFC - 有用的资源
- MFC - 讨论
MFC - 属性表
属性表,也称为选项卡对话框,是包含属性页的对话框。每个属性页都基于对话框模板资源并包含控件。它包含在一个页面上,顶部有一个选项卡。该选项卡为页面命名并指示其用途。用户单击属性表中的选项卡来选择一组控件。
为了创建属性页,让我们通过创建基于对话框的 MFC 项目来研究一个简单的示例。
创建项目后,我们需要添加一些属性页。
Visual Studio 通过显示“添加资源”对话框、展开“对话框”节点并选择 IDD_PROPPAGE_X 项之一,可以轻松地为属性页创建资源。
步骤 1 - 在解决方案资源管理器中右键单击您的项目,然后选择添加 → 资源。
步骤 2 - 选择 IDD_PROPPAGE_LARGE 并单击新建。
步骤 3 - 让我们将该属性页的 ID 和标题分别更改为IDD_PROPPAGE_1和属性页 1,如上所示。
步骤 4 - 右键单击设计器窗口中的属性页。
步骤 5 - 选择添加类别选项。
步骤 6 - 输入类名称并从基类下拉列表中选择 CPropertyPage。
步骤 7 - 单击“完成”继续。
步骤 8 - 按照上述步骤添加一个 ID IDD_PROPPAGE_2 和标题属性页 2 的属性页。
步骤 9 - 您现在可以看到创建的两个属性页。为了实现其功能,我们需要一个属性表。
属性表将属性页组合在一起并将其作为实体。
要创建属性表,请按照以下步骤操作 -
步骤 1 - 右键单击您的项目并选择“添加”>“类”菜单选项。
步骤 2 - 从左侧窗格中选择 Visual C++ → 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 - 编译并执行上述代码后,您将看到以下对话框。该对话框包含两个属性页。
