Introduction
In creating a product for a client, we needed to show a gradation of color to
represent confidence intervals in an audio spectral analysis (when the product
is released, this page will show an example of the usage of such a table, but I
can't show it right now). The idea is that colors would be more intense at the
high-confidence-level analysis level, and fade to white at the zero-confidence
level. The problem was to get a nice gradation that worked at different
resolutions, and also deal with the fact that the eye's response is somewhat
nonlinear. I did not favor typing in a table and experimenting "by
hand" and the idea that I would have to use something like PowerPoint or
Corel PhotoPaint to create this, and hand-translate to C/C++ did not appeal
either. So I wrote a little utility program that could create and read RGB
declarations suitable for inclusion in a C/C++ source file.
After having created it, I cleaned up the code a bit and decided to publish
it as an example of owner-draw controls. One of the controls, the owner-draw
button, is actually detailed in a companion essay on A
Better Bitmap Button. The other control, an owner-draw ListBox, appears in
another guise in my essay on A
Logging ListBox Control.
The ComboBox is interesting in that it is owner-draw with LBS_HASSTRINGS
whereas the ListBox is owner-draw without LBS_HASSTRINGS
, so two
different styles can be studied.
An example of the control is shown below.
Note that the selection highlighting in the ListBox does not extend across
the entire entry (which would not make sense, given the desire to show a color),
and the selection highlight is grayed out because the ListBox does not currently
have the focus. An array of buttons allows immediate selection of common R, G
and B values. In the ListBox, the only value stored is the COLORREF
value
and the string and color are generated at draw time; in the ComboBox, the
ItemData stores the COLORREF
and the string value stores the name, but
the color swatch and color values are generated at draw time. The sample color
swatch area represents a simple CStatic
and the color is drawn via a
simple OnEraseBkgnd
handler.
Since this was originally constructed as a Quick & Dirty solution to a
specific problem, I did not not apply all my standard production-quality
techniques (such as keeping all English-language strings in the STRINGTABLE
)
so it is not representative of what I consider customer-deliverable code, but it
is a close approximation.