HOWTO: Handle OCM_CTLCOLORxxx Reflected Messages

Last reviewed: July 2, 1997
Article ID: Q148242
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++, 32-bit Edition, version 4.0, 4.1, 4.2, 5.0

SUMMARY

This article shows you how to change the background color of an OLE control that subclasses a Windows Control, with sample code for an Edit control.

This article should apply to Button, Static, ListBox, and ComboBox controls as well.

MORE INFORMATION

Please refer to the following article in the Microsoft Knowledge Base:

   ARTICLE_ID : Q130952
   TITLE      : WM_CTLCOLORxxx Message Changes for Windows 95

for more about the exact WM_CTLCOLORxxx message sent by each control. 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

The sample code in this example illustrates how to handle OCM_CTLCOLOREDIT in order to change the background color of an OLE control that subclasses an Edit control

  1. Generate an MFC ActiveX Control Wizard Application, and select the option that allows you to subclass an Edit control.

  2. 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);
    

  3. In the .cpp file of your control's class, add an ON_MESSAGE entry to the message map:

          ON_MESSAGE(OCM_CTLCOLOREDIT, OnOcmCtlColor)
    

  4. Also in the .cpp file, implement the OnOcmCtlColor member function to process the reflected message:

          // Assuming CEdtclrCtrl is the class for this control
          LRESULT CEdtclrCtrl::OnOcmCtlColor(WPARAM wParam, LPARAM lParam)
          {
    
            //Declare CBrush* m_pBackBrush in your control's .h file
            if (m_pBackBrush == NULL)
               m_pBackBrush = new CBrush(RGB(0,0,0));
            CDC* pdc = CDC::FromHandle((HDC)wParam);
            pdc->SetBkMode(TRANSPARENT);
            pdc->SetBkColor(RGB(0,0,0));
            pdc->SetTextColor(RGB(0,255,0));
            HBRUSH far* hbr = (HBRUSH far*)m_pBackBrush->GetSafeHandle();
            return ((DWORD)hbr);
          }
    
    
NOTE: In your control's constructor, set m_pBackBrush = NULL, and in your control's destructor, delete m_pBackbBrush.

  1. Build and register your control.

  2. Insert this control into the ActiveX Control Test Container. Notice that the background color of your OLE control is changed.

REFERENCES

Refer to technical article “TN062: Message Reflection for Windows Controls” and the article “ActiveX Controls: Sub-classing a Windows Control in Visual C++ Programmer's Guide.”


Keywords : kbprg MfcOLE kbhowto
Technology : kbMfc kbole
Version : 4.0 4.1 4.2 5.0
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.