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.