Office 97 style Colour Picker control

Environment: VC4, VC5, VC6, Win9x, WinNT

This sample was contributed by Chris Maunder,
and extended by Alexander Bischofberger.

Colour Picker image
Download source files (13 Kb) or
sample project (41 Kb).

Latest updates here. (version 1.3)

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(&lt MESSAGE&gt, 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.


New in Version 1.1

The following changes have been made by Alexander Bischofberger.

The original version did not display a text inside the combo box. The current version
now displays the button text (as set in the dialog resource, or via SetWindowText).

You can choose whether the colour chosen using the dropdown colour chooser will
affect the text or the background colour using the new member function
SetSelectionMode:

		#define CP_MODE_TEXT 1		// Colour selection affects text colour
#define CP_MODE_BK 2 // Colour selection affects background colour (default)

void SetSelectionMode(UINT nMode);
UINT GetSelectionMode();

Two new member functions have been introduced to set the text colour and the
background colour directly:

		void     SetBkColour(COLORREF crColourBk);
COLORREF GetBkColour();

void SetTextColour(COLORREF crColourText);
COLORREF GetTextColour();

SetColour, GetColour and the the DDX-function
will set and get the colour according to the current selection mode.

Some minor changes were made in the drawing routine (now faster and able to show a
pressed combo box button), the messaging (CPN_SELCHANGE now sent to the parent).

The following changes have been made by
Chris Maunder
.

The button now stays pressed while colour choosing is in progress, and the initial
colour sent to the drop down colour chooser is highlighted as in Office 97. Oh – and
a line now separates the Custom Text area from the colours. weee!

Palette support is now included so things don’t look so bad in 256 colours.

See the updated sample how to use the new
functionality.

New in Version 1.2

The following changes have been made by
Chris Maunder
.

The picker now looks a little more like the office 97 version, although it
does not as yet support the “cool” look. Any number of colours can now be used
in the picker (although these are still hardwired in).

Two new functions were added:

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

These specify the text (if any) that will appear in the “Default” selection
area and the “Custom colour” selection area. See main section of this doc for
details.

New in Version 1.21

Minor bug fix when losing focus to another application. Fix suggested by
Paul Wilkerson.

New in Version 1.3

Colour cells now drawn with thin border (thanks to Geir Arne Trillhus).
If cancel is hit in the custom colour picker popup, a CPN_SELENDOK is no
longer sent. Also compiles clean under VC6

Updated 9 January 1999

More by Author

Must Read