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

Color Picker Combo Box

0.00/5 (No votes)
18 Oct 2000 5  
A combobox derived class that provides a simple color picker
  • Download demo project (James Twine's version) - 26 Kb
  • Download source files (James Twine's version) - 8 Kb


  • Download demo project (Mark Jackson's version) - 18 Kb
  • Download source files (Mark Jackson's version) - 2 Kb
  • Sample Image - ColourPickerCB.gif

    Introduction

    James Twine originally based this code on earlier work by Baldvin Hansson. Mark Jackson (www.mjsoft.co.uk) has expanded this to add a "Custom..." option at the bottom of the list which displays the standard colour picker dialog, and has also added DDX data exchange. The code has also been improved and tidied up in many other ways.

    The CColorPickerCB class implements a Combobox that displays colors as well as the name of the colors. Colors may be added or removed at runtime, and the control can be queried for the COLORREF value, or the name of the selected color. You can also set the selected color.

    The control correctly handles selection, enabled and disabled drawing. Since the control uses strings for display, the colors can be sorted.

    To use this control, create a Drop List Combobox with the Owner Draw Fixed and Has Strings styles. Attach a CColorPickerCB to the control, and off you go!

    The initialization routine populates the color picker with a color selection that is a subset of the X11 colorset, and are the colors that are recognized by IE.

    Note that the control will use 1/4 (one fourth) of its width for the color block, and the rest, minus a few pixels, for the color text. Be sure you make it wide enough.

    Credits

    Thanks to Marcel Galema for finding a bug with my inverted (selected) color usage, and suggesting a fix for it.

    Thanks also to Paul Wardle for providing DDX routines.

    Public Functions

    Here is a list of public functions in the CColorPickerCB class:

    COLORREF GetSelectedColorValue(void);          // Get Selected Color Value
    
    CString GetSelectedColorName(void);            // Get Selected Color Name
    
    void SetSelectedColorValue(COLORREF crColor);  // Set Selected Color Value
    
    void SetSelectedColorName(PCSTR cpColor);      // Set Selected Color Name
    
    
    // Initialize The Control With The Default Colorset
    
    void InitializeDefaultColors( void );          
    
    bool RemoveColor(PCSTR cpColor);               // Remove Color From List
    
    bool RemoveColor(COLORREF crColor);            // Remove Color From List
    
    int  AddColor(PCSTR cpName, COLORREF crColor); // Insert A New Color
    
    

    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