Click here to Skip to main content
11,804,008 members (80,137 online)
Click here to Skip to main content

Office 97 style Colour Picker control

, 8 Dec 2007 CPOL 213.7K 4.4K 84
Rate this:
Please Sign up or sign in to vote.
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, and a sample project is contained in

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:

Message Description
CPN_SELCHANGE Colour Picker Selection change
CPN_DROPDOWN Colour Picker drop down
CPN_CLOSEUP Colour Picker close up
CPN_SELENDOK Colour Picker end OK
CPN_SELENDCANCEL Colour 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.


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.


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


About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...

Comments and Discussions

QuestionOh nice. It's just what i need right now! Pin
JOhn Blacky23-Jan-15 16:39
memberJOhn Blacky23-Jan-15 16:39 
QuestionControl behavior Pin
DavidCrow22-Mar-12 3:31
memberDavidCrow22-Mar-12 3:31 
GeneralNice ! Thank you ! Pin
Koma Wang22-Nov-10 21:02
memberKoma Wang22-Nov-10 21:02 
GeneralVery long text in the Custom or Default Text Area Pin
Member 243931322-Aug-09 10:00
memberMember 243931322-Aug-09 10:00 
GeneralRe: Very long text in the Custom or Default Text Area Pin
Chris Maunder23-Oct-09 18:16
adminChris Maunder23-Oct-09 18:16 
GeneralProblem with Colour Picker control Pin
BobInNJ1-Mar-09 6:14
memberBobInNJ1-Mar-09 6:14 
GeneralRe: Problem with Colour Picker control Pin
Chris Maunder1-Mar-09 15:59
adminChris Maunder1-Mar-09 15:59 
GeneralRe: Problem with Colour Picker control Pin
Steve Palm4-Mar-09 10:41
memberSteve Palm4-Mar-09 10:41 
GeneralRe: Problem with Colour Picker control Pin
rbid8-Mar-09 1:55
memberrbid8-Mar-09 1:55 
GeneralThanks! Pin
hwfmoon9-Oct-08 5:03
memberhwfmoon9-Oct-08 5:03 
GeneralXP Style Pin
JimmyO9-Aug-08 22:20
memberJimmyO9-Aug-08 22:20 
GeneralAs of VC++2008 (VC9) Pin
alcbz4-Jun-08 0:52
memberalcbz4-Jun-08 0:52 
GeneralRe: As of VC++2008 (VC9) Pin
Member 9063139-Jan-09 13:38
memberMember 9063139-Jan-09 13:38 
GeneralOffice 2007 style Color Picker control Pin
OwfAdmin14-Jul-07 21:31
memberOwfAdmin14-Jul-07 21:31 
GeneralRe: Office 2007 style Color Picker control Pin
Kuryn29-Nov-07 13:42
memberKuryn29-Nov-07 13:42 
Generalnot compiling with VC7+ Pin
toxcct5-May-06 0:12
membertoxcct5-May-06 0:12 
GeneralRe: not compiling with VC7+ [now fixed] Pin
toxcct10-May-06 2:43
membertoxcct10-May-06 2:43 
GeneralRe: not compiling with VC7+ [now fixed] Pin
Dr.Luiji 19-Dec-07 23:51
member Dr.Luiji 19-Dec-07 23:51 
QuestionNon-MFC Version? Pin
jobe37746-Feb-06 23:31
memberjobe37746-Feb-06 23:31 
GeneralCOLORREF Value Pin
Dody_DK1-Dec-05 1:47
memberDody_DK1-Dec-05 1:47 
Generalputting custom color palette Pin
laiju13-Jun-05 21:57
susslaiju13-Jun-05 21:57 
GeneralRe: putting custom color palette Pin
laiju13-Jun-05 22:05
memberlaiju13-Jun-05 22:05 
QuestionHow do I add this to MFC Grid Control. Pin
T Turner8-Nov-04 4:30
memberT Turner8-Nov-04 4:30 
GeneralWPARAM and LPARAM casting Pin
Arthg20-Aug-04 8:39
sussArthg20-Aug-04 8:39 
QuestionHow to handle MSG in Toolbar? Pin
Tony Lee6-Jul-04 22:28
sussTony Lee6-Jul-04 22:28 
GeneralBug on multi-monitor system Pin
Paul S. Vickery25-May-04 23:31
memberPaul S. Vickery25-May-04 23:31 
GeneralKeyboard Navigation Pin
gjr25-Mar-04 19:08
membergjr25-Mar-04 19:08 
GeneralOffice 2000, 2003 colors Pin
Adrian Bacaianu16-Nov-03 5:34
memberAdrian Bacaianu16-Nov-03 5:34 
GeneralColor Picker License Pin
narm gadiraju10-Nov-03 21:36
membernarm gadiraju10-Nov-03 21:36 
GeneralInstallation Pin
gentle28-Oct-03 10:48
membergentle28-Oct-03 10:48 
GeneralSome addon :) Pin
Nicolay Ch28-Jun-03 23:53
memberNicolay Ch28-Jun-03 23:53 
Generalcasting COLORREF to another type Pin
Mathew Smith25-May-03 16:57
sussMathew Smith25-May-03 16:57 
GeneralRe: casting COLORREF to another type Pin
John M. Drescher25-May-03 18:02
memberJohn M. Drescher25-May-03 18:02 
GeneralAt last ! Pin
Trollslayer18-May-03 3:24
memberTrollslayer18-May-03 3:24 
GeneralRe: At last ! Pin
Chris Maunder18-May-03 6:04
adminChris Maunder18-May-03 6:04 
GeneralRe: At last ! Pin
AndrewVos25-May-06 1:31
memberAndrewVos25-May-06 1:31 
GeneralColors in Colour Picker Pin
Last Walrus18-Mar-03 22:47
sussLast Walrus18-Mar-03 22:47 
GeneralRe: Colors in Colour Picker Pin
Brad Bruce27-Mar-03 8:43
memberBrad Bruce27-Mar-03 8:43 
GeneralRe: Colors in Colour Picker Pin
Eugene Karankow27-Mar-03 23:25
memberEugene Karankow27-Mar-03 23:25 
GeneralHelp me!! I get confused by the skill of genius..... Pin
eslea30-Nov-02 0:18
membereslea30-Nov-02 0:18 
GeneralWIndows CE Version Pin
Joao Paulo Figueira12-Nov-02 1:30
memberJoao Paulo Figueira12-Nov-02 1:30 
GeneralRe: WIndows CE Version Pin
Chris Maunder12-Nov-02 7:44
adminChris Maunder12-Nov-02 7:44 
GeneralRe: WIndows CE Version Pin
shritiParashars7-Dec-06 20:03
membershritiParashars7-Dec-06 20:03 
QuestionCan I attatch in toolbar? Pin
ende3-Nov-02 19:29
sussende3-Nov-02 19:29 
AnswerRe: Can I attatch in toolbar? Pin
jefrusse28-Jan-03 7:19
memberjefrusse28-Jan-03 7:19 
GeneralRe: Can I attatch in toolbar? Pin
Gao Qingzhong31-Jan-04 8:50
memberGao Qingzhong31-Jan-04 8:50 
GeneralRe: Can I attatch in toolbar? Pin
jefrusse3-Feb-04 4:19
memberjefrusse3-Feb-04 4:19 
GeneralRe: Can I attatch in toolbar? Pin
duanjin5-Apr-10 22:00
memberduanjin5-Apr-10 22:00 
GeneralNeed to write code for BN_CLICKED in dialog class Pin
Deelip Menezes27-Jul-01 21:21
memberDeelip Menezes27-Jul-01 21:21 
QuestionHow to handle On_SelChange Pin
Reyes16-May-00 18:41
sussReyes16-May-00 18:41 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 8 Dec 2007
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid