Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / C++

Color CButton, CEdit, and CDialog using CAdvancedComponent

Rate me:
Please Sign up or sign in to vote.
2.75/5 (8 votes)
3 Nov 2008CPOL1 min read 36.9K   2.2K   15  
Using CAdvancedComponent to change the background, foreground, and other colors on CButton, CEdit, and CDialog.

Sample Image 1

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

  1. Add AdvancedComponent.h, AdvancedButton.h, and AdvancedButton.cpp to your project.
  2. Include "AdvancedButton.h" in the dialog class' header.
  3. Declare the button in the dialog class. For example:
  4. C++
    CAdvancedButton adBtn1;
  5. Initialize the button in the dialog class' OnInitDialog function:
  6. C++
    adBtn1.SubclassDlgItem(IDC_BUTTON1, this, RED, GREEN);
  7. Eventually change the draw form:
  8. C++
    adBtn1.SetDrawType(DRAW_ELLIPSE); // or DRAW_RECT
  9. Eventually change the colors:
  10. C++
    adBtn1.SetColors(RED);
    adBtn1.SetBackGround(YELLOW);
    adBtn1.SetForeGround(BLUE);

Using the CAdvancedEdit

  1. Add AdvancedComponent.h, AdvancedEdit.h, and AdvancedEdit.cpp to your project.
  2. Include "AdvancedEdit.h" in the dialog class' header.
  3. Declare the edit in the dialog class. For example:
  4. C++
    CAdvancedEdit adEdit1;
  5. Initialize the edit in the dialog class' OnInitDialog function:
  6. C++
    adEdit1.SubclassDlgItem(IDC_EDIT1, this);
  7. Change the colors:
  8. C++
    adEdit1.SetBackGround(YELLOW);
    adEdit1.SetForeGround(BLUE);
    adEdit1.SetTextBackGround(RED);

Using the CAdvancedDialog

  1. Add AdvancedComponent.h, AdvancedDialog.h, and AdvancedDialog.cpp to your project.
  2. Include "AdvancedDialog.h" in the dialog class' header.
  3. Replace the base class from CDialog to CAdvancedDialog in your dialog class' header.
  4. In the .cpp file, in the constructor, replace CDialog by CAdvancedDialog.
  5. In the .cpp file, replace CDialog::DoDataExchange by CAdvancedDialog::DoDataExchange.
  6. If they exist, do the same thing for OnInitDialog and the other inherited functions.
  7. In the .cpp file, replace "BEGIN_MESSAGE_MAP(CTest, CDialog)" by "BEGIN_MESSAGE_MAP(CTest, CAdvancedDialog)".
  8. 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:

C++
class CMyDialogClass : public CAdvancedDialog {
    ...
};

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

UML class diagram

UML Diagram

History

  • November 2008: First version distributed on CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
France (Metropolitan) France (Metropolitan)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --