How to use the System palette CColorDialog

Source: Internet
Author: User
Tags getcolor
1. Data membersCCOLORDIALOG::M_CC Choosecolor m_cc; The settings used to customize the dialog box for the structure. NotesIn the construction CColorDialogobject, you can use the M_CCSet the various aspects of the dialog box before calling the DoModal member function. ExampleThe code below uses CCOLORDIALOG::M_CC data member to//customize the settings of CColorDialog. The CColorDialog'll/shown as full open and with red as the selected color. CColorDialog Dlg; DLG.M_CC. Flags |= Cc_fullopen | Cc_rgbinit; Dlg.m_cc.rgbResult = RGB (255, 0, 0); Dlg. DoModal (); 2. ConstructorsCcolordialog::ccolordialog CColorDialog (Colorref clrinit = 0, DWORD dwFlags = 0, cwnd* pparentwnd = NULL); Constructs a CColorDialog object. ParametersLrinit the default color selection. If no value is specified, the default value is RGB (0,0,0) (black). DwFlags sets the function and appearance of the custom dialog box flag. For more information, see the CHOOSECOLOR structure of the Windows SDK. pParentWnd the pointer to the parent or owner window of the dialog box. ExampleShow the Color dialog with all the default settings. CColorDialog Dlg1; Dlg1. DoModal (); Show the fully opened color dialog with red as the selected color. CColorDialog Dlg2 (RGB (255, 0, 0), Cc_fullopen); Dlg2. DoModal (); 3. Operation function 1, CColorDialog::D omodal virtual int DoModal (); Displays the Color dialog box and allows the user to make a selection. return value IDOKOr IDCANCEL。 If IDCANCELReturns, the Windows Commdlgextendederror function is called to determine whether an error has occurred. IDOKAnd IDCANCELIs the constant indicating whether the user has selected the or Cancel button. NotesIf you want to initialize the various Color dialog box options by setting members of the M_CC structure, you should call the DoModalBefore you do this, but in the dialog box, after you construct the object. In the call DoModal, you can call other member functions to enter the dialog box by setting or retrieving information from the user. [3] 2.Ccolordialog::getcolor COLORREF GetColor () const; Returns the value that contains the selected color COLORREFStructure. return valueThe COLORREF value of the RGB information that contains the color is selected in the Color dialog box. Example/Get The selected color from the CColorDialog. CColorDialog Dlg; if (dlg. DoModal () = = IDOK) {COLORREF color = dlg. GetColor (); TRACE (_t ("RGB value of the selected color-red =%u,") _t ("green =%u, blue =%u\n"), Getrvalue (color), getgvalue (color) , Getbvalue (color)); } 3.Ccolordialog::getsavedcustomcolors Static COLORREF * Getsavedcustomcolors (); Retrieves a user-created custom color. return valueTo store a custom color pointer, 16 RGB color values are created by the user. Notes getsavedcustomcolorsThe member functions provide a pair of these colors. return at DoModal IDOKAfter that, these colors can be retrieved. In the returned array, each value of 16 RGB is initialized to RGB (255,255,255) (white). User-selected Custom colors are saved only between the dialog box calls in the application. If you want to save these two colors between application calls, you must put them in some other way, such as initialize (. INI) file. ExampleGet A pointer to an array of all RGB color values that stores//custom colors created by the user from CColorDialog. CColorDialog Dlg; if (dlg. DoModal () = = IDOK) {colorref* Ccolor = dlg. Getsavedcustomcolors (); for (int i=0; i <; i++) {TRACE (_t ("RGB value of the selected color-red =%u,") _t ("green =%u, blue =%u\n"), Ge Trvalue (Ccolor[i]), Getgvalue (Ccolor[i]), Getbvalue (Ccolor[i])); } }
  
4.Ccolordialog::setcurrentcolor void Setcurrentcolor (COLORREF CLR); Forces the current color selection to the specified color. ParametersThe CLR RGB color value. NotesThis feature is called from inside a message handler or OnColorOK. The dialog box automatically updates the user's selection based on the CLR parameter values. 4. PropertiesCcolordialog::oncolorok virtual BOOL OnColorOK (); Validates the color of the Override Join dialog box. return valueNonzero if the dialog box should not be closed, otherwise the accept input color is 0. NotesOverride this function only if you want to provide custom validation for the user to select a color in the Color dialog box. The user can choose the color in one of the following two ways: Click on the color palette, select the color in the edit box, enter an RGB value typically, you do not need to use this feature because the framework provides the default validation of colors and displays a message box if an invalid color is encoded. You can call Setcurrentcolor from OnColorOKForce color selection. In OnColorOKIs fired (that is, if the user clicks accept color changes), you can call GetColor to get the RGB value for the new color. ExampleOverride OnColorOK to validate the color entered to the//Red, Green, and Blue edit controls. If the color//is BLACK (i.e. RGB (0, 0,0)) and then force the current color//selection to be the color initially selected When the//dialog box is created. The color dialog won ' t close so//user can enter a new color. BOOL Cmycolordlg::oncolorok () {//Value in Red Edit control. COLORREF clrref = GetColor (); if (RGB (0, 0, 0) = = clrref) {AfxMessageBox (_t ("BLACK is not a acceptable color.") _t ("Please enter a color again");// GetColor () returns initially selected color. Setcurrentcolor (GetColor ()); Won ' t dismiss Color dialog. return TRUE; }//OK to dismiss color dialog. return FALSE; } 5. Notes CColorDialog The   object is a dialog box with a list that displays the system-defined colors. When the dialog box exits, the user can select or create a special color from the list and report back to the application. Constructs a CColorDialog object you can use a constructor in a class, or you can derive a subclass from class CColorDialog, and then define your own constructor. After you have constructed the CColorDialog object, you can set or modify any of the values in the M_CC structure to initialize the value of the dialog box control. M_CC is a choosecolor type of structure. After you initialize the dialog box control, you can call the member function DoModal to display the dialog box and allow the user to select a color. DoModal returns the user's choice of the button OK or cancel (the return value is idok,idcancel in turn). If DoModal returns IDOK, you can call a member function of CColorDialog to retrieve the color information selected by the user. You can use the Windows function Commdlgextendederror to detect whether an error occurred during the initialization of the dialog box and to obtain information about the error. Class CColorDialog requires the use of COMMDLG.DLL files provided by Windows3.1 or later. Customizing dialog boxes, you can derive a subclass from CColorDialog, provide a custom dialog template, and add a message map to handle notifications sent by the extended control. All suspended messages will be sent to the base class. No custom hook functions are required. Note: In some versions, if other CDialog objects are dimmed in the frame, the CColorDialog object will not appear with a gray background. Required header files: #include <afxdlgs.h> [

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.