Visit the Ultimate Toolbox main page for an overview and configuration guide to the Ultimate Toolbox library.
Source code and project files for this sample can be found in the samples\gui\ColorPickerButton directory of the sample projects download.
Overview
The COXColorPickerButton
is derived from COXBitmapButton
(with the DROPDOWN
style predefined) and uses COXColorPickerCtrl
to display the Color Picker popup bar.
You can set any color to the COXColorPickerButton
using SetColor
and this color can be retrieved using GetColor
.
To display the associated color, we draw a color band at the bottom of the button. By default, the height of the band would be 5 pixels (if neither image nor text is associated with the button, then the color band takes all available space). You can set/get the color band height using:
SetColorBandHeight
GetColorBandHeight
The COXColorPickerCtrl
control associated with COXColorPickerButton
can be retrieved using:
Also, some helper functions provided set/get default the color of the associated Color Picker control:
SetDefaultColor
GetDefaultColor
You can also use COXBitmapButton
and COXColorPickerCtrl
functions to customize the COXColorPickerButton
.
Usage
For this CDialog
based example, we've created a command button on the dialog resource with an ID of IDC_BUTTON_COLOR_PICKER
, and the Owner Draw property set to True, and added a CButton
member variable m_btnColorPicker
with the Add Member Variable Wizard.
Next, we'll include the appropriate header and switch the declaration to one of COXColorPickerButton
in the dialog header:
#include "OXColorPickerButton.h"
class CColorPickerButtonDlg : public CDialog
{
public:
CColorPickerButtonDlg(CWnd* pParent = NULL);
enum { IDD = IDD_COLORPICKERBUTTON_DIALOG };
CComboBox m_comboNumColors;
COXColorPickerButton m_btnColorPicker;
...
Next we'll declare a member COLORREF m_clr
in the dialog header...
COLORREF m_clr;
... and add a DDX data exchange call inside the dialog's DoDataExchange
method that will cause the control to update this member when UpdateData(TRUE)
is called:
void CColorPickerButtonDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
...
DDX_ColorPicker(pDX, IDC_BUTTON_COLOR_PICKER, m_clr);
}
We'll have added a BN_CLICKED
event with the class wizard or Properties | Events wizard, so getting the color the user selected is simple - this sample from the Gui\ColorPickerButton sample simply redraws the areas on the dialog that display the color selected:
void CColorPickerButtonDlg::OnButtonColorPicker()
{
COLORREF oldColor=m_clr;
if (!UpdateData(TRUE))
return;
if(m_clr!=oldColor)
{
InvalidateRect(m_rectTop);
InvalidateRect(m_rectBottom);
}
}
The sample also shows how to set the number of colors to be shown in the associated COXColorPickerCtrl
, the sizes of the color buttons, and the various COXBitmapButton
styles available.
You will find full class references for all of these classes in the compiled HTML help documentation.
Initial CodeProject release August 2007.