![]() |
Desktop Development »
Miscellaneous »
Colour Selection Controls
Beginner
License: The Code Project Open License (CPOL)
XColorDialog - an MFC color picker control that displays a color hexagon and a color spectrumBy Hans DietrichXColorDialog displays a color hexagon and a color spectrum that allows user selection, and provides APIs for color based on RGB and HSL color models. |
C++ (VC6, VC8.0), Windows, Visual Studio (VS2005), MFC, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
![]() |
![]() |
This article is based on my articles XColorHexagonCtrl and XColorSpectrumCtrl.
For a detailed description of the user interface behaviors of the two tabs in XColorDialog, please see my articles XColorHexagonCtrl and XColorSpectrumCtrl.
XColorDialog is implemented using standard MFC CDialog
and CTabCtrl. Two tab pages host the color controls, which
communicate with the parent dialog via registered window messages.
The parent dialog then keeps the color control on the other tab page
informed of currently selected color, and updates
color display on the parent dialog:
![]() |
|
![]() |
The Custom tab includes the same controls as the MS Office® dialog, allowing user to select the color model (RGB or HSL) and values:
| Function | Description |
|---|---|
| COLORREF GetColor() | Retrieves RGB value for current color |
| int GetColorModel() | Retrieves color model setting from Custom tab |
| int GetCurTab() | Retrieves current tab |
| void GetHSL(BYTE *h, BYTE *s, BYTE *l) | Retrieves HSL values for current color |
| COLORREF GetRGB() | Retrieves RGB value for current color |
| CXColorDialog& SetColorModel(int nColorModel) | Sets starting color model for Custom tab |
| CXColorDialog& SetCurrentColor(COLORREF cr) | Sets the current color from RGB values |
| CXColorDialog& SetHSL(BYTE h, BYTE s, BYTE l) | Sets the current color from HSL values |
| CXColorDialog& SetRGB(COLORREF cr) | Sets the current color from RGB values |
| CXColorDialog& SetStartTab(int nStartTab) | Sets the starting tab |
| CXColorDialog& SetTitle(LPCTSTR lpszTitle) | Sets the title of the dialog box |
| CXColorDialog& SetTooltipFormat(int nFormat) | Sets the tooltip format for the CXColorHexagonCtrl and CXColorSpectrumCtrl controls |
WM_ERASEBKGND handler to each tab page,
so that I could paint the background with GDI function
GradientFill():
BOOL CTabStandard::OnEraseBkgnd(CDC* pDC)
{
CRect rectClient;
GetClientRect(&rectClient);
TRIVERTEX vert[2];
vert[0].x = 0;
vert[0].y = 0;
vert[0].Red = (COLOR16) (GetRValue(m_crStartColor) << 8);
vert[0].Green = (COLOR16) (GetGValue(m_crStartColor) << 8);
vert[0].Blue = (COLOR16) (GetBValue(m_crStartColor) << 8);
vert[0].Alpha = 0x0000;
vert[1].x = rectClient.right;
vert[1].y = rectClient.bottom;
vert[1].Red = (COLOR16) (GetRValue(m_crEndColor) << 8);
vert[1].Green = (COLOR16) (GetGValue(m_crEndColor) << 8);
vert[1].Blue = (COLOR16) (GetBValue(m_crEndColor) << 8);
vert[1].Alpha = 0x0000;
GRADIENT_RECT rect;
rect.UpperLeft = 0;
rect.LowerRight = 1;
::GradientFill(pDC->m_hDC, vert, 2, &rect, 1, GRADIENT_FILL_RECT_V);
return TRUE; //CDialog::OnEraseBkgnd(pDC);
}
Here is what the Standard tab looks like with gradient fill:
CFormView or
CPropertyPage.
To integrate CXColorDialog into your app, you first need to
add following files to your project:
The .cpp files marked with † should be set to Not using precompiled header in Visual Studio. Otherwise, you will get error
fatal error C1010: unexpected end of file while looking for precompiled header directive
For Visual Studio 6 - go to View | Resource Includes... and in bottom listbox, scroll down to end. Insert #include "XColorDialog.rc" right before #endif:
|
|
|
For Visual Studio 2005 - right-click .rc file in Resource View, then choose Resource Includes... from shortcut menu:
#include "XColorDialog.rc" right before #endif:
|
CMyColorDialog dlg(m_crCurrent, m_nTooltip | ((m_nStartTab+1) << 4)); dlg.SetTitle(_T("My Colors")); if (dlg.DoModal() == IDOK) { // < Add code here to handle selected color > }
CXColorDialog to make use of
virtual function OnColorOK():
class CMyColorDialog : public CXColorDialog { public: CMyColorDialog(COLORREF crInitial = 0, DWORD dwFlags = XCD_TOOLTIP_NONE | XCD_OPEN_HEXAGON, CWnd* pParent = NULL) : CXColorDialog(crInitial, dwFlags, pParent) { } virtual BOOL OnColorOK() { COLORREF cr = GetRGB(); if (cr == RGB(255,0,0)) { AfxMessageBox(_T("That color is not allowed.")); return 1; } return 0; } };
This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 5 Apr 2008 Editor: |
Copyright 2008 by Hans Dietrich Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |