CCombobox Owner Draw Color Dialog

   CComboBox class in MFC is used for manipulating the combo box. MFC allows achieving any custom behaviors on controls by inheriting from its own classes. This article illustrates an  owner-draw combo box class, which inherits from the CComboBox class.

   The class uses an abstract object to draw the items. In this case a class derived from the abstract object is created to make the control list colors. This abstract c++ mfc class can also be used as a parent class to derive a new class and implement new or different functionalities.

Abstract class for implementing CComboBox owner draw:

   The abstract class CDrawC contains methods that are called from the combo box class only. They are

       virtual void DrawItem(LPDRAWITEMSTRUCT lpdis, bool bHasFocus) = 0;

   This DrawItem function actually draws the items inside the mfc combo boxes using the Win32 DRAWITEMSTRUCT.

      virtual void MeasureItem(LPMEASUREITEMSTRUCT lpmis) = 0;

    This function sets the item size once or every time depending of the owner-draw style: fixed or variable on the CComboBox derived combo box class.

      virtual void InitCombo(CComboBox* pCombo) = 0;

   Initializes the control holding the class if it is a CComboBox (or derived from it)

      virtual int AddItemCmb(CComboBox* pCombo, UINT nItem) = 0;

   Adds an item to the control holding the class if it is a CComboBox (or derived from it).

Using the CcomboBox derived owner draw class:

  1. This class is attached to the combo box via it?s method SetDrawC. It must be added before the control is created, so it is better called in the parent dialog class constructor.

  2. The Init() method of the control should be called immediately after it?s creation.

  3. Items are added with the AddItem function of the control.

  4. Combo boxes using from this type can be created either dynamically or using the dialog editor and after adding a variable of type CComboBox just rename it with CODrawCombo.

  5. Don?t forget to call SetDrawC and Init.

  6. Combo boxes must be created with the following styles: CBS_DROPDOWNLIST, CBS_HASSTRINGS, CBS_OWNERDRAWFIXED or CBS_OWNERDRAWVARIABLE.

   In this particular example a combo box listing colors is implemented. The init method is obsolete for there is no initialization needed for this case. The AddItem implementation inserts a not used string in the control and sets the item data to the color needed: 

virtual int AddItemCmb(CComboBox* pCombo, UINT nItem) {
           
int n;
           
n = pCombo->AddString("nu");
           
pCombo->SetItemData(n, nItem);
           
return n;
}

   After that in the drawing function the color is received as item data and drawn in a black rectangle like this:

CPen pen(PS_SOLID, 1, RGB(0,0,0));
CBrush brush;
?.
dc.SelectObject(pen);
dc.SelectObject(brush);
dc.Rectangle(ColRect);
?.
//lpszText is the number
dc.DrawText(lpszText, strlen(lpszText), TextRect, DT_SINGLELINE|DT_VCENTER);

   When the selection in the combo box is changed an event is raised. We catch this event to update the sample text with Invalidate. But the function that actually changes the color of the sample text is OnCtlColor. There you can see how to get the currently selected color from the control ie:

m_colorCombo.GetItemData(m_colorCombo.GetCurSel())

   Read more on general MFC Owner draw issues. This might help in reducing the general complexities of programming owner draw controls.

   Find the source files and project files here. 

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > c > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction