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

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds