Download demo project - 30 Kb
Download source - 14 Kb
This control is a simple modification to the "Office 97 style Colour Picker" control by Chris Maunder.
You may refer to that article for futher details. The
new code is dependant on the CColourPopup
class from that article (though it is
included in the source and demo downloads) with one small modification to its
code that asserted the parent class was a CColourPicker
.
I was hoping to capture the appearance of the color button that appears in
some Windows dialogs including the Display Properties|Appearance property page.
The code is very straightforward, borrows from the original CColourPicker
when
possible, and introduces new drawing code to match as closely as possible the
Windows color picker.
Like the original control, this button can be used in place of any dialog or
window button control, and supports direct creation or subclassing though
DDX_Control
. It also includes a DDX_ColorButton
routine for setting and
retreiving the COLORREF
value in DoDataExchange
.
CColorButton
has the following public members:
CColorButton(void);
virtual ~CColorButton(void);
__declspec(property(get=GetColor,put=SetColor)) COLORREF Color;
__declspec(property(get=GetDefaultColor,put=SetDefaultColor)) COLORREF DefaultColor;
__declspec(property(get=GetTrackSelection,put=SetTrackSelection)) BOOL TrackSelection;
__declspec(property(put=SetCustomText)) LPCTSTR CustomText;
__declspec(property(put=SetDefaultText)) LPCTSTR DefaultText;
COLORREF GetColor(void) const;
void SetColor(COLORREF Color);
COLORREF GetDefaultColor(void) const;
void SetDefaultColor(COLORREF Color);
void SetCustomText(LPCTSTR tszText);
void SetDefaultText(LPCTSTR tszText);
void SetTrackSelection(BOOL bTrack);
BOOL GetTrackSelection(void) const;
The table below lists the messages generated by the CColorButton
class.
They are identical to the CColourPicker
messages.
Message |
Description |
lParam |
wParam |
CPN_SELCHANGE | Colour Picker Selection change | Color | Control ID |
CPN_DROPDOWN | Colour Picker drop down | Color | Control ID |
CPN_CLOSEUP | Colour Picker close up | Color | Control ID |
CPN_SELENDOK | Colour Picker end OK | Color | Control ID |
CPN_SELENDCANCEL | Colour Picker end (cancelled) | Color | Control ID |
In order to handle the messages generated by the CColorButton
control, you will need to
manually add ON_MESSAGE(CPN_XXX, MessageFn)
handlers to your message map. The handler function will
take the form of a standard Windows message handler as shown below.
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
ON_MESSAGE(CPN_SELENDOK, OnSelEndOK)
ON_MESSAGE(CPN_SELENDCANCEL, OnSelEndCancel)
ON_MESSAGE(CPN_SELCHANGE, OnSelChange)
ON_MESSAGE(CPN_CLOSEUP, OnCloseUp)
ON_MESSAGE(CPN_DROPDOWN, OnDropDown)
END_MESSAGE_MAP()
LONG CMyDialog::OnSelEndOK(UINT , LONG )
{
TRACE0("Selection ended OK\n");
return TRUE;
}
LONG CMyDialog::OnSelEndCancel(UINT , LONG )
{
TRACE0("Selection cancelled\n");
return TRUE;
}
LONG CMyDialog::OnSelChange(UINT , LONG )
{
TRACE0("Selection changed\n");
return TRUE;
}
LONG CMyDialog::OnCloseUp(UINT , LONG )
{
TRACE0("Colour picker close up\n");
return TRUE;
}
LONG CMyDialog::OnDropDown(UINT , LONG )
{
TRACE0("Colour picker drop down\n");
return TRUE;
}
Acknowledgments
The included source is a minor alteration of the already great code supplied by
Chris Maunder, who had help from Alexander Bischofberger, Paul Wilkerson, and Geir Arne Trillhus.
All appreciative feedback should be passed to them, complaints to me.