Click here to Skip to main content
11,806,026 members (62,515 online)
Click here to Skip to main content

The Ultimate Toolbox Color Picker

, 24 Aug 2007 CPOL 22.3K 475 18
Rate this:
Please Sign up or sign in to vote.
An easy to use Color Picker button included with the Ultimate Toolbox

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.


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:

  • GetColorPickerCtrl

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.


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:


// CColorPickerButtonDlg dialog

#include <span class="code-string">"OXColorPickerButton.h"

class CColorPickerButtonDlg : public CDialog
// Construction

    CColorPickerButtonDlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data


    CComboBox    m_comboNumColors;
    COXColorPickerButton    m_btnColorPicker;

Next we'll declare a member COLORREF m_clr in the dialog header...


... 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)


    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() 
    // TODO: Add your control notification handler code here

    COLORREF oldColor=m_clr;

    if (!UpdateData(TRUE))


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

The Ultimate Toolbox
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.

Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
Group type: Organisation

411 members

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 25 Aug 2007
Article Copyright 2007 by The Ultimate Toolbox
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid