Win32 call system color dialog box, win32 call dialog box

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.