使用 CAdvancedComponent 对 CButton、CEdit 和 CDialog 进行颜色设置
使用 CAdvancedComponent 更改 CButton、CEdit 和 CDialog 的背景、前景和其他颜色。
引言
CAdvancedButton
、CAdvancedEdit
和 CAdvancedDialog
分别是分别从 CButton
、CEdit
和 CDialog
派生的 MFC 控制。
这些类实现了 CAdvancedComponent
虚拟类,并提供更改背景和前景颜色的函数。
Using the Code
要将 CAdvancedComponent
类集成到您的应用程序中,请按照以下步骤操作
使用 CAdvancedButton
- 将 AdvancedComponent.h、AdvancedButton.h 和 AdvancedButton.cpp 添加到您的项目中。
- 在对话框类的头文件中包含 "AdvancedButton.h"。
- 在对话框类中声明按钮。例如
- 在对话框类的
OnInitDialog
函数中初始化按钮 - 最终更改绘制方式
- 最终更改颜色
CAdvancedButton adBtn1;
adBtn1.SubclassDlgItem(IDC_BUTTON1, this, RED, GREEN);
adBtn1.SetDrawType(DRAW_ELLIPSE); // or DRAW_RECT
adBtn1.SetColors(RED);
adBtn1.SetBackGround(YELLOW);
adBtn1.SetForeGround(BLUE);
使用 CAdvancedEdit
- 将 AdvancedComponent.h、AdvancedEdit.h 和 AdvancedEdit.cpp 添加到您的项目中。
- 在对话框类的头文件中包含 "AdvancedEdit.h"。
- 在对话框类中声明编辑框。例如
- 在对话框类的
OnInitDialog
函数中初始化编辑框 - 更改颜色
CAdvancedEdit adEdit1;
adEdit1.SubclassDlgItem(IDC_EDIT1, this);
adEdit1.SetBackGround(YELLOW);
adEdit1.SetForeGround(BLUE);
adEdit1.SetTextBackGround(RED);
使用 CAdvancedDialog
- 将 AdvancedComponent.h、AdvancedDialog.h 和 AdvancedDialog.cpp 添加到您的项目中。
- 在对话框类的头文件中包含 "AdvancedDialog.h"。
- 在对话框类的头文件中,将基类从
CDialog
替换为CAdvancedDialog
。 - 在 .cpp 文件中,在构造函数中,将
CDialog
替换为CAdvancedDialog
。 - 在 .cpp 文件中,将
CDialog::DoDataExchange
替换为CAdvancedDialog::DoDataExchange
。 - 如果存在,对
OnInitDialog
和其他继承函数执行相同的操作。 - 在 .cpp 文件中,将 "
BEGIN_MESSAGE_MAP(CTest, CDialog)
" 替换为 "BEGIN_MESSAGE_MAP(CTest, CAdvancedDialog)
"。 - 使用
CAdvancedDialog
的函数。
如果您希望 CAdvancedDialog
的子窗口自动更改,请使用指向主对话框的指针作为第一个参数创建子对话框
class CMyDialogClass : public CAdvancedDialog {
...
};
CMyDialogClass myDialogBow(this);
myDialogBow.DoModal();
UML 类图
历史
- 2008 年 11 月:首次在 CodeProject 上发布。