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

BitmapPickerComboEx - A Nice Looking Combo Box with Sections

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
22 Jan 2009CPOL2 min read 40.8K   2K   36   6
Control to create a Combobox dropdown with icons and sections headers

Introduction 

This ComboBox style control features a nice drop down list with icons and headlines. It's useful if you have a ComboBox with a limited, pre-defined list of choices that can be grouped into sections. Typical uses are project types, document types, etc.

Screenshots

Image 1Image 2

Screenshots: Sample usages for BitmapPickerComboEx, taken from my file sync software.

Basic Usage

You'll find three elements in the ComboBox:

  • Headlines (Bold, without icons)
  • Items (with Icons)
  • Blanks links

Setting It Up

The Control

The ComboBox control is set up with the usual steps for custom control:

  1. In the resource editor: Create a ComboBox in your dialog.
  2. Use the OwnerdrawnVariable and HasStrings options in the ComboBox properties
  3. In your *.h file: Add this line "CBitmapPickerCombo m_Combox;"
  4. In your *.cpp file: Add this line "DDX_Control(pDX, IDC_COMBO1, m_Combox);" in DoDataExchange. (The number in IDC_COMBO1 may differ. Check the ComboBox properties.)
  5. Add the files BitmapPickerCombo.cpp and BitmapKeyed.cpp to your project.

The Bitmaps

The control needs bitmaps if you want to display them in the ComboBox. For each image:

  1. In the resource editor: Create a Bitmap with black background and a key bitmap (of the same object) with white background and black pixels instead of colors.
  2. In your *.h file: Add this line "CBitmapKeyed bitmap" (With a different name each time, of course)
  3. Initialize each bitmap with "bitmap.Init (this, NULL, IDB_YOURIMAGE, IDB_YOURIMAGEKEY);"

Filling It With Data 

Create a Headline

C++
m_Combox.AddBitmap (NULL, "Headline");

Create an Item

C++
m_Combox.AddBitmap (&bitmap, "Item");

(See above for "bitmap".)

Create a Blank Line

C++
m_Combox.AddBitmap (NULL, "");

Handling Invalid Selections

Please note that any of the above line types can be selected by the user. I experimented with making headlines and blank lines non-selectable but that made the selected with cursor keys impossible. I'd recommend to check for an invalid selected (like a blank line) and disable the "OK" button in your dialog in such a case.

Acknowledgements

The code is heavily based on Anneke Sicherer-Roetman's BitmapPickerCombo (also available on The Code Project). I only made some modifications to make it look nicer, so I can claim only a small part of the credit. Thanks Anneke.

History

  • 22nd January, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDelete item Pin
blueh16-Mar-14 16:52
blueh16-Mar-14 16:52 
AnswerRe: Delete item Pin
ITSTH16-Mar-14 22:28
ITSTH16-Mar-14 22:28 
GeneralMy vote of 2 Pin
Grump27-Jan-09 6:34
Grump27-Jan-09 6:34 
GeneralSuggestions Pin
Hans Dietrich23-Jan-09 0:39
mentorHans Dietrich23-Jan-09 0:39 
AnswerRe: Suggestions Pin
ITSTH23-Jan-09 1:42
ITSTH23-Jan-09 1:42 
GeneralRe: Suggestions Pin
Hans Dietrich23-Jan-09 5:42
mentorHans Dietrich23-Jan-09 5:42 
OK, thanks.

In the case where you're adding blank lines to the combo, you may want to add code to the control to disallow selection of a blank line (similar to what happens in menus with separator line).

Also, there is no need to include these files in zip (they will be re-created): .aps, .clw, .ncb, .opt, .plg


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.