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
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
DDX_ColourPicker routine to get/set the colour of
the control using a variable of type
The Colour Picker is contained in the class
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
CColourPicker only has the following public member functions:
void SetColour(COLORREF crColour);
void SetDefaultText(LPCTSTR szDefaultText);
void SetCustomText(LPCTSTR szCustomText);
void SetSelectionMode(UINT nMode);
void SetBkColour(COLORREF crColourBk);
void SetTextColour(COLORREF crColourText);
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
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
to make colour changes affect the text colour, and
CP_MODE_BK to make changes
affect the background (default).
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
There are also a number of user messages that may be handled to
get more information from the control. These are:
|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
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.
Current passion project: CodeProject.AI Server
. Please join in!
Chris is the Co-founder of the popular code-sharing site CodeProject.com, the digital advertising agency DeveloperMedia.com and the content marketing agency ContentLab.IO.
He's been programming way too long and has been, in various guides, an astrophysicist, mechanic, mathematician, physicist, breeder of carnivorous plants, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a serial entrepreneur.
Chris has programmed everything from FORTRAN on a CRAY to Python on a Pi, but generally leans on TypeScript, C#, and SQL for the front, middle and back bits of his applications. His current focus is on ensuring developers know enough about Artificial Intelligence to be dangerous.