65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 CAdvancedComponent 对 CButton、CEdit 和 CDialog 进行颜色设置

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.75/5 (8投票s)

2008年11月4日

CPOL

1分钟阅读

viewsIcon

37477

downloadIcon

2226

使用 CAdvancedComponent 更改 CButton、CEdit 和 CDialog 的背景、前景和其他颜色。

Sample Image 1

引言

CAdvancedButtonCAdvancedEditCAdvancedDialog 分别是分别从 CButtonCEditCDialog 派生的 MFC 控制。

这些类实现了 CAdvancedComponent 虚拟类,并提供更改背景和前景颜色的函数。

Using the Code

要将 CAdvancedComponent 类集成到您的应用程序中,请按照以下步骤操作

使用 CAdvancedButton

  1. AdvancedComponent.hAdvancedButton.hAdvancedButton.cpp 添加到您的项目中。
  2. 在对话框类的头文件中包含 "AdvancedButton.h"。
  3. 在对话框类中声明按钮。例如
  4. CAdvancedButton adBtn1;
  5. 在对话框类的 OnInitDialog 函数中初始化按钮
  6. adBtn1.SubclassDlgItem(IDC_BUTTON1, this, RED, GREEN);
  7. 最终更改绘制方式
  8. adBtn1.SetDrawType(DRAW_ELLIPSE); // or DRAW_RECT
  9. 最终更改颜色
  10. adBtn1.SetColors(RED);
    adBtn1.SetBackGround(YELLOW);
    adBtn1.SetForeGround(BLUE);

使用 CAdvancedEdit

  1. AdvancedComponent.hAdvancedEdit.hAdvancedEdit.cpp 添加到您的项目中。
  2. 在对话框类的头文件中包含 "AdvancedEdit.h"。
  3. 在对话框类中声明编辑框。例如
  4. CAdvancedEdit adEdit1;
  5. 在对话框类的 OnInitDialog 函数中初始化编辑框
  6. adEdit1.SubclassDlgItem(IDC_EDIT1, this);
  7. 更改颜色
  8. adEdit1.SetBackGround(YELLOW);
    adEdit1.SetForeGround(BLUE);
    adEdit1.SetTextBackGround(RED);

使用 CAdvancedDialog

  1. AdvancedComponent.hAdvancedDialog.hAdvancedDialog.cpp 添加到您的项目中。
  2. 在对话框类的头文件中包含 "AdvancedDialog.h"。
  3. 在对话框类的头文件中,将基类从 CDialog 替换为 CAdvancedDialog
  4. .cpp 文件中,在构造函数中,将 CDialog 替换为 CAdvancedDialog
  5. .cpp 文件中,将 CDialog::DoDataExchange 替换为 CAdvancedDialog::DoDataExchange
  6. 如果存在,对 OnInitDialog 和其他继承函数执行相同的操作。
  7. .cpp 文件中,将 "BEGIN_MESSAGE_MAP(CTest, CDialog)" 替换为 "BEGIN_MESSAGE_MAP(CTest, CAdvancedDialog)"。
  8. 使用 CAdvancedDialog 的函数。

如果您希望 CAdvancedDialog 的子窗口自动更改,请使用指向主对话框的指针作为第一个参数创建子对话框

class CMyDialogClass : public CAdvancedDialog {
    ...
};

CMyDialogClass myDialogBow(this);
myDialogBow.DoModal();

UML 类图

UML Diagram

历史

  • 2008 年 11 月:首次在 CodeProject 上发布。
© . All rights reserved.