// ColorStatic.cpp : implementation file // // Copyright (C) 2002-2004 William Braynen // Contact information can be found at www.samdurak.com // // The CColorStatic MFC class consists of two files: ColorStatic.cpp // and ColorStatic.h. It is a subclass of CStatic and allows to // change the background and text colors of a Windows static control. // However, it changes the text color of all the text and does not // implement multi-colored text or multi-colored backgrounds. // // Use this class just as you would a regular CStatic class. // Just replace 'CStatic' in the declaration by 'CColorStatic'. // And don't forget to #include ColorStatic.h // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "stdafx.h" #include "ColorStatic.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define RGB_DEFAULT_BACKGROUND RGB(0,0,0) #define RGB_DEFAULT_TEXT RGB(255,255,255) ///////////////////////////////////////////////////////////////////////////// // CColorStatic CColorStatic::CColorStatic (COLORREF backgroundColor, COLORREF textColor) { if (0 == backgroundColor && 0 == textColor) { m_backgroundColor = RGB_DEFAULT_BACKGROUND; m_textColor = RGB_DEFAULT_TEXT; } // Create a green brush for the background for the class of controls: m_brush.CreateSolidBrush (m_backgroundColor); } CColorStatic::~CColorStatic() { m_brush.DeleteObject(); } BEGIN_MESSAGE_MAP(CColorStatic, CStatic) //{{AFX_MSG_MAP(CColorStatic) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CColorStatic message handlers BOOL CColorStatic::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult) { if (WM_CTLCOLOR != message && WM_CTLCOLORSTATIC != message) { return CStatic::OnChildNotify(message, wParam, lParam, pLResult); } // Set the text foreground to red: HDC hdcChild = (HDC)wParam; ::SetTextColor(hdcChild, m_textColor); // Set the text background to green: ::SetBkColor(hdcChild, m_backgroundColor); // Send what would have been the return value of OnCtlColor() - the // brush handle - back in pLResult: *pLResult = (LRESULT)(m_brush.GetSafeHandle()); // Return TRUE to indicate that the message was handled: return TRUE; } void CColorStatic::SetBackgroundColor(COLORREF color) { // Create a green brush for the background for the class of controls: m_brush.DeleteObject(); m_brush.CreateSolidBrush (color); m_backgroundColor = color; } void CColorStatic::SetTextColor(COLORREF color) { m_textColor = color; } void CColorStatic::SetFont(int nPointSize, LPCTSTR lpszFaceName) { m_font.CreatePointFont (nPointSize, lpszFaceName); CWnd::SetFont (&m_font); }