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.



About the Author

Ohad Redlich

programmer, B.Sc Computer Science M.B.A Management and Business Administration

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds