Color, Link and Image Editor Controls for MFC - More Colors
(Page 3 of 5 )
The color bar is a large frame that hosts multiple colors and maybe two buttons (automatic and other) that will choose a default color or bring up the color dialog.
Method | What it does |
ContextToSize | Calculates the size needed for the buttons and the colors, and then adjusts to this. A sort of size to content. |
CreateControl | Handles the create process and resize of the control to contain the colors. |
Create | The same as CreateControl, but does not resize. |
EnableAutomaticButton | Show/hide this. |
EnableOtherButton | Show/hide this. |
GetColor | Currently selected color. |
GetCommandID | Returns the command ID of the current color bar control. |
GetHighlightedColor | Tells what color is used to signal the selected state of a color. |
GetHorzMargin | Space between the client area boundary and left/right color cell. |
GetVertMargin | Space between the client area boundary and top/bottom color cell. |
IsTearOff | Is the color bar dockable? |
SetColor | Sets the currently selected color. |
SetColorName | Sets the name for a specified color. |
SetCommandID | Sets a new command ID for a color bar. |
SetDocumentColors | Sets a list of controls to display inside the color bar. |
SetHorzMargin | Set the horizontal margin. |
SetVertMargin | Sets the vertical margin. |
CStatic m_wndColorBarFrame;
CMFCColorBar m_wndColorBar;
...
// Create regular color bar:
CRect rectColorBar;
m_wndColorBarFrame.GetClientRect(&rectColorBar);
//transform this point to the context of the window
m_wndColorBarFrame.MapWindowPoints(this, &rectColorBar);
m_wndColorBar.SetHorzMargin(0);
m_wndColorBar.SetVertMargin(0);
m_wndColorBar.EnableOtherButton(_T("Other..."));
m_wndColorBar.CreateControl(this, rectColorBar, IDC_COLORBAR, 5 /* columns */);
m_wndColorBar.SetColor(RGB(0, 0, 0));

Finally, call the same GetColor method to find out the chosen color:
COLORREF color = m_wndColorBar.GetColor();
Using a custom palette is a little more complicated. You need to create a logical palette and fill it one by one with the elements that you want to put inside it. To do this we will use a cycle and some shift operations.
// define a structure where to define the colors
#define NUM_COLOURS 64
struct
{
LOGPALETTE LogPalette;
PALETTEENTRY PalEntry[NUM_COLOURS];
}pal;
//create a logical palette -> and set version +color numbers
LOGPALETTE* pLogPalette = (LOGPALETTE*) &pal;
pLogPalette->palVersion = 0x300;
pLogPalette->palNumEntries = (WORD) NUM_COLOURS;
// Go from color to color
COLORREF colorStart = RGB(0, 0, 255);
COLORREF colorFinish = RGB(255, 255, 255);
int nShift = 6;
// Generate the colors
for (int i = 0; i < NUM_COLOURS; i++)
{ BYTE bR = (BYTE)((GetRValue(colorStart) *(NUM_COLOURS - i) + GetRValue(colorFinish) * i) >> nShift);
BYTE bG = (BYTE)((GetGValue(colorStart) *(NUM_COLOURS - i) + GetGValue(colorFinish) * i) >> nShift);
BYTE bB = (BYTE)((GetBValue(colorStart) *(NUM_COLOURS - i)
+ GetBValue(colorFinish) * i) >> nShift);
//set the new color combination
pLogPalette->palPalEntry[i].peRed = bR;
pLogPalette->palPalEntry[i].peGreen = bG;
pLogPalette->palPalEntry[i].peBlue = bB;
pLogPalette->palPalEntry[i].peFlags = 0; }
//Create the palette
m_palColorPicker.CreatePalette(pLogPalette);
//Set the margins and create the control with our palette
m_wndColorBar2.SetHorzMargin(0);
m_wndColorBar2.SetVertMargin(0);
m_wndColorBar2.CreateControl(this, rectColorBar, IDC_COLORBAR2, -1 /* columns */, &m_palColorPicker);
//set the selected control
m_wndColorBar2.SetColor(colorStart);

Next: The color picker control >>
More .NET Articles
More By Gabor Bernat