다음을 통해 공유


CColorDialog::OnColorOK

Override to validate the color entered into the dialog box.

virtual BOOL OnColorOK( );

Return Value

Nonzero if the dialog box should not be dismissed; otherwise 0 to accept the color that was entered.

Remarks

Override this function only if you want to provide custom validation of the color the user selects in the color dialog box.

The user can select a color by one of the following two methods:

  • Clicking a color on the color palette. The selected color's RGB values are then reflected in the appropriate RGB edit boxes.

  • Entering values in the RGB edit boxes

Overriding OnColorOK allows you to reject a color the user enters into a common color dialog box for any application-specific reason.

Normally, you do not need to use this function because the framework provides default validation of colors and displays a message box if an invalid color is entered.

You can call SetCurrentColor from within OnColorOK to force a color selection. Once OnColorOK has been fired (that is, the user clicks OK to accept the color change), you can call GetColor to get the RGB value of the new color.

Example

// Override 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)), 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 an 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;                          
}

Requirements

Header: afxdlgs.h

See Also

Reference

CColorDialog Class

Hierarchy Chart