A Color Combo Control

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

screen-shot

To draw items in different colours in a combo box is a little more involved than the usual subclassing routines that some may be used to. We actually need to capture the creation of the combo control. In order to do this, we need to first subclass our form while it is being created (using the SetWindowsHookEx api) and capture the WM_CREATE message for the combo as it’s created. We then need to change the style of the combo to include CBS_OWNERDRAWVARIABLE). Once we have changed the style of the combo as it is created, we can release our existing subclassing.

Now, when the form is loaded, we again need to subclass the form and capture any WM_DRAWITEM for the combo – this allows us to interrupt the painting process for the combo and change the colour of each item as it is drawn.

Be sure to read through the code carefully! You may find that trying to debug the application can cause GPF’s in VB (same as any other subclassing example) – so be careful out there!

Download zipped project and exe (10k)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read