Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

Owner-Draw Control Example: A Color Table Generator

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
4 Feb 20022 min read 159.6K   1.4K   23   26
A control for generating colour gradients

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.

Image 1

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

Comments and Discussions

 
QuestionIs this project still active? Pin
RedDk30-Apr-14 6:08
RedDk30-Apr-14 6:08 
Generalcomplie error Pin
kinghei17-Sep-05 0:23
kinghei17-Sep-05 0:23 
GeneralRe: complie error Pin
Joseph M. Newcomer17-Sep-05 6:19
Joseph M. Newcomer17-Sep-05 6:19 
GeneralRe: complie error Pin
Joseph M. Newcomer17-Sep-05 14:31
Joseph M. Newcomer17-Sep-05 14:31 
Generalreadme.txt file missing Pin
JasonDoucette8-Apr-04 4:55
JasonDoucette8-Apr-04 4:55 
GeneralRe: readme.txt file missing Pin
Joseph M. Newcomer8-Apr-04 6:01
Joseph M. Newcomer8-Apr-04 6:01 
Generalread rgb compose value Pin
31-Mar-02 15:04
suss31-Mar-02 15:04 
GeneralRe: read rgb compose value Pin
Joseph M. Newcomer31-Mar-02 17:00
Joseph M. Newcomer31-Mar-02 17:00 
QuestionWhy is this rated so low? Pin
Nish Nishant5-Feb-02 20:01
sitebuilderNish Nishant5-Feb-02 20:01 
AnswerRe: Why is this rated so low? Pin
Senkwe Chanda5-Feb-02 20:28
Senkwe Chanda5-Feb-02 20:28 
GeneralRe: Why is this rated so low? Pin
Nish Nishant5-Feb-02 20:37
sitebuilderNish Nishant5-Feb-02 20:37 
GeneralRe: Why is this rated so low? Pin
Nemanja Trifunovic6-Feb-02 8:01
Nemanja Trifunovic6-Feb-02 8:01 
GeneralRe: Why is this rated so low? Pin
Nish Nishant6-Feb-02 8:21
sitebuilderNish Nishant6-Feb-02 8:21 
GeneralRe: Why is this rated so low? Pin
Nemanja Trifunovic6-Feb-02 8:37
Nemanja Trifunovic6-Feb-02 8:37 
GeneralRe: Why is this rated so low? Pin
Nish Nishant6-Feb-02 8:39
sitebuilderNish Nishant6-Feb-02 8:39 
GeneralRe: Why is this rated so low? Pin
Bill Leibold6-Feb-02 9:03
Bill Leibold6-Feb-02 9:03 
GeneralRe: Why is this rated so low? Pin
Nish Nishant6-Feb-02 9:10
sitebuilderNish Nishant6-Feb-02 9:10 
AnswerRe: Why is this rated so low? Pin
peterchen6-Feb-02 7:54
peterchen6-Feb-02 7:54 
GeneralRe: Why is this rated so low? Pin
Nish Nishant6-Feb-02 8:20
sitebuilderNish Nishant6-Feb-02 8:20 
AnswerRe: Why is this rated so low? Pin
Chris Losinger6-Feb-02 8:10
professionalChris Losinger6-Feb-02 8:10 
GeneralRe: Why is this rated so low? Pin
Nish Nishant6-Feb-02 8:18
sitebuilderNish Nishant6-Feb-02 8:18 
GeneralRe: Why is this rated so low? Pin
Bill Leibold6-Feb-02 9:06
Bill Leibold6-Feb-02 9:06 
GeneralRe: Why is this rated so low? Pin
Nish Nishant6-Feb-02 9:12
sitebuilderNish Nishant6-Feb-02 9:12 
GeneralRe: Why is this rated so low? Pin
Bill Leibold6-Feb-02 9:57
Bill Leibold6-Feb-02 9:57 
GeneralRe: Why is this rated so low? Pin
Nish Nishant6-Feb-02 16:03
sitebuilderNish Nishant6-Feb-02 16:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.