Click here to Skip to main content
15,896,154 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 37K   2.2K   15  
Using CAdvancedComponent to change the background, foreground, and other colors on CButton, CEdit, and CDialog.
// AdvancedDialog.cpp : implementation file
//

#include "stdafx.h"
#include "AdvancedDialog.h"
#include "AdvancedEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// AdvancedDialog.cpp : implementation file for the CAdvancedDialog class
//
// Written by Loic Brayat (dev@pileouface.org)
//
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// CAdvancedDialog dialog

IMPLEMENT_DYNAMIC(CAdvancedDialog, CDialog)

CAdvancedDialog::CAdvancedDialog(UINT nIDTemplate, CWnd* pParentWnd)
: CDialog(nIDTemplate, pParentWnd)
{
    Initialize(pParentWnd);
}

CAdvancedDialog::CAdvancedDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd)
: CDialog(lpszTemplateName, pParentWnd)
{
    Initialize(pParentWnd);
}

CAdvancedDialog ::~CAdvancedDialog() {

    // If the parent is a CAdvancedDialog...
    if (pParent) {
        // ...we unregister
        pParent->lChilds.remove(this);
    }

    if (cBrush.GetSafeHandle())
       cBrush.DeleteObject();
}

void CAdvancedDialog::CopyColorsFrom(const CAdvancedDialog* pDialog) {
    m_BGColor = pDialog->m_BGColor;
    m_FGColor = pDialog->m_FGColor;
}

void CAdvancedDialog::Initialize(CWnd * pParentWnd) {
    
    bUpdateChild = true;

    CRuntimeClass * tmp = GetRuntimeClass();

    // If the parent is a CAdvancedDialog...
    if (pParentWnd && (pParentWnd->GetRuntimeClass()->IsDerivedFrom(this->GetRuntimeClass()))) {

        // ...we use its values
        CopyColorsFrom(((CAdvancedDialog*)pParentWnd));

        // and register as its child
        ((CAdvancedDialog*)pParentWnd)->lChilds.push_back(this);
        pParent = ((CAdvancedDialog*)pParentWnd);
    }

    else {
        m_BGColor = RGB(80, 255, 10);
        m_FGColor = RGB(20, 10, 255);
        pParent = NULL;
    }
}

void CAdvancedDialog::RedrawComponent() {
    RedrawWindow();

    if (bUpdateChild) {
        std::list <CAdvancedDialog*>::iterator it;
        for (it = lChilds.begin(); it != lChilds.end(); it++) {
            (*it)->CopyColorsFrom(this);
            (*it)->RedrawComponent();
        }
    }
}

BEGIN_MESSAGE_MAP(CAdvancedDialog, CDialog)
	//{{AFX_MSG_MAP(CAdvancedDialog)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdvancedDialog message handlers

HBRUSH CAdvancedDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // If control is a CAdvancedEdit, we don't touch
    CAdvancedEdit adEdit;
    CRuntimeClass * p = pWnd->GetRuntimeClass();
    if (p->IsDerivedFrom(adEdit.GetRuntimeClass()))
        return hbr;

    // Set foreground color
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(m_FGColor);

    // If the control is a CListBox or a CEdit, 
    //  we don't change the background
    switch (nCtlColor) {
        case CTLCOLOR_LISTBOX :
        case CTLCOLOR_EDIT :
            return hbr;
    }

    // Set background color
    if (cBrush.GetSafeHandle())
       cBrush.DeleteObject();
    cBrush.CreateSolidBrush(m_BGColor);    

    return cBrush;
}

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