Microsoft KB Archive/148242

= How to change the background color of an OLE control in Visual C++ =

Article ID: 148242

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q148242

<div class="notice_section">

Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

<div class="summary_section">

SUMMARY
This article describes how to modify the background color of an OLE control that subclasses a Windows Control. Sample code for an Edit control is provided.

This article also applies to Button, Static, ListBox, and ComboBox controls.

<div class="moreinformation_section">

MORE INFORMATION
For more information about the exact WM_CTLCOLORxxx message that is sent by each control, click the following article number to view the article in the Microsoft Knowledge Base:

130952 WM_CTLCOLORxxx message changes

If a control sends the WM_CTLCOLORSTATIC, you have to handle the OCM_CTLCOLORSTATIC message in the OCX and so on.

To change the background color of an OLE Control that subclasses an Edit Control, you must handle the OCM_CTLCOLOREDIT(32-bit) messages. These messages are intercepted by the "reflector window" (created for an OLE control that subclasses a Windows control) that reflects them back to the OLE control itself. In response to these reflected messages, you must set the background color (and optionally the foreground color) and return a handle to a brush initialized with the background color.

Step-by-Step Example
<ol> Generate an MFC ActiveX Control Wizard Application, and select the option that allows you to subclass an Edit control.</li>  To handle an OCM_CTLCOLOREDIT reflected window message, declare the following handler function in the .h file of your control's class: LRESULT OnOcmCtlColor(WPARAM wParam, LPARAM lParam); </li> <li> In the .cpp file of your control's class, add an ON_MESSAGE entry to the message map: ON_MESSAGE(OCM_CTLCOLOREDIT, OnOcmCtlColor) </li> <li>Declare CBrush* m_pBackBrush in your control's .h file.</li> <li>Set m_pBackBrush to NULL in the constructor.</li> <li> Release the GDI object and delete the brush object in the control destructor: CEdtclrCtrl::~CEdtclrCtrl {      if(m_pBackBrush!=NULL) {          m_pBackBrush->DeleteObject; delete m_pBackBrush; }   }                    </li></ol>

<div class="references_section">