Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

The Ultimate Toolbox Color Picker

0.00/5 (No votes)
25 Aug 2007 1  
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.

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:

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

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:

/////////////////////////////////////////////////////////////////////////////


// CColorPickerButtonDlg dialog


#include "OXColorPickerButton.h"



class CColorPickerButtonDlg : public CDialog
{
// Construction


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


// Dialog Data


    //{{AFX_DATA(CColorPickerButtonDlg)


    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);
    //{{AFX_DATA_MAP(CColorPickerButtonDlg)


    ...
    //}}AFX_DATA_MAP


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

History

Initial CodeProject release August 2007.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here