对话框标题






4.85/5 (26投票s)
2002年7月23日
2分钟阅读

438436

4747
为任何对话框或属性表添加标题。
引言
如果您想给用户一些反馈,并使您的应用程序看起来更专业,只需使用这些简单的类。 这段代码的灵感来自 Mustafa Demirhan 关于 破解 CPropertySheet 的文章。
CDialogHeader
类将向您的对话框添加一个标题区域,该区域将显示一个图标、一个标题和一些描述文本。
下面是添加了标题控件的标准对话框的图片。
标题可以显示图标、标题和描述文本,并具有自定义字体大小、对齐方式和背景颜色。
CMyPropertySheet
类将对话框标题合并到属性表控件中。
下面是带有内置标题控件的属性表控件的两个选项卡。 每个选项卡都可以有自己的图标和文本。
如何使用 CDialogHeader 类
- 在您的项目中包含文件
DialogHeader.h
和DialogHeader.cpp
。 - 将 include 语句添加到您的对话框头文件
#include 'DialogHeader.h' //For the header ctrl area
- 向您的对话框类添加一个
CDialogHeader
类型的成员变量CDialogHeader m_HeaderCtrl; //Dialogheader
在您的对话框的
OnInit
函数中执行以下操作- 设置标题的显示数据
m_HeaderCtrl.SetHeaderData(hIcon,"Title","Description");
或者,您可以使用以下方法单独设置/更改每个成员
m_HeaderCtrl.SetIconHandle(hIcon); m_HeaderCtrl.SetTitleText("Title"); m_HeaderCtrl.SetDescText("Description.");
- 上面的函数是最常用的,但是您可以设置以下所有选项
字体大小
m_HeaderCtrl.SetFontSizes(TitleSize,DescSize);
文本对齐方式
m_HeaderCtrl.SetTextAlign(DT_LEFT); //选项为 {DT_LEFT,DT_CENTER,DT_RIGHT}
背景颜色
m_HeaderCtrl.SetBackgroundColor(RGB(255,255,255));
空白图标模式
m_HeaderCtrl.SetBlankIcon(TRUE); //TRUE=始终为图标分配空间
图标偏移量
m_HeaderCtrl.SetIconOffset(5); //定义图标和边框的左上角
标题偏移量
m_HeaderCtrl.SetTitleOffset(10); //定义图标和标题文本之间的空间
描述偏移量
m_HeaderCtrl.SetDescOffset(10); //定义当对齐方式为 DT_LEFT 或 DT_RIGHT 时描述文本的缩进量
标题高度
m_HeaderCtrl.SetHeaderHeight(x); //如果 x=0,自动设置高度
注意:必须先设置字体大小和偏移量,然后才能使用 x=0 设置标题高度。 有关所有函数的更完整描述,请参阅头文件。
- 通过调用以下命令显示标题并调整对话框窗口的大小
m_HeaderCtrl.Init(this);
- 通过调用以下命令将对话框控件向下移动以适应标题区域
m_HeaderCtrl.MoveCtrls(this);
- 设置标题的显示数据
就是这样! 下面的代码显示了一个典型的示例,说明如何初始化和显示对话框标题
//Example of using CDialogHeader BOOL CDlgFileTypes::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here //Set initial header data m_HeaderCtrl.SetTitleText("Title"); m_HeaderCtrl.SetDescText("Description text."); m_HeaderCtrl.SetIconHandle(AfxGetApp()->LoadIcon(IDI_HARDDRIVE)); //Create the header ctrl m_HeaderCtrl.Init(this); //Move all dialog ctrls down to accommodate header area m_HeaderCtrl.MoveCtrls(this); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
如何使用 MyPropertySheet 类
- 在您的项目中包含文件
DialogHeader.h
、DialogHeader.cpp
、MyPropertySheet.h
和MyPropertySheet.cpp
。 - 在每个要替换标准
CPropertySheet
的头文件中添加 include 语句#include "MyPropertySheet.h" //For the custom Propertysheet ctrl
- 添加一个新的
CMyPropertySheet
成员变量/局部变量或将CPropertySheet
替换为CMyPropertySheet
。 - 使用
PSHEETITEM
变量将属性页添加到属性表。
注意:有关所有函数和选项的更完整描述,请参阅头文件。
下面的示例创建了一个属性表,其中包含两个选项卡,每个选项卡都具有不同的图标、文本和布局
void CDialogHeaderDemoDlg::OnButton1() { //Variables CMyPropertySheet sheet("Options"); PropPage1 page1; CPropPage2 page2; PSHEETITEM pItem; //Get a default header item pItem=sheet.GetDefaultPSItem; //Add tab 1 to sheet pItem.sTitle="Title 1"; pItem.sDesc="Propertypage number 1 options."; pItem.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); sheet.AddPage(&page1,pItem); //Add tab 2 to sheet pItem.sTitle="Title 2"; pItem.sDesc="Propertypage number 2 options."; pItem.hIcon=AfxGetApp()->LoadIcon(IDI_HARDDRIVE); //Change the alignment for tab 2 pItem.uAlign=DT_CENTER; //Add page two to the propertysheet sheet.AddPage(&page2,pItem); //Display the propertysheet ctrl sheet.DoModal(); }
希望您喜欢这段代码并发现它有用。