Reference website: http://blog.csdn.net/u013242177/article/details/50437358
- First, include the commdlg.h header file, which is the header file for the common dialog, including the file dialog, the color dialog, the Print dialog box, and so on.
- It then declares a variable of type Choosecolor, declaring a variable rgblinecolor of type COLORREF, which is used to store the color selected by the user.
Staticstatic colorref Rgblinecolor;
Where Choosecolor is a struct, including some information about initializing the Color dialog box with the Choosecolor function, you can find the structure from MSDN as follows:
typedef struct {
DWORD lStructSize;
HWND hWndOwner;
HWND hinstance;
COLORREF Rgbresult;
colorref* lpcustcolors;
DWORD Flags;
LPARAM Lcustdata;
Lpcchookproc Lpfnhook;
LPCTSTR Lptemplatename;
} Choosecolor;
- And then assign a value to the contents of the 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;
- Finally, use the Choosecolor function to create a color dialog box for the user to select a color
if (Choosecolor (&= stchoosecolor.rgbresult;}
Get the RGB component values in COLORREF:
COLORREF Colorrrefrgb =rgb (+ = = Getbvalue ( COLORRREFRGB);
code example:
#include"commdlg.h"......................................voidCmydialogt::onbnclickedbuttonpickcolor () {//Todo:add your control notification handler code here StaticChoosecolor Stchoosecolor; StaticCOLORREF Rgblinecolor; StaticCOLORREF dwcustcolors[ -]; 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); }}
Operation Result:
Win32 Call System Colors dialog box