Click here to Skip to main content
13,349,539 members (48,245 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 3 Aug 2004


, 3 Aug 2004
Rate this:
Please Sign up or sign in to vote.
A very simple button that shows a color and lets the user change it by clicking on the button.



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 :-).


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.

<PRE lang=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).


  • 4 August, 2004 - Initial version.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

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.

You may also be interested in...

Comments and Discussions

QuestionPermissions Pin
Member 58847820-Dec-10 9:05
memberMember 58847820-Dec-10 9:05 
AnswerRe: Permissions Pin
PEK28-Dec-10 0:01
memberPEK28-Dec-10 0:01 
GeneralDetailed instructions on how to use buttons Pin
Davenish10-Apr-07 6:44
memberDavenish10-Apr-07 6:44 
GeneralException Error Pin
danielrgeddes20-Sep-06 22:34
memberdanielrgeddes20-Sep-06 22:34 
GeneralRe: Exception Error Pin
PEK5-Dec-06 9:22
memberPEK5-Dec-06 9:22 
JokeI think another way is better Pin
WilliamZhou18-Jan-06 16:50
memberWilliamZhou18-Jan-06 16:50 
GeneralRe: I think another way is better Pin
PEK21-Jan-06 1:06
memberPEK21-Jan-06 1:06 
GeneralFlat Button Pin
Erwin_Cebu16-Feb-05 20:16
memberErwin_Cebu16-Feb-05 20:16 
GeneralRe: Flat Button Pin
PEK17-Feb-05 10:24
memberPEK17-Feb-05 10:24 
GeneralRe: Flat Button Pin
Erwin_Cebu17-Feb-05 13:16
memberErwin_Cebu17-Feb-05 13:16 
GeneralCDialogBar &amp; custom buttons like CColorBox Pin
Sammmmmmmmmmy5-Oct-04 1:19
memberSammmmmmmmmmy5-Oct-04 1:19 
GeneralRe: CDialogBar &amp; custom buttons like CColorBox Pin
PEK5-Oct-04 8:26
memberPEK5-Oct-04 8:26 
GeneralI get scared... Pin
norm.net4-Aug-04 1:28
membernorm.net4-Aug-04 1:28 
GeneralRe: I get scared... Pin
PEK4-Aug-04 5:11
memberPEK4-Aug-04 5:11 
GeneralRe: I get scared... Pin
YoSilver5-Aug-04 14:27
memberYoSilver5-Aug-04 14:27 
Agree. Intelligent programs ARE very annoying. Just look at the behavior of the help viewer in MS Office 2000... it kills me!!!

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 4 Aug 2004
Article Copyright 2004 by PEK
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid