Color CButton, CEdit, and CDialog using CAdvancedComponent
Using CAdvancedComponent to change the background, foreground, and other colors on CButton, CEdit, and CDialog.
Introduction
CAdvancedButton
, CAdvancedEdit
, and CAdvancedDialog
are MFC controls respectively derived from CButton
, CEdit
, and CDialog
.
These classes implement the CAdvancedComponent
virtual class, and offer functions to change background and foreground colors.
Using the Code
To integrate the CAdvancedComponent
classes into your application, please follow the steps below:
Using the CAdvancedButton
- Add AdvancedComponent.h, AdvancedButton.h, and AdvancedButton.cpp to your project.
- Include "AdvancedButton.h" in the dialog class' header.
- Declare the button in the dialog class. For example:
- Initialize the button in the dialog class'
OnInitDialog
function: - Eventually change the draw form:
- Eventually change the colors:
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);
Using the CAdvancedEdit
- Add AdvancedComponent.h, AdvancedEdit.h, and AdvancedEdit.cpp to your project.
- Include "AdvancedEdit.h" in the dialog class' header.
- Declare the edit in the dialog class. For example:
- Initialize the edit in the dialog class'
OnInitDialog
function: - Change the colors:
CAdvancedEdit adEdit1;
adEdit1.SubclassDlgItem(IDC_EDIT1, this);
adEdit1.SetBackGround(YELLOW);
adEdit1.SetForeGround(BLUE);
adEdit1.SetTextBackGround(RED);
Using the CAdvancedDialog
- Add AdvancedComponent.h, AdvancedDialog.h, and AdvancedDialog.cpp to your project.
- Include "AdvancedDialog.h" in the dialog class' header.
- Replace the base class from
CDialog
toCAdvancedDialog
in your dialog class' header. - In the .cpp file, in the constructor, replace
CDialog
byCAdvancedDialog
. - In the .cpp file, replace
CDialog::DoDataExchange
byCAdvancedDialog::DoDataExchange
. - If they exist, do the same thing for
OnInitDialog
and the other inherited functions. - In the .cpp file, replace "
BEGIN_MESSAGE_MAP(CTest, CDialog)
" by "BEGIN_MESSAGE_MAP(CTest, CAdvancedDialog)
". - Use
CAdvancedDialog
's functions.
If you want the CAdvancedDialog
children to change automatically, create the child dialog using a pointer on the main dialog as the first parameter:
class CMyDialogClass : public CAdvancedDialog {
...
};
CMyDialogClass myDialogBow(this);
myDialogBow.DoModal();
UML class diagram
History
- November 2008: First version distributed on CodeProject.