A Color Picker

This color picker behaves just like the ones in the "Appearance" tab of the Display properties dialog. Here is how the button looks like:

The picker has a "most recently used colors" feature. If you click on the "Other" button and select a color that is not in the current set, the least recently used color gets replaced.

It is very easy to use.

How to include in your project

  1. Copy ColorBtn.cpp and ColorBtn.h into your project directory.
  2. Copy ColorBtn.rc into your res directory.
  3. Add this line to your rc2 file:
    #include "ColorBtn.rc"
  4. Add ColorBtn.cpp to your project.

How to use

  1. Create a button on your dialog. Mark it as "Owner Draw".
  2. Declare a variable in your dialog class, like this:
    CColorBtn colorbtn;
  3. At OnInitDialog, call SubclassDlgItem, like this:
    colorbtn.SubclassDlgItem(IDC_BUTTON,this); // Use your button ID here
  4. Repeat the process for each button you want. You'll end up with as many member variables as buttons.

You can set and read the selected color using the "currentcolor" member variable.

How to make your color table persistent

When the buttons start up, the color table is set to the standard 20 static colors. After using them for a while, those colors might have changed. If you want to preserve the modified color table, follow one of the these procedures.

In a single application, all buttons share a single color table. Because of this, you don't have to do this for each button. Just use the static version of the desired function once (e.g. call CColorBtn::Load()).

There are two ways to make your color table persistent. Application-wise and Document-wise.

Application-wise means that you store the color table in the registry. This way, all documents share the same color table. CColorBtn provides two static functions to do this:

  • CColorBtn::Load()
    Gets the colors from the registry
  • CColorBtn::Save()
    Stores the colors in the registry

These functions require that you call SetRegistryKey() before using them.

Document-wise means that you store the color table in a file along with the rest of the document data. This way, each document has its own color table. Note that even in Document-wise fashion, all the buttons you use in a single application share their color table (this was at the artists' petition, I don't know, they seem to like it this way). CColorBtn provides two functions to do this:

  • Serialize(CArchive &ar)
    Serializes the color table, you call this in your document's Serialize() function.
  • Reset()
    Resets the color table back to the standard 20 static colors. You call this in your document's OnNewDocument() function.

Download Source.



Comments

  • It's Fantabulous!!!

    Posted by Legacy on 01/06/2004 12:00am

    Originally posted by: C++ Lover

    It's Fantabulous!!!
    

    Reply
  • You are wonderful :)

    Posted by Legacy on 09/26/2003 12:00am

    Originally posted by: JokerHata

    I think it so long~!
    
    you show me a great code and method to resolve it
    Thank you very much~!

    Reply
  • Thanks!

    Posted by Legacy on 05/20/2003 12:00am

    Originally posted by: Petras

    Thank you, you saved me some time too :)

    Reply
  • Nice! & Thanks!

    Posted by Legacy on 03/04/2003 12:00am

    Originally posted by: Shin Hyun Sup

    Thanks!

    Reply
  • Pop-up doesn't appear below button as shown

    Posted by Legacy on 11/08/2002 12:00am

    Originally posted by: Xeon


    Now, the screen shot of this article shows the pop-up color palette below the button neatly. But in my program, this pop-up actually show up at some other position instead!

    The thing I wanna know is : is there anyway we can change the position that this pop-up color palette show up?

    Please and thanks a lot!! :--)
    Xeon.

    Reply
  • Pop-up doesn't appear below button as shown

    Posted by Legacy on 11/08/2002 12:00am

    Originally posted by: Xeon


    Now, the screen shot of this article shows the pop-up color palette below the button neatly. But in my program, this pop-up actually show up at some other position instead!

    The thing I wanna know is : is there anyway we can change the position that this pop-up color palette show up?

    Please and thanks a lot!! :--)
    Xeon.

    Reply
  • support for multiple monitors...

    Posted by Legacy on 06/12/2002 12:00am

    Originally posted by: rakka rage

    when i use this control on my second monitor the popup appears at the edge of my first monitor. i was wondering if anyone has resolved this problem?

    thanks.

    Reply
  • The right place to call colorbtn.SubclassDlgItem(IDC_BUTTON1,this);

    Posted by Legacy on 04/25/2002 12:00am

    Originally posted by: Rick yu

    I believe that the virtual function virtual "void PreSubclassWindow()" of CDialog is the right place to call colorbtn.SubclassDlgItem(IDC_BUTTON1,this).

    Enjoy!

    By the way, the colorpick class is great! Thank you, Luis Ortega. You are really someone!

    Reply
  • Using this control in a CDialogBar

    Posted by Legacy on 04/23/2002 12:00am

    Originally posted by: marcela

    Has anybody used this control in a CDialogBar? I can't make it work; buttons just don't appear in the dialog.
    
    

    Any help would be really appreciated!

    Thanks!

    Reply
  • Hide member

    Posted by Legacy on 02/15/2002 12:00am

    Originally posted by: Enrico S. Penetti

    Very nice ColorButton.

    I have a suggestion for your Button, to handle the color outside button not with the member from the button.
    Make the variable COLORREF currentcolor protected and overload the operator = and COLORREF like

    operator=(const COLORREF &color)
    {
    currentcolor = color;
    }
    operator COLORREF()
    {
    return currentcolor;
    }

    Then you can use the Button as follows:

    COLORREF color = DEFAULT_COLOR;
    CColorBtn m_btnColor = color;
    ...
    color = m_btnColor;

    sincerely
    Enrico

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds