Knowledge Base Nr: 00160 colorstatic.cpp - http://www.swe-kaiser.de

MFC: Farbiges Static-Control für Dialoge

  
/*
Mit ClassWizard:
- neue Klasse erzeugen CColorStatic abgeleitet von CStatic
- Memberfunktion für WM_PAINT anlegen: OnPaint()

Im Dialogeditor
- StaticText-Elemente einfügen und mit ClassWizard Membervariablen vom
Typ Control|ColorStatic anlegen
*/

//CColorStatic editieren:
class CColorStatic : public CStatic
{
...
public:
void Text(COLORREF cref, const char* lpszText);
...
protected:
CString m_strText;
COLORREF m_cref;
...
};

CColorStatic::CColorStatic()
{
m_cref = RGB(255,255,255);
m_strText = "ColorStatic";
}

void CColorStatic::Text(COLORREF cref, const char* lpszText)
{
m_cref = cref;
m_strText = lpszText;
}


void CColorStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting

dc.SetBkColor(m_cref);
dc.TextOut(0,0, m_strText);
}

//in Dialogfunktionen benutzen
void CColorcontroltestDlg::OnButtonred()
{
m_text.Text(RGB(255,0,0), "blubber");
Invalidate();
}

void CColorcontroltestDlg::OnButton2()
{
m_text.Text(RGB(0,255,0), "alles nix?");
Invalidate();
}