HOWTO: Change the Color of an MFC Child Control ClassLast reviewed: June 26, 1997Article ID: Q132080 |
The information in this article applies to:
SUMMARYTo change the color scheme of a standard class of controls in an MFC-based application, follow these steps:
MORE INFORMATIONThis approach works for list boxes, the list boxes of combo boxes, button controls, edit controls, static controls, message boxes, and dialog boxes. (This approach does not work for push buttons and the CRichEditCtrl. The color of a standard CButton object is determined by system settings. If you want a different color for push buttons, use a CBitmapButton. To change the color of a CRichEditCtrl use its member functions.) For an alternative approach, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q117778 TITLE : Changing the Background Color of an MFC Edit ControlIt illustrates how to change the background color of a control in the parent window. When a control is about to be redrawn, it sends the message WM_CTLCOLOR to its parent. This message is handled by the OnCtlColor() member function of the parent. OnCtlColor() allows the parent to modify the drawing of the child by:
The following sample code defines a class of edit controls with red text on a green background. The sample code shows only what is necessary to change the color of the controls. It does not include code generated by the ClassWizard.
Sample Code
/* Compile options needed: Default */ // NOTE: The sample code is for 32-bit. It has to be modified for // 16-bit. See the comment below. // ** MYEDIT.H ** class CMyEdit : public CEdit { public: BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM Param, LRESULT* pLResult); protected: static CBrush m_brush; }; // ** MYEDIT.CPP ** #include "myedit.h" // Create a green brush for the background for the class of controls: CBrush CMyEdit::m_brush(RGB(0,128,0)); BOOL CMyEdit::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult) { // If "message" is not the message you're after, do default processing: // For 16-bit applications change WM_CTLCOLOREDIT to WM_CTLCOLOR if (message != WM_CTLCOLOREDIT) { return CEdit::OnChildNotify(message,wParam,lParam,pLResult); } // Set the text foreground to red: HDC hdcChild = (HDC)wParam; SetTextColor(hdcChild, RGB(0,0,255)); // Set the text background to green: SetBkColor(hdcChild, RGB(0,128,0)); // 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; } |
Additional reference words: 2.50 2.51 2.52 3.00 3.10 3.20
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |