Click here to Skip to main content
Email Password   helpLost your password?
  • 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
    
    
    You must Sign In to use this message board.
     
     
    Per page   
     FirstPrevNext
    GeneralInitializing dropbox
    al2500
    16:01 22 Jul '07  
    Hi,

    I'd like to initialize the colorpicker combo box to a previously selected color.

    I'm using

    m_ColorPicker.SetSelectedColorValue(m_ColorPickerColor);

    in OnInitDialog.

    I verified that m_ColorPickerColor is a valid COLORREF value but the drop box is blank and the variable m_ColorPickerColor is 0H when the following is executed in OnOK

    m_ColorPickerColor = m_ColorPicker.GetSelectedColorValue();

    I guessing I need more than SetSelectedColorValue.

    Thanks


    QuestionAsserttion failed!
    jrmd
    3:37 14 Jan '07  
    When CColorPickerCB running in based on dialog app is OK.
    But when I dynamic creating it, I get an assertion in winctrl1.cpp line 186:

    void CComboBox::MeasureItem(LPMEASUREITEMSTRUCT)
    { ASSERT(FALSE); }


    My source code as follows:

    // in .h file:

    CColorPickerCB m_wndColorPicker;

    // in .cpp file:

    int CMyWnd::OnCreate() {
    bRet = m_wndTextColor.Create( WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | // <-- Assert!
    CBS_OWNERDRAWFIXED | CBS_SORT | CBS_HASSTRINGS | WS_VSCROLL,
    CRect(0,0,0,0), this, IDC_CHATOPAREA_TEXTCOLOR );

    ...
    ......
    }

    Why?
    AnswerRe: Asserttion failed!
    James R. Twine
    8:13 14 Jan '07  
    jrmd wrote:
    My source code as follows:
    // in .h file:
    CColorPickerCB m_wndColorPicker;
    // in .cpp file:
    int CMyWnd::OnCreate() {
    bRet = m_wndTextColor.Create( WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | // <-- Assert!
    CBS_OWNERDRAWFIXED | CBS_SORT | CBS_HASSTRINGS | WS_VSCROLL,
    CRect(0,0,0,0), this, IDC_CHATOPAREA_TEXTCOLOR );
       Well, for starters, the CColorPickerCB class is used for the m_wndColorPicker member, but your code is failing when you try to create whatever class the m_wndTextColor member is.

       If you have a good look at the point where the ASSERT is firing, you will see that it is firing in the CComboBox base class, and not within CColorPickerCB.

       Peace!



    -=- James
    Please rate this message - let me know if I helped or not!
    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
    See DeleteFXPFiles

    GeneralRe: Asserttion failed!
    jrmd
    3:22 15 Jan '07  
    Now is OK because I added a virtual member function for CColorPickerCB:

    void CColorPickerCB::MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
    {
    }
    GeneralDoubt
    sruti_p
    22:11 13 Jun '06  
    Its really a very good program.
    But i am getting an exception when i add a new member variable to CClorPickerCB.
    Any solution plz........
    AnswerFirst Steps... (Was: Re: Doubt)
    James R. Twine
    2:13 14 Jun '06  
       OK - the first thing that anyone having trouble has to do is take the example application, build it, and test it.  If the unmodified test app fails, then post a problem.

       If it works correctly, it should be used as a guide to how to use the control.

       If you are going to post without any further details on the problem (code being used, errors and/or exceptions encountered, call stack, debug vs. release issues, etc.) then there is no help I can provide.

       Peace!

    -=- James
    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
    DeleteFXPFiles & CheckFavorites
    (Please rate this post!)
    QuestionChanging the Font
    pscholl
    23:17 10 May '06  
    Dynamically changing (enlarging) the Font results in a very ugly and unreadable output because the line spacing between the different colors is not adjusted.
    Is there an easy way to overcome this problem?
    My control is on a full-page CFormView and I shrink/enlarge the font together with all the controls on the formview to account for the different screen resolutions.
    I tried overiding OnMeasureItem in the view, but this is not called when I change the font.
    AnswerRe: Changing the Font
    James R. Twine
    2:15 14 Jun '06  
       Please send me a minimal application (zipped and encrypted) that demonstrates what you are trying to do.  I will have a look at it.

       Send it to spam(at)jrtwine(dot)com (yes, that is a real address).

       Peace!

    -=- James
    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
    DeleteFXPFiles & CheckFavorites
    (Please rate this post!)
    AnswerRe: Changing the Font
    pscholl
    6:02 27 Feb '07  
    Well, 9 months later... My apologies, I'm sorry.
    I have found the simple (when you know it) answer myself. Something along the lines:

    m_ColorCode.SetItemHeight(0, iHeight);

    seems to do the trick nicely.



    GeneralDebug assertion failed
    left1none
    0:22 7 Feb '06  
    when I run my debug build of my app I get an assertion in winctrl1.cpp line 202. I create the variable and then call create and it is created on the toolbar. I know the code is correct until I get to the creation of your control because if I change the variable to CComboBox then it works fine. Plus it doesn't do anything wrong when I build it in release mode or in debug it does ok if I hit ignore. What could be the problem. Here is where I create it. Let me know if this would be the problem and why. m_wndToolBar.m_wndSnap.Create(WS_CHILD|CBS_DROPDOWNLIST|CBS_OWNERDRAWFIXED|CBS_HASSTRINGS, rect, &m_wndToolBar, IDC_SNAP_COMBO);

    m_wndSnap is an instance of your control. Please help.
    AnswerRe: Debug assertion failed
    James R. Twine
    2:16 14 Jun '06  
       Please send me a minimal application (zipped and encrypted) that demonstrates what you are trying to do.  I will have a look at it.

       Send it to spam(at)jrtwine(dot)com (yes, that is a real address).

       Peace!

    -=- James
    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
    DeleteFXPFiles & CheckFavorites
    (Please rate this post!)
    GeneralRe: Debug assertion failed
    shvalbo
    3:22 14 Dec '06  
    in order to solve this problem all you have to do is override the
    MeasureItem() method...that's it.

    virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);

    do it with the wizard.
    GeneralOwner draw combobox with CBS_DROPDOWN style
    G.A.
    0:18 15 Dec '04  
    Hello everybody,
    I'm trying to make a color combobox control. I set CBS_HASSTRINGS and CBS_OWNERDRAWFIXED styles and handle virtual function DrawItem(). It works great with CBS_DROPDOWNLIST style, but when I make combobox with CBS_DROPDOWN or CBS_SIMPLE my colors are lost. Can anyone tell me what I'm doing wrong?

    Thanks in advans.
    GeneralRe: Owner draw combobox with CBS_DROPDOWN style
    James R. Twine
    2:18 14 Jun '06  
       The control only supports the CBS_DROPDOWNLIST style of combobox.

       Peace!

    -=- James
    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
    DeleteFXPFiles & CheckFavorites
    (Please rate this post!)
    AnswerRe: Owner draw combobox with CBS_DROPDOWN style
    Member 4263302
    1:12 19 May '09  
    SetFont
    GeneralHow can I use a CComboBox as a Lenght specifier?
    Timawa_69
    22:00 20 Apr '04  
    Great code......

    can you help me with mine, See I need to draw a line which will have a constant lenght according to the choices user select on the combo box the problem is, combo box is a string how can I interpret each string as a long integer so that I can apply those values for my line?

    and do you know how to set line color with option buttons (Radio Buttons);)

    thanks in advance

    [BORN_TO_BE_GOOD]
    GeneralRe: How can I use a CComboBox as a Lenght specifier?
    James R. Twine
    15:19 26 Apr '04  
    You can use the Item's Data to store the length of the line you need to draw. That way, you can still present the user with textual information (if you choose to show both text and a line), or just use the Item's Data for drawing a line in the client area.

    Setting line color, if I understand your question, would be as simple as waiting for a button to be pressed, and making the control change the color it uses to draw the lines. An additional COLORREF member would be how I would do it.

    Sorry that I cannot provide more information, or some example code (as I want to do) but I am working on-site now and currently find myself with little free time.

    Peace!

    -=- James (Sonork:100.21837)
    [Tip for SUV winter driving survival: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"!]
    [Get Delete FXP Files Now!]

    GeneralCan this combo box be made FLAT
    mangeshb123
    19:48 16 Oct '03  
    Hi,
    Thats great to see color combo box, as i needed.
    Thanks for it.

    I want to know , can this combo be made to look flat like (XP) style ?
    Please reply.
    GeneralRe: Can this combo box be made FLAT
    James R. Twine
    4:30 17 Oct '03  
       The best (not the easiest) way I can think to do this is to take an existing implementation of a flat combo box (there are several out there), and simply "drop in" my item drawing code.

       It should not be that hard to do...

       Peace!

    -=- James (Sonork:100.21837)
    [Tip for SUV winter driving survival: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"!]
    [Get Check Favorites 1.6 Now!]

    GeneralHow to initialize with my own colors? I'd add few methods..
    andru123
    1:16 6 Nov '02  
    I need to use my custom colors, and a few methods would be useful for me, I think:
    void RemoveAllColors();


    btw, is control being updated?
    GeneralRe: How to initialize with my own colors? I'd add few methods..
    James R. Twine
    7:58 8 Aug '03  
    I need to use my custom colors, and a few methods would be useful for me, I think: void RemoveAllColors();
       You could just not call either of the stock Initialize... functions thus resulting in no colors needing to be removed and add your own.  Although a new function to remove them all may be useful.

    btw, is control being updated?
       Yes, but only when I fix or add something.

       Peace!


    -=- James (Sonork:100.21837)
    [Tip for SUV winter driving survival: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"!]
    [Get Check Favorites 1.6 Now!]

    GeneralHow to Create a ComboBox in toolbar?
    white jungle
    4:27 16 Nov '01  
    I want to create a comboBox in toolbar,But I don't know how to!
    hope someone can help me!


    Don't Look At Me That Way!
    GeneralRe: How to Create a ComboBox in toolbar?
    Carlos Antollini
    4:50 16 Nov '01  
    You need to create a Dialog bar, is easy.


    best Regards And Happy Friday Wink

    Carlos Antollini.
    GeneralRe: How to Create a ComboBox in toolbar?
    tvhead80
    18:32 21 Mar '03  

    Is it impossible to create a ComboBox in toolbar without creating a Dialog bar?

    to be Ace.
    GeneralRe: How to Create a ComboBox in toolbar?
    left1none
    0:10 7 Feb '06  
    You can create combobox in toolbar. I have one right now. But the easiest way is to use dialog bar.


    Last Updated 19 Oct 2000 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010