Win32 call system color dialog box, win32 call dialog box
Reference: http://blog.csdn.net/u013242177/article/details/50437358
- First, you must include the commdlg. h header file. This is the header file of the general dialog box, including the file dialog box, color dialog box, and Print dialog box.
- Then declare a CHOOSECOLOR variable and a COLORREF variable rgbLineColor to store the selected color.
static CHOOSECOLOR stChooseColor; static COLORREF rgbLineColor;
Here, CHOOSECOLOR is a struct, including information about the color dialog box initialized using the ChooseColor function. You can find the struct in MSDN as follows:
Typedef struct {
DWORD lStructSize;
HWND hwndOwner;
HWND hInstance;
COLORREF rgbResult;
COLORREF * lpCustColors;
DWORD Flags;
LPARAM lCustData;
LPCCHOOKPROC lpfnHook;
LPCTSTR lpTemplateName;
} CHOOSECOLOR;
- Then assign a value to the content of stChooseColor.
stChooseColor.lStructSize = sizeof(CHOOSECOLOR) ;stChooseColor.hwndOwner = hWnd ;stChooseColor.rgbResult = rgbLineColor ;stChooseColor.lpCustColors = (LPDWORD) dwCustColors ;stChooseColor.Flags = CC_RGBINIT ;stChooseColor.lCustData = 0 ;stChooseColor.lpfnHook = NULL ;stChooseColor.lpTemplateName = NULL ;
- Finally, use the ChooseColor function to create a color dialog box for the user to choose a color.
if (ChooseColor(&stChooseColor)){ rgbLineColor = stChooseColor.rgbResult; }
Obtain the RGB component value in COLORREF:
COLORREF colorrrefRGB =RGB(120,250,110);BYTE r = GetRValue(colorrrefRGB);BYTE g = GetGValue(colorrrefRGB);BYTE b = GetBValue(colorrrefRGB);
Sample Code:
#include "commdlg.h"......................................void CmyDialogt::OnBnClickedButtonPickcolor(){ // TODO: Add your control notification handler code here static CHOOSECOLOR stChooseColor; static COLORREF rgbLineColor; static COLORREF dwCustColors[16]; stChooseColor.lStructSize = sizeof(CHOOSECOLOR) ; stChooseColor.hwndOwner = this->m_hWnd ; stChooseColor.rgbResult = rgbLineColor ; stChooseColor.lpCustColors = (LPDWORD) dwCustColors ; stChooseColor.Flags = CC_RGBINIT ; stChooseColor.lCustData = 0 ; stChooseColor.lpfnHook = NULL ; stChooseColor.lpTemplateName = NULL ; if (ChooseColor(&stChooseColor)) { rgbLineColor = stChooseColor.rgbResult; BYTE r = GetRValue(rgbLineColor); BYTE g = GetGValue(rgbLineColor); BYTE b = GetBValue(rgbLineColor); }}
Running result: