[CStatic & CFrameWnd] Hintergurndfarbe eines CStatic controls ändern.
-
Hallo zusammen,
ich habe folgendes Problem.
Ich versuch die Hintergrundfarbe eines CStatic Elements zu ändern. Bei Google finde ich diversen Sourcecode der für ein CDialog Element vielleicht funktioniert, bei einem CFrameWnd allerdings versagt.
Kann mir da irgendjemand helfen?
Hier mein Code:
Die FrameWnd Klasseclass SkodaMainFrame : public CFrameWnd { public: SkodaMainFrame(); protected: CStatic m_FaderBez; CStatic m_BalanceBez; CSliderCtrl m_FaderSlider; CSliderCtrl m_BalanceSlider; CStatic m_FaderValue; CStatic m_BalanceValue; afx_msg BOOL OnEraseBkgnd( CDC * dc); afx_msg HBRUSH OnCtlColor( CDC* dc, CWnd* wnd, UINT color); DECLARE_MESSAGE_MAP() };
Die OnCtlColor Methode
HBRUSH SkodaMainFrame::OnCtlColor( CDC* dc, CWnd* wnd, UINT color) { dc->SetTextColor(RGB(255,0,0)); dc->SetBkColor(RGB(192,192,192)); return (HBRUSH)(CBrush(RGB(0,0,0))); }
Entwicklungsumgebung: MS Visual C++ 6.0
Danke für die Hilfe
Tobi
-
Du könntest deine eigene von CStatic abgeleitete Klasse schreiben.
Kleines Beispiel:
CColorStatic.h#if !defined(AFX_COLORSTATIC_H__EAD89BD9_AA06_4DAC_95B4_4607417A11F6__INCLUDED_) #define AFX_COLORSTATIC_H__EAD89BD9_AA06_4DAC_95B4_4607417A11F6__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // ColorStatic.h : Header-Datei // ///////////////////////////////////////////////////////////////////////////// // Fenster CColorStatic class CColorStatic : public CStatic { private: CBrush m_brush; // Konstruktion public: CColorStatic(); // Attribute public: // Operationen public: // Überschreibungen // Vom Klassen-Assistenten generierte virtuelle Funktionsüberschreibungen //{{AFX_VIRTUAL(CColorStatic) //}}AFX_VIRTUAL // Implementierung public: virtual ~CColorStatic(); // Generierte Nachrichtenzuordnungsfunktionen protected: //{{AFX_MSG(CColorStatic) afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ fügt unmittelbar vor der vorhergehenden Zeile zusätzliche Deklarationen ein. #endif // AFX_COLORSTATIC_H__EAD89BD9_AA06_4DAC_95B4_4607417A11F6__INCLUDED_
und natürlich noch die
CColorStatic.cpp// ColorStatic.cpp: Implementierungsdatei // #include "stdafx.h" #include "MyCStatic.h" #include "ColorStatic.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CColorStatic CColorStatic::CColorStatic() { m_brush.CreateSolidBrush(RGB(255,0,0)); } CColorStatic::~CColorStatic() { } BEGIN_MESSAGE_MAP(CColorStatic, CStatic) //{{AFX_MSG_MAP(CColorStatic) ON_WM_CTLCOLOR_REFLECT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // Behandlungsroutinen für Nachrichten CColorStatic HBRUSH CColorStatic::CtlColor(CDC* pDC, UINT nCtlColor) { // TODO: Attribute des Gerätekontexts hier ändern // TODO: Pinsel ungleich NULL zurückgeben, falls die Behandlungsroutine des übergeordneten nicht aufgerufen werden soll //return NULL; pDC->SetBkMode(TRANSPARENT); return (HBRUSH)m_brush; }
-
Das musst Du aber auch nicht neu erfinden.
Hier findest Du 2 Artikel, die dies alles bereits zur Verfügung stellen.
Allerdings setzt das auch voraus, dass die Static-Controls auf eigene Klassen umgesetzt werden.http://www.codeproject.com/staticctrl/coloredit_colorstatic.asp
http://www.codeproject.com/staticctrl/CFontStatic.aspGruß André
-
Hallo,
danke euch zwei, werde mich mal daran versuchen...
Ich hoffe es hilft...
Schönen Feierabend
Gruss
Tobi
-
guenni81 schrieb:
Du könntest deine eigene von CStatic abgeleitete Klasse schreiben.
Kleines Beispiel:
CColorStatic.h#if !defined(AFX_COLORSTATIC_H__EAD89BD9_AA06_4DAC_95B4_4607417A11F6__INCLUDED_) #define AFX_COLORSTATIC_H__EAD89BD9_AA06_4DAC_95B4_4607417A11F6__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // ColorStatic.h : Header-Datei // ///////////////////////////////////////////////////////////////////////////// // Fenster CColorStatic class CColorStatic : public CStatic { private: CBrush m_brush; // Konstruktion public: CColorStatic(); // Attribute public: // Operationen public: // Überschreibungen // Vom Klassen-Assistenten generierte virtuelle Funktionsüberschreibungen //{{AFX_VIRTUAL(CColorStatic) //}}AFX_VIRTUAL // Implementierung public: virtual ~CColorStatic(); // Generierte Nachrichtenzuordnungsfunktionen protected: //{{AFX_MSG(CColorStatic) afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ fügt unmittelbar vor der vorhergehenden Zeile zusätzliche Deklarationen ein. #endif // AFX_COLORSTATIC_H__EAD89BD9_AA06_4DAC_95B4_4607417A11F6__INCLUDED_
und natürlich noch die
CColorStatic.cpp// ColorStatic.cpp: Implementierungsdatei // #include "stdafx.h" #include "MyCStatic.h" #include "ColorStatic.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CColorStatic CColorStatic::CColorStatic() { m_brush.CreateSolidBrush(RGB(255,0,0)); } CColorStatic::~CColorStatic() { } BEGIN_MESSAGE_MAP(CColorStatic, CStatic) //{{AFX_MSG_MAP(CColorStatic) ON_WM_CTLCOLOR_REFLECT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // Behandlungsroutinen für Nachrichten CColorStatic HBRUSH CColorStatic::CtlColor(CDC* pDC, UINT nCtlColor) { // TODO: Attribute des Gerätekontexts hier ändern // TODO: Pinsel ungleich NULL zurückgeben, falls die Behandlungsroutine des übergeordneten nicht aufgerufen werden soll //return NULL; pDC->SetBkMode(TRANSPARENT); return (HBRUSH)m_brush; }
Hi guennie81,
hat super funktioniert...
Danke...
Gruss
Tobi