Click here to Skip to main content
Licence 
First Posted 3 Aug 2004
Views 101,199
Bookmarked 53 times

CColorBox

By | 3 Aug 2004 | Article
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.

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 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

PEK



Sweden Sweden

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionPermissions PinmemberMember 5884788:05 20 Dec '10  
AnswerRe: Permissions PinmemberPEK23:01 27 Dec '10  
GeneralDetailed instructions on how to use buttons PinmemberDavenish5:44 10 Apr '07  
GeneralException Error Pinmemberdanielrgeddes21:34 20 Sep '06  
GeneralRe: Exception Error PinmemberPEK8:22 5 Dec '06  
JokeI think another way is better PinmemberWilliamZhou15:50 18 Jan '06  
GeneralRe: I think another way is better PinmemberPEK0:06 21 Jan '06  
GeneralFlat Button PinmemberErwin_Cebu19:16 16 Feb '05  
GeneralRe: Flat Button PinmemberPEK9:24 17 Feb '05  
GeneralRe: Flat Button PinmemberErwin_Cebu12:16 17 Feb '05  
GeneralCDialogBar & custom buttons like CColorBox PinmemberSammmmmmmmmmy0:19 5 Oct '04  
GeneralRe: CDialogBar & custom buttons like CColorBox PinmemberPEK7:26 5 Oct '04  
GeneralI get scared... Pinmembernorm.net0:28 4 Aug '04  
GeneralRe: I get scared... PinmemberPEK4:11 4 Aug '04  
GeneralRe: I get scared... PinmemberYoSilver13:27 5 Aug '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 4 Aug 2004
Article Copyright 2004 by PEK
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid