Colored ComboBox

If you have ever tried, you must have noticed that coloring a ComboBox is not that trivial at all. If you want to color only the edit and/or the list box, it is very simple, and I am sure you know how to do that. But, if you want ALL the combo to look different, including the Windows’ Gray button (near the edit box), it is not that simple; you have to draw it yourself. I will show how to do it easily and effectively. An example for my colored combo-box can be viewed in the following picture:

In short, the main “technique” here is holding a CBrush (of course) as a member, and the COLORREF I want to use. In the CtlColor method, I change the colors, but the main work is done in the OnPaint() method. There, I actually paint the control by myself—and yes, including the irritating gray button; we all want to change its color! (It is not that simple because we have to know where this button is located—on the left side or on the right, and more…)

Another major problem is in case of the drop-down LIST. In this case, there is no “CEdit” inside the combo-box, so even if we paint it colorfully, the framework paints it back to the Windows’ default. Because of this, I am forbidden to use this style, but manage it differently my way: I had to keep CColoredEdit in any case, and another flag that lets me know what style the user wants to use. CColoredEdit enables me to rule the edit part of the combo, even with a drop list. In this case, I do not use CStatic, but I give a ReadOnly style to this CEDit. There are other problems in the drop-LIST mode: Because there is no CEdit, Windows allows you to open the list-box by clicking the mouse over the static area. In addition, when the list is dropped down, pressing the Enter key selects the item and closes the list. For this purpose, I inherited the CColoredEdit, and created the CColoredEditInCombo class, which is dedicated to work with my comboBox. It handles the mouse and keyboard events, and does the work like Windows.

The biggest problem that I have not solved yet—but I will!!—is the issue of ScrollBars inside the dropped-down list. I have to change their colors too, but as I started working on it, I saw it needs a lot of work. So, in the meantime, you can use this Combo, and if you find a good way to color the ScrollBars, let me know! If I find a way sooner, I will let you know.

To summarize, this Combo works (for now) ONLY in drop-down mode. (Who works with simple mode anyway???) If you want to work with the drop LIST, use my interface easily.

Enjoy.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read