5,696,038 members and growing! (14,342 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Colour Selection Controls     Intermediate License: The Code Project Open License (CPOL)

Office 97 style Colour Picker control

By Chris Maunder

A simple drop in color chooser control
VC6, C++Windows, NT4, MFC, VS6, Visual Studio, Dev

Posted: 19 Nov 1999
Updated: 8 Dec 2007
Views: 125,895
Bookmarked: 69 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
56 votes for this Article.
Popularity: 8.50 Rating: 4.86 out of 5
0 votes, 0.0%
1
1 vote, 3.0%
2
0 votes, 0.0%
3
3 votes, 9.1%
4
29 votes, 87.9%
5

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:

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.

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)

About the Author

Chris Maunder


Sitebuilder, Financialadmin, Editor, Staff, Admin
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.
Occupation: Founder
Company: The Code Project
Location: Canada Canada

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 39 (Total in Forum: 39) (Refresh)FirstPrevNext
GeneralThanks!memberhwfmoon6:03 9 Oct '08  
GeneralXP StylememberJimmyO23:20 9 Aug '08  
GeneralAs of VC++2008 (VC9)memberalcbz1:52 4 Jun '08  
GeneralOffice 2007 style Color Picker controlmemberOwfAdmin22:31 14 Jul '07  
GeneralRe: Office 2007 style Color Picker controlmemberKuryn14:42 29 Nov '07  
Generalnot compiling with VC7+membertoxcct1:12 5 May '06  
GeneralRe: not compiling with VC7+ [now fixed]membertoxcct3:43 10 May '06  
GeneralRe: not compiling with VC7+ [now fixed]member Dr.Luiji 0:51 20 Dec '07  
GeneralNon-MFC Version?memberjobe37740:31 7 Feb '06  
GeneralCOLORREF ValuememberDody_DK2:47 1 Dec '05  
Generalputting custom color palettesusslaiju22:57 13 Jun '05  
GeneralRe: putting custom color palettememberlaiju23:05 13 Jun '05  
GeneralHow do I add this to MFC Grid Control.memberT Turner5:30 8 Nov '04  
GeneralWPARAM and LPARAM castingsussArthg9:39 20 Aug '04  
GeneralHow to handle MSG in Toolbar?sussTony Lee23:28 6 Jul '04  
GeneralBug on multi-monitor systemmemberPaul S. Vickery0:31 26 May '04  
GeneralKeyboard Navigationmembergjr20:08 25 Mar '04  
GeneralOffice 2000, 2003 colorsmemberAdrian Bacaianu6:34 16 Nov '03  
GeneralColor Picker Licensemembernarm gadiraju22:36 10 Nov '03  
GeneralInstallationmembergentle11:48 28 Oct '03  
GeneralSome addon :)memberNicolay Ch0:53 29 Jun '03  
Generalcasting COLORREF to another typesussMathew Smith17:57 25 May '03  
GeneralRe: casting COLORREF to another typememberJohn M. Drescher19:02 25 May '03  
GeneralAt last !memberTrollslayer4:24 18 May '03  
GeneralRe: At last !adminChris Maunder7:04 18 May '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Dec 2007
Editor: Chris Maunder
Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project