Click here to Skip to main content
Click here to Skip to main content

CColorBox

By , 3 Aug 2004
Rate this:
Please Sign up or sign in to vote.

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

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 Smile | :) . 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
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 PinmemberMember 58847820-Dec-10 8:05 
AnswerRe: Permissions PinmemberPEK27-Dec-10 23:01 
GeneralDetailed instructions on how to use buttons PinmemberDavenish10-Apr-07 5:44 
GeneralException Error Pinmemberdanielrgeddes20-Sep-06 21:34 
GeneralRe: Exception Error PinmemberPEK5-Dec-06 8:22 
JokeI think another way is better PinmemberWilliamZhou18-Jan-06 15:50 
GeneralRe: I think another way is better PinmemberPEK21-Jan-06 0:06 
GeneralFlat Button PinmemberErwin_Cebu16-Feb-05 19:16 
GeneralRe: Flat Button PinmemberPEK17-Feb-05 9:24 
Look in CColorBox::DrawItem where the border is drawn. DrawFrameControl is used to do this, and I think you only need to slightly change the last value to get a flat button.
GeneralRe: Flat Button PinmemberErwin_Cebu17-Feb-05 12:16 

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.

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