Click here to Skip to main content
14,607,949 members

Color Picker Combo Box

Rate this:
4.58 (11 votes)
Please Sign up or sign in to vote.
4.58 (11 votes)
18 Oct 2000CPOL
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


    James Twine originally based this code on earlier work by Baldvin Hansson. Mark Jackson ( 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.


    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


    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    About the Author

    James R. Twine
    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(?):

    Comments and Discussions

    QuestionDebug Assertion Failed! Pin
    asdfdfewe11-Jul-16 21:19
    Memberasdfdfewe11-Jul-16 21:19 
    AnswerRe: Debug Assertion Failed! Pin
    James R. Twine12-Jul-16 14:49
    MemberJames R. Twine12-Jul-16 14:49 
    GeneralRe: Debug Assertion Failed! Pin
    asdfdfewe12-Jul-16 23:24
    Memberasdfdfewe12-Jul-16 23:24 
    Generalexactly what i needed Pin
    ekt_22-May-12 22:00
    Memberekt_22-May-12 22:00 
    GeneralInitializing dropbox Pin
    al250022-Jul-07 15:01
    Memberal250022-Jul-07 15:01 
    QuestionAsserttion failed! Pin
    AndersChen14-Jan-07 2:37
    MemberAndersChen14-Jan-07 2:37 
    AnswerRe: Asserttion failed! Pin
    James R. Twine14-Jan-07 7:13
    MemberJames R. Twine14-Jan-07 7:13 
    GeneralRe: Asserttion failed! Pin
    AndersChen15-Jan-07 2:22
    MemberAndersChen15-Jan-07 2:22 
    GeneralDoubt Pin
    Member 303984313-Jun-06 21:11
    MemberMember 303984313-Jun-06 21:11 
    AnswerFirst Steps... (Was: Re: Doubt) Pin
    James R. Twine14-Jun-06 1:13
    MemberJames R. Twine14-Jun-06 1:13 
    QuestionChanging the Font Pin
    pscholl10-May-06 22:17
    Memberpscholl10-May-06 22:17 
    AnswerRe: Changing the Font Pin
    James R. Twine14-Jun-06 1:15
    MemberJames R. Twine14-Jun-06 1:15 
    AnswerRe: Changing the Font Pin
    pscholl27-Feb-07 5:02
    Memberpscholl27-Feb-07 5:02 
    GeneralDebug assertion failed Pin
    left1none6-Feb-06 23:22
    Memberleft1none6-Feb-06 23:22 
    AnswerRe: Debug assertion failed Pin
    James R. Twine14-Jun-06 1:16
    MemberJames R. Twine14-Jun-06 1:16 
    GeneralRe: Debug assertion failed Pin
    shvalbo14-Dec-06 2:22
    Membershvalbo14-Dec-06 2:22 
    GeneralOwner draw combobox with CBS_DROPDOWN style Pin
    G.A.14-Dec-04 23:18
    MemberG.A.14-Dec-04 23:18 
    GeneralRe: Owner draw combobox with CBS_DROPDOWN style Pin
    James R. Twine14-Jun-06 1:18
    MemberJames R. Twine14-Jun-06 1:18 
    AnswerRe: Owner draw combobox with CBS_DROPDOWN style Pin
    Member 426330219-May-09 0:12
    MemberMember 426330219-May-09 0:12 
    QuestionHow can I use a CComboBox as a Lenght specifier? Pin
    Timawa_6920-Apr-04 21:00
    MemberTimawa_6920-Apr-04 21:00 
    AnswerRe: How can I use a CComboBox as a Lenght specifier? Pin
    James R. Twine26-Apr-04 14:19
    MemberJames R. Twine26-Apr-04 14:19 
    QuestionCan this combo box be made FLAT Pin
    mangeshb12316-Oct-03 18:48
    Membermangeshb12316-Oct-03 18:48 
    AnswerRe: Can this combo box be made FLAT Pin
    James R. Twine17-Oct-03 3:30
    MemberJames R. Twine17-Oct-03 3:30 
    QuestionHow to initialize with my own colors? I'd add few methods.. Pin
    andru1236-Nov-02 0:16
    Memberandru1236-Nov-02 0:16 
    AnswerRe: How to initialize with my own colors? I'd add few methods.. Pin
    James R. Twine8-Aug-03 6:58
    MemberJames R. Twine8-Aug-03 6:58 

    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.

    Posted 16 Jan 2000


    59 bookmarked