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

Color Picker Combo Box

Rate me:
Please Sign up or sign in to vote.
4.62/5 (12 votes)
18 Oct 2000CPOL 284.8K   10K   60   42
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    President JRTwine Software, LLC
    United States United States
    Programming since the age of 10, started professionally at the age of 17. Currently involved in both Client and Server side development on Win32 platforms for MC/HA/FT financial applications. Development experience with Win32, Win16, Linux and other flavors of Unix.

    Extensive multithreaded development experience on Windows platforms using the Win32 SDK, and MFC.

    Experience with HA/FT n-tiered Client/Server systems as well as GUI apps of varying complexity. Some experience with Game developement.

    Having learned that the stuff you can barely get away with doing Client-side apps just does not cut it in the real "Server World", I am amazed how many 'professionals' cannot tell the difference between "works" and "correct" or try to (mis)use VB and/or MFC on server-side development projects, never considering that just because it RUNS, does not mean it runs WELL.

    Lastly, I am also a collector of arcade games, and can perform repairs, conversions, etc. Search for my name, you will find me on lots of arcade-related documents and sites.

    Sites of interest(?):
    http://www.jrtwine.com
    http://www.jrtwine.com/jtwine
    http://www.signingtime.com
    http://www.deletefxpfiles.com
    http://www.checkfavorites.com
    http://www.coinop.org

    Comments and Discussions

     
    QuestionHow to Create a ComboBox in toolbar? Pin
    white jungle16-Nov-01 3:27
    white jungle16-Nov-01 3:27 
    AnswerRe: How to Create a ComboBox in toolbar? Pin
    Carlos Antollini16-Nov-01 3:50
    Carlos Antollini16-Nov-01 3:50 
    GeneralRe: How to Create a ComboBox in toolbar? Pin
    tvhead8021-Mar-03 17:32
    tvhead8021-Mar-03 17:32 
    GeneralRe: How to Create a ComboBox in toolbar? Pin
    left1none6-Feb-06 23:10
    left1none6-Feb-06 23:10 
    GeneralAdd a check box Pin
    inchak22-Sep-01 17:48
    inchak22-Sep-01 17:48 
    GeneralRe: Add a check box Pin
    James R. Twine14-Jun-06 1:19
    James R. Twine14-Jun-06 1:19 
    GeneralCMYK color support Pin
    Arthur Guntling22-Mar-01 6:27
    Arthur Guntling22-Mar-01 6:27 
    GeneralRe: CMYK color support Pin
    Erik Thompson22-Mar-01 6:59
    sitebuilderErik Thompson22-Mar-01 6:59 
    GeneralRe: CMYK color support Pin
    Arthur Guntling22-Mar-01 23:15
    Arthur Guntling22-Mar-01 23:15 
    GeneralCorrected Version Coming... Pin
    James R. Twine3-Oct-00 4:47
    James R. Twine3-Oct-00 4:47 
    GeneralRe: Corrected Version Coming... Pin
    James R. Twine19-Oct-00 11:38
    James R. Twine19-Oct-00 11:38 
    GeneralGood questions about the Get and Set... Pin
    James R. Twine24-Sep-00 14:39
    James R. Twine24-Sep-00 14:39 
    QuestionWhat do you mean when you say 'attach a CColorPickerCB' to the control? Pin
    Ron King13-Sep-00 5:37
    Ron King13-Sep-00 5:37 
    AnswerRe: What do you mean when you say 'attach a CColorPickerCB' to the control? Pin
    James R. Twine24-Sep-00 14:40
    James R. Twine24-Sep-00 14:40 
    GeneralGood ctrl but where are the SET Functions Pin
    Roger Sager14-Jun-00 23:15
    Roger Sager14-Jun-00 23:15 
    QuestionGood but where is the Get Functions??? Pin
    Anthony17-May-00 2:04
    Anthony17-May-00 2:04 
    AnswerRe: Good but where is the Get Functions??? Pin
    Mark Jackson13-Jun-00 5:21
    Mark Jackson13-Jun-00 5:21 

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

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