Click here to Skip to main content
Click here to Skip to main content

A color picker button

, 4 Sep 2000
Rate this:
Please Sign up or sign in to vote.
A simple modification to Chris Maunder's "Office 97 style Colour Picker" control

  • Download demo project - 30 Kb
  • Download source - 14 Kb
  • Sample image

    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:

    //***********************************************************************
    // Name:        CColorButton
    // Description:	Default constructor.
    // Parameters:	None.
    // Return:      None.	
    // Notes:       None.
    //***********************************************************************
    CColorButton(void);
    
    //***********************************************************************
    // Name:        CColorButton
    // Description:	Destructor.
    // Parameters:	None.
    // Return:      None.		
    // Notes:       None.
    //***********************************************************************
    virtual ~CColorButton(void);
    
    //***********************************************************************
    //**                        Property Accessors                         **
    //***********************************************************************	
    __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;
    
    //***********************************************************************
    // Name:        GetColor
    // Description:	Returns the current color selected in the control.
    // Parameters:	void
    // Return:      COLORREF 
    // Notes:       None.
    //***********************************************************************
    COLORREF GetColor(void) const;
    
    //***********************************************************************
    // Name:        SetColor
    // Description:	Sets the current color selected in the control.
    // Parameters:	COLORREF Color
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetColor(COLORREF Color);
    
    
    //***********************************************************************
    // Name:        GetDefaultColor
    // Description:	Returns the color associated with the 'default' selection.
    // Parameters:	void
    // Return:      COLORREF 
    // Notes:       None.
    //***********************************************************************
    COLORREF GetDefaultColor(void) const;
    
    //***********************************************************************
    // Name:        SetDefaultColor
    // Description:	Sets the color associated with the 'default' selection.
    //              The default value is COLOR_APPWORKSPACE.
    // Parameters:	COLORREF Color
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetDefaultColor(COLORREF Color);
    
    //***********************************************************************
    // Name:        SetCustomText
    // Description:	Sets the text to display in the 'Custom' selection of the
    //              CColourPicker control, the default text is "More Colors...".
    // Parameters:  LPCTSTR tszText
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetCustomText(LPCTSTR tszText);
    
    //***********************************************************************
    // Name:        SetDefaultText
    // Description:	Sets the text to display in the 'Default' selection of the
    //              CColourPicker control, the default text is "Automatic". If
    //              this value is set to "", the 'Default' selection will not
    //              be shown.
    // Parameters:	LPCTSTR tszText
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetDefaultText(LPCTSTR tszText);
    
    //***********************************************************************
    // Name:        SetTrackSelection
    // Description:	Turns on/off the 'Track Selection' option of the control
    //              which shows the colors during the process of selection.
    // Parameters:	BOOL bTrack
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetTrackSelection(BOOL bTrack);
    
    //***********************************************************************
    // Name:		GetTrackSelection
    // Description:	Returns the state of the 'Track Selection' option.
    // Parameters:	void
    // Return:		BOOL 
    // Notes:		None.
    //***********************************************************************
    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 changeColorControl ID
    CPN_DROPDOWN Colour Picker drop downColorControl ID
    CPN_CLOSEUP Colour Picker close upColorControl ID
    CPN_SELENDOK Colour Picker end OKColorControl ID
    CPN_SELENDCANCEL Colour Picker end (cancelled)ColorControl 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)
    	//{{AFX_MSG_MAP(CMyDialog)
    	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)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    LONG CMyDialog::OnSelEndOK(UINT /*lParam*/, LONG /*wParam*/)
    {
        TRACE0("Selection ended OK\n");
        return TRUE;
    }
    
    LONG CMyDialog::OnSelEndCancel(UINT /*lParam*/, LONG /*wParam*/)
    {
        TRACE0("Selection cancelled\n");
        return TRUE;
    }
    
    LONG CMyDialog::OnSelChange(UINT /*lParam*/, LONG /*wParam*/)
    {
        TRACE0("Selection changed\n");
        return TRUE;
    }
    
    LONG CMyDialog::OnCloseUp(UINT /*lParam*/, LONG /*wParam*/)
    {
        TRACE0("Colour picker close up\n");
        return TRUE;
    }
    
    LONG CMyDialog::OnDropDown(UINT /*lParam*/, LONG /*wParam*/)
    {
        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.

    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

    Share

    About the Author

    James White
    Web Developer
    United States United States
    James manages RationalPath Inc, specializing in ASP.NET productivity applications, from the North Shore of Long Island.

    Comments and Discussions

     
    Questionhow to change the background of other controls Pinmemberiyranly26-Sep-07 22:34 
    AnswerRe: how to change the background of other controls PinmemberJames White27-Sep-07 3:26 
    GeneralRe: how to change the background of other controls Pinmemberiyranly27-Sep-07 4:08 
    GeneralRe: how to change the background of other controls PinmemberJames White27-Sep-07 5:29 
    GeneralRe: how to change the background of other controls Pinmemberiyranly27-Sep-07 17:59 
    GeneralRe: how to change the background of other controls PinmemberJames White28-Sep-07 3:42 
    GeneralRe: how to change the background of other controls Pinmemberiyranly28-Sep-07 3:54 
    QuestionDDX_ColorButton bug? Pinmembersteverino18-Oct-06 10:26 
    GeneralDoesn't work right when moved Pinmembernaveed15-Nov-04 6:50 
    GeneralRe: Doesn't work right when moved PinmemberJames White15-Nov-04 7:04 
    GeneralRe: Doesn't work right when moved Pinmembernaveed15-Nov-04 11:14 
    QuestionWhat is TrackSelection? Pinmemberquzi2-Mar-04 2:15 
    AnswerRe: What is TrackSelection? PinmemberJames White2-Mar-04 3:42 
    GeneralRe: What is TrackSelection? Pinmemberquzi3-Mar-04 0:03 
    GeneralRe: What is TrackSelection? PinmemberJames White3-Mar-04 6:38 
    GeneralAutomatic Color PinsussAnonymous6-Sep-03 3:16 
    Generalproblem with many buttons Pinmembermimosa25-Aug-03 21:22 
    GeneralRe: problem with many buttons PinmemberJames White26-Aug-03 3:53 
    GeneralRe: problem with many buttons Pinmembermimosa27-Aug-03 13:30 
    GeneralRe: problem with many buttons Pinmembermimosa27-Aug-03 14:58 
    GeneralRe: problem with many buttons PinmemberJames White28-Aug-03 5:05 
    GeneralAdding to a Toolbar Pinsussmyinger17-Apr-03 11:12 
    General(Possibly) Serious Error PinmemberJonah Bishop29-Jan-03 15:49 
    GeneralRe: (Possibly) Serious Error PinmemberJames White30-Jan-03 3:22 
    GeneralRe: (Possibly) Serious Error PinmemberPaul S. Vickery7-Nov-03 4:06 
    GeneralRe: (Possibly) Serious Error PinmemberJames White7-Nov-03 4:43 
    GeneralRe: (Possibly) Serious Error PinmemberPaul S. Vickery7-Nov-03 5:26 
    GeneralNot loosing focus PinmemberPetrK6-Jan-03 3:15 
    GeneralRe: Not loosing focus PinmemberCommoDave21-Jul-03 20:02 
    GeneralTwo clicks aren't always better than one PinmemberDerek Lakin30-Apr-02 11:04 
    GeneralRe: Two clicks aren't always better than one PinmemberJames White30-Apr-02 11:16 
    GeneralRe: Two clicks aren't always better than one PinmemberDerek Lakin30-Apr-02 22:30 
    GeneralSupport for Multiple Displays PinmemberRob Adams15-Aug-01 8:53 
    GeneralMore Multiple Displays fix PinmemberIng-Long Eric Kuo16-Sep-03 23:29 
    AnswerRe: Support for Multiple Displays PinmemberTruePyroman10-Feb-06 11:46 
    GeneralUsing Colopicker in Property Page PinmemberJohn Hale26-Jan-01 1:29 
    GeneralIt's works in Property Page PinmemberRoy Luo3-Mar-02 18:25 
    GeneralRe: Using Colopicker in Property Page Pinsusseli k.6-Nov-02 20:23 
    GeneralUsing your code PinmemberGreg Garrison29-Dec-00 10:53 
    GeneralRe: Using your code PinmemberAnonymous29-Dec-00 11:08 
    GeneralWindow Activation PinsussHilton Campbell14-Sep-00 9:33 
    GeneralRe: Window Activation PinsussJames White15-Sep-00 3:25 
    GeneralRe: Window Activation PinmemberCameron Grant27-May-01 20:06 
    GeneralRe: Window Activation PinmemberKyle Alons26-Feb-02 12:54 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Mobile
    Web01 | 2.8.141015.1 | Last Updated 5 Sep 2000
    Article Copyright 2000 by James White
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid