Click here to Skip to main content
15,886,110 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.
#ifndef __ADVANCEDBUTTON_H__
#define __ADVANCEDBUTTON_H__

/////////////////////////////////////////////////////////////////////////////
// AdvancedButton.h : header file for the CAdvancedButton class
//
// Written by Loic Brayat (dev@pileouface.org)
//
// HOW TO :
//
// - Create the button on the dialog box
// - Include "AdvancedButton.h" in the dialog class's header
// - Declare the button in the dialog class. For example :
//      CAdvancedButton adBtn1;
// - Initialize the button in the dialog's class OnInitDialog funtion :
//      adBtn1.SubclassDlgItem(IDC_BUTTON1, this, RED, GREEN);
// - Eventually change the colors later :
//      adBtn1.SetColors(RED);
//      adBtn1.SetBackGround(YELLOW);
//      adBtn1.SetForeGround(BLUE);
//      ...
//
/////////////////////////////////////////////////////////////////////////////

#include "AdvancedComponent.h"

typedef enum {DRAW_RECT=0, DRAW_ELLIPSE=1} TAdvancedButtonDrawType;

class CAdvancedButton : public CButton, public CAdvancedComponent
{
DECLARE_DYNAMIC(CAdvancedButton)
public:

	CAdvancedButton(); 
	virtual ~CAdvancedButton(); 

	bool SubclassDlgItem(
        const UINT nID, CWnd* pParent, 
		const COLORREF BGColor = LTGRAY,
		const COLORREF FGColor = BLACK,
		const COLORREF DisabledColor = DKGRAY
	);

    bool SetColors(
        const COLORREF BGColor, 
        const COLORREF FGColor = BLACK,
        const COLORREF DisabledColor = DKGRAY
    );

    COLORREF GetDisabledColor() const { return m_disabled; }

    void SetDrawType(TAdvancedButtonDrawType type) { 
        drawType = type; 
        this->RedrawComponent();
    };

    TAdvancedButtonDrawType GetDrawType() const { return drawType; };

protected:

	void SetState(bool bHighlight);

	void PreSubclassWindow();

    void RedrawComponent() {
        RedrawWindow();
    }

	virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);

private:

    COLORREF m_disabled;
    TAdvancedButtonDrawType drawType;

    DECLARE_MESSAGE_MAP()

};

/////////////////////////////////////////////////////////////////////////////

#endif 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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