How to subclass CListBox and CEdit inside of CComboBox

Article translations Article translations
Article ID: 174667 - View products that this article applies to.
This article was previously published under Q174667
Expand all | Collapse all

On This Page


While it is simple to directly subclass a combo box control, it is not simple to subclass the edit or list box inside a combo box. The problem is that it is difficult to get the HWNDs of the child controls in a portable manner.

One safe way to subclass the internal edit and list box controls is to subclass them in the WM_CTLCOLORXXX messages. Because Win32 sends separate WM_CTLCOLOREDIT and WM_CTLCOLORLISTBOX messages, these messages are safe and easy ways to get the HWNDs of the child controls of the combo box.

Below is a CSuperComboBox class, which is an MFC implementation of this method. Because MFC routes all the WM_CTLCOLOR messages to OnCtlColor, the subclassing takes place there.


Use ClassWizard to derive a class from CComboBox and add message handlers for WM_CTLCOLOR and WM_DESTROY. Then manually edit the header file to add the data members, m_edit and m_listbox. Finally, copy the code from the message handlers below:

Sample code

   // SuperComboBox.h : header file
   class CSuperComboBox : public CComboBox
      CEdit      m_edit;
      CListBox   m_listbox;
      afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
      afx_msg void OnDestroy();

   // SuperComboBox.cpp : implementation file
   HBRUSH CSuperComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      if (nCtlColor == CTLCOLOR_EDIT)
         //[ASCII 160][ASCII 160][ASCII 160]Edit control
         if (m_edit.GetSafeHwnd() == NULL)
      else if (nCtlColor == CTLCOLOR_LISTBOX)
         //ListBox control
         if (m_listbox.GetSafeHwnd() == NULL)
      HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
      return hbr;

   void CSuperComboBox::OnDestroy()
      if (m_edit.GetSafeHwnd() != NULL)
      if (m_listbox.GetSafeHwnd() != NULL)
Note that for subclassing to occur, the dialog box must be painted at least once. There are cases when the dialog box doesn't paint at all (for example, closing the dialog box before it is displayed, hidden dialog boxes). This method may not be suitable when access to the subclassed windows are needed in these cases.

(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Kelly Marie Ward, Microsoft Corporation


Article ID: 174667 - Last Review: November 21, 2006 - Revision: 4.1
  • 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++ 4.2 Professional Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 5.0 Professional Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
kbhowto kbuidesign kbcombobox kbctrl kbcode KB174667

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from