www.pudn.com > mfc资源大全1.rar > button_color_picker2.shtml




   
   
   Controls - A Color Picker


A Color Picker


This article was contributed by Luis Ortega

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:

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:

Download Source.

Posted on: April 11, 98.


Goto HomePage
© 1998 Zafir Anjum 
Contact me: zafir@home.com