Click here to Skip to main content
15,850,685 members
Articles / Desktop Programming / MFC
Article

Office 97 style Colour Picker control

Rate me:
Please Sign up or sign in to vote.
4.94/5 (40 votes)
8 Dec 2007CPOL3 min read 273.9K   5.2K   89   52
A simple drop in color chooser control

Colour Picker image

In an effort to have the latest and greatest wizz-bang features in my programs I unashamedly ripped of the colour picker from Office 97.

Initially I tried to modify an owner drawn combo box and combine that with a multicolumn combobox, but current multicolumn combo boxes are really just a single column with dividing lines drawn in. I then decided to write the whole thing from scratch based on a button, since it would at least give me a BN_CLICKED notification to get things started.

The colour picker is in two parts: an owner drawn button that reflects the currently selected colour, and a popup colour chooser window to select this colour. When the user clicks on the button the popup window appears and all mouse messages are captured until the left mouse button is clicked, or until the Enter or Escape keys are pressed. The popup window can be navigated using the mouse or the keyboard and includes tooltips explaining what each colour is.

The control can be incorporated into a project like any other CButton derived control. Either Create the control manually, subclass an existing CButton or DDX_control it. The control also comes with a DDX_ColourPicker routine to get/set the colour of the control using a variable of type COLORREF.

The Colour Picker is contained in the class CColourPicker. It uses the class CColourPopup for the popup window. These classes are contained in the file colour_picker_src.zip, and a sample project is contained in colour_picker_demo.zip.

CColourPicker only has the following public member functions:

void     SetColour(COLORREF crColour);
COLORREF GetColour();

void     SetDefaultText(LPCTSTR szDefaultText);
void     SetCustomText(LPCTSTR szCustomText);

void     SetSelectionMode(UINT nMode); // Either CP_MODE_TEXT or CP_MODE_BK

UINT     GetSelectionMode();

void     SetBkColour(COLORREF crColourBk);
COLORREF GetBkColour();
  
void     SetTextColour(COLORREF crColourText);
COLORREF GetTextColour();

SetDefaultText allows you to set the text that will appear in the "Default" area of the colour chooser. If you pass NULL, then the Default area will not be available to the user. If this area is availble and the user selects it, the value CLR_DEFAULT will be returned.

SetCustomText allows you to set the text that will appear in the "Custom" area of the colour chooser. If you pass NULL, then the Custom area will not be available to the user. The Custom area allows the user to select a custom colour using the standard windows colour selection dialog.

You can choose whether the colour chosen using the dropdown colour chooser will affect the text or the background colour using the function SetSelectionMode(int nMode). Possible values for nMode are CP_MODE_TEXT to make colour changes affect the text colour, and CP_MODE_BK to make changes affect the background (default).

SetColour, GetColour and the the DDX-function will set and get the colour according to the current selection mode. To access the text colour and the background colour directly use the Set/GetTextColour and Set/GetBkColour functions.

There are also a number of user messages that may be handled to get more information from the control. These are:

MessageDescription
CPN_SELCHANGEColour Picker Selection change
CPN_DROPDOWNColour Picker drop down
CPN_CLOSEUPColour Picker close up
CPN_SELENDOKColour Picker end OK
CPN_SELENDCANCELColour Picker end (cancelled)

These messages can be handled using

ON_MESSAGE(< MESSAGE>, MessageFn)
in you message map entries, where MessageFn is of the form

afx_msg LONG MessageFn(UINT lParam, LONG wParam);

The demo program gives an example of how to do this.

Acknowledgments

Alexander Bischofberger kindly supplied the Selection mode modifications, as well as the background and text color methods. Paul Wilkerson fixed a focus related bug, and Geir Arne Trillhus also helped fix a few bugs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
QuestionLicense Query- For Chris Maunder Pin
rahulsia811-Dec-15 18:26
rahulsia811-Dec-15 18:26 
AnswerRe: License Query- For Chris Maunder Pin
Chris Maunder1-Dec-15 18:40
cofounderChris Maunder1-Dec-15 18:40 
QuestionOh nice. It's just what i need right now! Pin
JOhn Blacky23-Jan-15 17:39
JOhn Blacky23-Jan-15 17:39 
QuestionControl behavior Pin
David Crow22-Mar-12 4:31
David Crow22-Mar-12 4:31 
GeneralNice ! Thank you ! Pin
wangningyu22-Nov-10 22:02
wangningyu22-Nov-10 22:02 
GeneralVery long text in the Custom or Default Text Area Pin
mihi6422-Aug-09 11:00
mihi6422-Aug-09 11:00 
GeneralRe: Very long text in the Custom or Default Text Area Pin
Chris Maunder23-Oct-09 19:16
cofounderChris Maunder23-Oct-09 19:16 
GeneralProblem with Colour Picker control Pin
BobInNJ1-Mar-09 7:14
BobInNJ1-Mar-09 7:14 
GeneralRe: Problem with Colour Picker control Pin
Chris Maunder1-Mar-09 16:59
cofounderChris Maunder1-Mar-09 16:59 
GeneralRe: Problem with Colour Picker control Pin
Steve Palm4-Mar-09 11:41
Steve Palm4-Mar-09 11:41 
GeneralRe: Problem with Colour Picker control Pin
rbid8-Mar-09 2:55
rbid8-Mar-09 2:55 
GeneralThanks! Pin
hwfmoon9-Oct-08 6:03
hwfmoon9-Oct-08 6:03 
GeneralXP Style Pin
JimmyO9-Aug-08 23:20
JimmyO9-Aug-08 23:20 
GeneralAs of VC++2008 (VC9) Pin
alcbz4-Jun-08 1:52
alcbz4-Jun-08 1:52 
GeneralRe: As of VC++2008 (VC9) Pin
Member 9063139-Jan-09 14:38
Member 9063139-Jan-09 14:38 
GeneralOffice 2007 style Color Picker control Pin
OwfAdmin14-Jul-07 22:31
OwfAdmin14-Jul-07 22:31 
GeneralRe: Office 2007 style Color Picker control Pin
Kuryn29-Nov-07 14:42
Kuryn29-Nov-07 14:42 
Generalnot compiling with VC7+ Pin
toxcct5-May-06 1:12
toxcct5-May-06 1:12 
GeneralRe: not compiling with VC7+ [now fixed] Pin
toxcct10-May-06 3:43
toxcct10-May-06 3:43 
GeneralRe: not compiling with VC7+ [now fixed] Pin
Dr.Luiji20-Dec-07 0:51
professionalDr.Luiji20-Dec-07 0:51 
QuestionNon-MFC Version? Pin
jobe37747-Feb-06 0:31
jobe37747-Feb-06 0:31 
GeneralCOLORREF Value Pin
Dody_DK1-Dec-05 2:47
Dody_DK1-Dec-05 2:47 
Generalputting custom color palette Pin
laiju13-Jun-05 22:57
laiju13-Jun-05 22:57 
GeneralRe: putting custom color palette Pin
laiju13-Jun-05 23:05
laiju13-Jun-05 23:05 
QuestionHow do I add this to MFC Grid Control. Pin
Member 155548-Nov-04 5:30
Member 155548-Nov-04 5:30 

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.