Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC
Article

CColorBox

Rate me:
Please Sign up or sign in to vote.
4.55/5 (17 votes)
3 Aug 2004Ms-RL2 min read 130.5K   5.2K   57   15
A very simple button that shows a color and lets the user change it by clicking on the button.

CColorBox

Introduction

CColorBox is a very, very simple control. It shows a color, and when the user clicks on it, the standard color selector in Windows pops up to let the user change the color. It is not much more complicated than that :-).

Background

I once needed a control that shows a color (like the color boxes in the color selector in Windows). Since I didn't find something like that, I tried to do it myself. If you haven't done your own control, just like me before this project, maybe this code is useful. It's small and, hopefully, quite easy to understand.

Using the code

CColorBox is very simple to use. To add a CColorBox in your project, do these:

  • Add colorbox.cpp and colorbox.h to your project.
  • Select a button in the resource editor and add a CButton variable for this control.
  • Change the variable type from CButton to CColorBox in the header file.

That's all. Now, it's time to look at the functions you can use.

void SetColor(const COLORREF newColor);
COLORREF GetColor() const;

I assume that these functions don't need much of an explanation :-). Call SetColor to set the color, and GetColor to get the color.

C++
BOOL SelectColor();
void SetCustomColors(COLORREF *customcolors);

If you call SelectColor, the color selector in Windows (CColorDialog) pops up and lets the user change the color. If the user changes the color, the function returns TRUE. In the color selector, the user has the ability to define 16 own colors. If you want to specify and save these colors, you should call SetCustomColors with a pointer to at least 16 COLORREF. The custom colors will then be saved in this memory area.

void SetAllowChange(const BOOL allowchange);
BOOL GetAllowChange() const;

As default, the user can change the color when he clicks on the button (SelectColor is called). If you want to disable this behavior, call SetAllowChange(FALSE).

void SetSelected(const BOOL selected);
BOOL GetSelected() const;

Look at the screen shot. Button 2 has a darker border than the other buttons. That means, it is "Selected". If you look at the boxes in the color selector, you see that the boxes have a similar feature. So, if you want to have a dark border around the button, call SetSelected(TRUE).

History

  • 4 August, 2004 - Initial version.

License

This article, along with any associated source code and files, is licensed under Microsoft Reciprocal License


Written By
PEK
Sweden Sweden
PEK is one of the millions of programmers that sometimes program so hard that he forgets how to sleep (this is especially true when he has more important things to do). He thinks that there are not enough donuts in the world. He likes when his programs works as they should do, but dislikes when his programs is more clever than he is.

Comments and Discussions

 
QuestionPermissions Pin
Member 58847820-Dec-10 8:05
Member 58847820-Dec-10 8:05 
AnswerRe: Permissions Pin
PEK27-Dec-10 23:01
PEK27-Dec-10 23:01 
GeneralDetailed instructions on how to use buttons Pin
Davenish10-Apr-07 5:44
Davenish10-Apr-07 5:44 
GeneralException Error Pin
danielrgeddes20-Sep-06 21:34
danielrgeddes20-Sep-06 21:34 
GeneralRe: Exception Error Pin
PEK5-Dec-06 8:22
PEK5-Dec-06 8:22 
JokeI think another way is better Pin
WilliamZhou18-Jan-06 15:50
WilliamZhou18-Jan-06 15:50 
GeneralRe: I think another way is better Pin
PEK21-Jan-06 0:06
PEK21-Jan-06 0:06 
GeneralFlat Button Pin
Erwin_Cebu16-Feb-05 19:16
Erwin_Cebu16-Feb-05 19:16 
How can we change the button's appearance from Push-like to Flat type. Thanks.

Best Regards,

Erwin
GeneralRe: Flat Button Pin
PEK17-Feb-05 9:24
PEK17-Feb-05 9:24 
GeneralRe: Flat Button Pin
Erwin_Cebu17-Feb-05 12:16
Erwin_Cebu17-Feb-05 12:16 
GeneralCDialogBar & custom buttons like CColorBox Pin
Zeek25-Oct-04 0:19
Zeek25-Oct-04 0:19 
GeneralRe: CDialogBar & custom buttons like CColorBox Pin
PEK5-Oct-04 7:26
PEK5-Oct-04 7:26 
GeneralI get scared... Pin
NormDroid4-Aug-04 0:28
professionalNormDroid4-Aug-04 0:28 
GeneralRe: I get scared... Pin
PEK4-Aug-04 4:11
PEK4-Aug-04 4:11 
GeneralRe: I get scared... Pin
YoSilver5-Aug-04 13:27
YoSilver5-Aug-04 13:27 

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.