Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / MFC

Skin the ComboBox in 3 Simple Function Calls

Rate me:
Please Sign up or sign in to vote.
3.24/5 (17 votes)
24 Mar 20041 min read 145.4K   6.6K   54   19
Set the skin for combobox drop down list of combo box.

Sample Image - Skin_Combo_Box.jpg

Introduction

This is a simple sub classed control from CComboBox that allows you to apply skin to the combobox and its droplist. The skins used can be a pixel wide (for the background and corners of the droplist) to keep the control light.

The CComboBox Overrides

The following functions are overridden (just like in most of the sub classed controls). It's all easy to understand.

C++
virtual BOOL Create(LPCTSTR lpszClassName, 
    LPCTSTR lpszWindowName, DWORD dwStyle, 
    const RECT& rect, CWnd* pParentWnd, UINT nID, 
    CCreateContext* pContext = NULL);
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
C++
afx_msg void OnPaint();
afx_msg void OnDestroy();
C++
afx_msg LRESULT OnCtlColorListBox(WPARAM wParam, LPARAM lParam);

The following function is taken from the multicombobox project on Code Project (sorry I forgot the author and link).

C++
afx_msg LRESULT OnCtlColorListBox(WPARAM wParam, LPARAM lParam);

How to Use

It's very simple to use. Add a combobox on the dialog box, make a member variable of CComboBox type, change the type of object to CBitComboBox in the Dialog header file, and add the bitmaps in the resource to skin the combobox.

Change the header file to look as follows:

Include BitComboBox header:

C++
#include "BitComboBox.h"

Change the control's class to CBitComboBox from CComboBox.

C++
CBitComboBox m_cmbBitmap;

Call the following functions in CBitComboBox class to set resource IDs of the skin bitmaps on OnInitDialog (or anywhere in your project before combobox is created).

C++
m_cmbBitmap.SetComboBitmap(IDB_COMBO_LEFT,
   IDB_COMBO_RIGHT,IDB_COMBO_CEN);
m_cmbBitmap.SetComboListBitmap(IDB_LIST_LEFT, 
   IDB_LIST_RIGHT,IDB_LIST_TOP,IDB_LIST_BOT);
m_cmbBitmap.SetHighlightColor(RGB(115,138,174), RGB(255,255,255));
m_cmbBitmap.SetNormalPositionColor(RGB(115,138,154), RGB(255,255,255));

What is Missing

The following things are missing. If someone can complete them, please let me know:

  1. If there is no skin, set a default combobox should appear (can be done by simply calling the CComboBox functions from overrides).
  2. I have not implemented the skins for edit control of combobox (can be done in OnCtrlColor of CBitComboBox).

History

  • 25th March, 2004: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
Web Developer
Hong Kong Hong Kong
innovating, managing and developing next generation media products and services

Comments and Discussions

 
Questionhow do i add a string Pin
Member 412653612-Jun-15 2:31
Member 412653612-Jun-15 2:31 
QuestionDynamically create button Pin
Member 114400852-Apr-15 19:32
Member 114400852-Apr-15 19:32 
GeneralMy vote of 4 Pin
Sun-Mi Kang11-Sep-13 21:24
Sun-Mi Kang11-Sep-13 21:24 
GeneralMy vote of 5 Pin
JJMatthews20-Apr-12 3:12
JJMatthews20-Apr-12 3:12 
QuestionHow to use the skins for edit control of comboboxex? [modified] Pin
wanglinhai88826-Nov-09 18:53
wanglinhai88826-Nov-09 18:53 
Generalproblem with scrollbar Pin
Mircea Grelus7-Oct-05 0:41
Mircea Grelus7-Oct-05 0:41 
Generalsubclassing listbox Pin
Member 15482897-Apr-05 3:56
Member 15482897-Apr-05 3:56 
GeneralRe: subclassing listbox Pin
artsmouse25-May-05 3:09
artsmouse25-May-05 3:09 
GeneralRe: subclassing listbox Pin
Engin Lee25-May-05 4:21
Engin Lee25-May-05 4:21 
GeneralResize Windows Pin
cochondinde29-Dec-04 13:28
cochondinde29-Dec-04 13:28 
GeneralScrollbar Pin
Holli H.28-Oct-04 4:54
Holli H.28-Oct-04 4:54 
GeneralDrawText Pin
Orxorand30-Sep-04 2:20
Orxorand30-Sep-04 2:20 
Maybe you want to replace
dc.DrawText(sz,&rc,DT_VCENTER | DT_SINGLELINE );
with
dc.SelectObject(GetFont());<br />
dc.DrawText(sz,&rcRepaint,DT_VCENTER | DT_SINGLELINE|DT_END_ELLIPSIS );

to prevent that the Text is writen over the button when it is longer than the combobox and that the font is bold before the list was drawn once.
GeneralAnother another bug, crash and SetCurSel(2) does not work, here is the remedy Pin
Dr. Sai20-Sep-04 23:39
Dr. Sai20-Sep-04 23:39 
GeneralRe: Another another bug, crash and SetCurSel(2) does not work, here is the remedy Pin
Ashok Jaiswal21-Sep-04 15:14
Ashok Jaiswal21-Sep-04 15:14 
GeneralAnother Bug Pin
Dr. Sai16-Sep-04 0:48
Dr. Sai16-Sep-04 0:48 
GeneralBitmap down arrow button Pin
vivadot20-Jun-04 16:49
vivadot20-Jun-04 16:49 
GeneralRe: Bitmap down arrow button Pin
Orxorand4-Oct-04 2:30
Orxorand4-Oct-04 2:30 
GeneralIt crashes when focus comes to skinned combo using down arrow key Pin
Atif Mushtaq25-Mar-04 0:23
Atif Mushtaq25-Mar-04 0:23 
GeneralRe: It crashes when focus comes to skinned combo using down arrow key Pin
Ashok Jaiswal25-Mar-04 14:22
Ashok Jaiswal25-Mar-04 14:22 
GeneralRe: It crashes when focus comes to skinned combo using down arrow key Pin
whatisron3-Apr-04 11:32
whatisron3-Apr-04 11:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.