如何将属性表添加到对话框中 [英] how to add property sheet to the dialog box

查看:104
本文介绍了如何将属性表添加到对话框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话框。

我想将属性表添加到该对话框(不是无模式)。

请帮助我!

I have a dialog box.
I want to add property sheet to that dialog box(not modeless).
Please help me!

推荐答案

属性表提供了一个非常好的用户界面,它允许将几个对话框模板集成在一起,并且用户可以通过使用选项卡控件在它们之间切换。如果需要在单个对话框模板中包含许多常用控件,这将非常有用。

由于属性表与对话框非常相似,我们可以创建对话框然后将其更改为

属性表。以这种方式创建属性表的原因是因为当前Developer Studio确实不支持
不支持直接实现属性表。

在MFC中,有两个类应该用于实现属性表:CPropertySheet和

CPropertyPage。前一类用于创建一个包含制表符控件的框架窗口,第二个用于实现每个页面的
类。

要实现一个属性表,我们先来需要从CPropertySheet派生一个类,然后在其中声明一个

或更多CPropertyPage类型的成员变量。每个变量都将与一个对话框

模板相关联。由于CPropertyPage派生自类CDialog,因此可以在CPropertyPage的成员函数中访问

CDialog的所有公共成员和受保护成员。





示例6.1-1\DB演示了如何基于属性表创建应用程序。首先使用应用程序向导生成

作为基于对话框的应用程序(默认类为CDBApp和CDBDlg),然后

CDBDlg的基类从CDialog更改为CPropertySheet的。由于CPropertySheet类确实没有成员IDD来存储对话框模板ID,我们需要从类中删除以下行

CDBDlg:

第6章对话框

144

enum {IDD = IDD_DIALOG_DB};

将不使用默认对话框模板IDD_DIALOG_DB,因此它也会从

应用程序资源中删除。以下是修改后的类:

class CDBDlg:public CPropertySheet

{

public:

CDBDlg(CWnd * pParent = NULL);

// {{AFX_DATA(CDBDlg)

//}} AFX_DATA

// {{AFX_VIRTUAL(CDBDlg )

受保护:

虚拟空虚DoDataExchange(CDataExchange * pDX);

//}} AFX_VIRTUAL

protected :

HICON m_hIcon;

// {{AFX_MSG(CDBDlg)

虚拟BOOL OnInitDialog();

afx_msg void OnSysCommand(UINT nID,LPARAM lParam);

afx_msg void OnPaint();

afx_msg HCURSOR OnQueryDragIcon();

//} } AFX_MSG

DECLARE_MESSAGE_MAP()

};

我们还需要在CDBDlg的实现文件中找到所有关键字CDialog并将其更改为

CPropertySheet。这些更改应该在以下函数中发生:CDBDlg的构造函数,

函数DoDataExchange(...),OnInitDialog(),OnSysCommand(...),OnPaint(...)和消息映射

宏。

接下来我们需要创建每个页面。创建属性页面的过程与

相同,创建一个对话框,除了为对话框模板添加新类时,我们必须从

类派生它的CPropertyPage。在该示例中,将三个对话框模板添加到应用程序中,它们的ID分别为

ID_DIALOG_PAGE1,ID_DIALOG_PAGE2和ID_DIALOG_PAGE3。 CPage1,CPage2和

CPage3三个类也通过使用类向导添加,它们都是从CPropertyPage派生的。当

这样做时,我们需要提供相应对话框模板的ID。

在类CDBDlg中,为每个页面声明一个新的成员变量:

#includePage.h

......

class CDBDlg:public CPropertySheet

{

......

受保护:

......

CPage1 m_page1;

CPage2 m_page2;

CPage3 m_page3;

......

};

通过调用函数

CPropertySheet :: AddPage(...)。以下是样本中每个页面的添加方式:

CDBDlg :: CDBDlg(CWnd * pParent / * = NULL * /):CPropertySheet()

{

// {{AFX_DATA_INIT(CDBDlg)

//}} AFX_DATA_INIT

m_hIcon = AfxGetApp() - > LoadIcon(IDR_MAINFRAME);

AddPage(&m_page1);

AddPage(&m_page2);

AddPage(&m_page3);

}

函数CPropertySheet :: AddPage(...)只有一个参数,它是指向CPropertyPage类型的指针

对象。

这些是实现属性的必要步骤片。对于每个属性页面,我们还可以为控件添加消息处理程序,其过程与独立对话框的过程相同

框。

默认情况下,属性表将以tab模式实现:

属性表中将有一个选项卡控件,可用于选择属性页。属性表也可以在

向导模式下实现,在这种情况下,选项卡控件将被两个按钮替换(标有Previous和

Next )。在这种模式下,页面只能通过按钮点击顺序选择。

要启用向导模式,我们需要做的就是调用函数CPropertySheet ::

SetWizardMode()添加完所有页面后。例如,如果我们想在示例中启用向导模式,我们应该按如下方式实现CDBDlg的构造函数:

CDBDlg :: CDBDlg(CWnd * pParent / * = NULL * /):CPropertySheet()

{

// {{AFX_DATA_INIT(CDBDlg)

//}} AFX_DATA_INIT
m_hIcon = AfxGetApp() - > LoadIcon(IDR_MAINFRAME);

AddPage(&m_page1);

AddPage(&m_page2);

AddPage(&m_page3);

SetWizardMode();

}

样本6.2-2 \DB与样本6.2-相同1 \DB,但属性表以

向导模式实现。

如果我们需要在SDI或MDI应用程序中实现属性表对话框,大多数步骤

仍然是一样的。我们可以先创建一个新的基于CPropertySheet的类,然后添加对话框

模板和基于CPropertyPage的类,用它们在CPropertySheet中声明新变量

派生类,调用函数CPropertySheet ::在其构造函数中添加AddPage(...)。我们可以随时调用函数

CPropertySheet :: DoModal()来调用属性表。
Property sheet provides a very nice user interface, it allows several dialog templates to be integrated
together, and the user can switch among them by using tab control. This is especially useful if there are
many common controls that need to be included in a single dialog template.
Because property sheet is very similar to dialog box, we can create a dialog box then change it to
property sheet. The reason for creating property sheet this way is because currently Developer Studio does
not support direct implementation of property sheet.
In MFC, there are two classes that should be used to implement property sheet: CPropertySheet and
CPropertyPage. The former class is used to create a frame window that contains tab control, the second
class is used to implement each single page.
To implement a property sheet, we first need to derive a class from CPropertySheet, then declare one
or more CPropertyPage type member variables within it. Each variable will be associated with a dialog
template. Because CPropertyPage is derived from class CDialog, all the public and protected members of
CDialog are accessible in the member functions of CPropertyPage.


Sample 6.1-1\DB demonstrates how to create application based on property sheet. First it is generated
as a dialog based application by using Application Wizard (the default classes are CDBApp and CDBDlg), then
the base class of CDBDlg is changed from CDialog to CPropertySheet. Since class CPropertySheet does
not have member IDD to store the dialog template ID, we need to delete the following line from class
CDBDlg:
Chapter 6. Dialog Box
144
enum { IDD = IDD_DIALOG_DB };
The default dialog box template IDD_DIALOG_DB will not be used, so it is also deleted from the
application resources. The following is the modified class:
class CDBDlg : public CPropertySheet
{
public:
CDBDlg(CWnd* pParent = NULL);
//{{AFX_DATA(CDBDlg)
//}}AFX_DATA
//{{AFX_VIRTUAL(CDBDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);
//}}AFX_VIRTUAL
protected:
HICON m_hIcon;
//{{AFX_MSG(CDBDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
We also need to find all the keyword CDialog in the implementation file of CDBDlg and change them to
CPropertySheet. The changes should happen in the following functions: the constructor of CDBDlg,
function DoDataExchange(…), OnInitDialog(), OnSysCommand(…), OnPaint(…), and message mapping
macros.
Next we need to create each single page. The procedure of creating a property page is the same with
creating a dialog box, except that when adding new class for a dialog box template, we must derive it from
class CPropertyPage. In the sample, three dialog templates are added to the application, their IDs are
ID_DIALOG_PAGE1, ID_DIALOG_PAGE2 and ID_DIALOG_PAGE3 respectively. Three classes CPage1, CPage2 and
CPage3 are also added through using Class Wizard, which are all derived from CPropertyPage. When
doing this, we need to provide the ID of the corresponding dialog template.
In class CDBDlg, a new member variable is declared for each page:
#include "Page.h"
……
class CDBDlg : public CPropertySheet
{
……
protected:
……
CPage1 m_page1;
CPage2 m_page2;
CPage3 m_page3;
……
};
The pages should be added in the constructor of CPropertySheet by calling function
CPropertySheet::AddPage(…). The following is how each page is added in the sample:
CDBDlg::CDBDlg(CWnd* pParent /*=NULL*/) : CPropertySheet()
{
//{{AFX_DATA_INIT(CDBDlg)
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
AddPage(&m_page1);
AddPage(&m_page2);
AddPage(&m_page3);
}
Function CPropertySheet::AddPage(…) has only one parameter, it is a pointer to CPropertyPage type
object.
These are the necessary steps for implementing property sheet. For each property page, we can also
add message handlers for the controls, the procedure of which is the same with that of a standalone dialog
box.
By default, the property sheet will be implemented in "tab" mode: there will be a tab control in the
property sheet, which can be used to select property pages. The property sheet can also be implemented in
"wizard" mode, in which case tab control will be replaced by two buttons (labeled with "Previous" and
"Next"). In this mode, the pages can only be selected sequentially through button clickings.
To enable wizard mode, all we need to do is calling function CPropertySheet::
SetWizardMode()after all the pages have been added. For example, if we want to enable wizard mode in
the sample, we should implement the constructor of CDBDlg as follows:
CDBDlg::CDBDlg(CWnd* pParent /*=NULL*/) : CPropertySheet()
{
//{{AFX_DATA_INIT(CDBDlg)
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
AddPage(&m_page1);
AddPage(&m_page2);
AddPage(&m_page3);
SetWizardMode();
}
Sample 6.2-2\DB is the same with sample 6.2-1\DB, except that the property sheet is implemented in
wizard mode.
If we need to implement a property sheet dialog box in an SDI or MDI application, most of the steps
are still the same. We can start by creating a new CPropertySheet based class, then adding dialog
templates and CPropertyPage based classes, using them to declare new variables in CPropertySheet
derived class, calling function CPropertySheet::AddPage(…) in its constructor. We can call function
CPropertySheet::DoModal() at anytime to invoke the property sheet.


这篇关于如何将属性表添加到对话框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆