Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC
Article

The Ultimate Toolbox Color Picker

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
24 Aug 2007CPOL2 min read 35.9K   689   19  
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:

C++
/////////////////////////////////////////////////////////////////////////////

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

C++
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:

C++
    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:

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, 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.
This is a Organisation

476 members

Comments and Discussions

 
-- There are no messages in this forum --