CColorDialog类封装了颜色对话框,此类允许您将颜色选择对话框合并到应用程序中。颜色对话框就像画家的调色板一样,可显示系统定义的颜色列表,用户可以从列表中选择或创建特定颜色。构造一个CColorDialog类对象后,可用DoModal( )函数来显示颜色对话框。
CColorDialog
的构造函数原型如下:
CColorDialog(COLORREF clrInit = 0, DWORD dwFlags = 0, CWnd* pParentWnd = NULL);
参数
clrInit
默认颜色选择。 如果未指定任何值,则默认值为 RGB (0,0,0) (黑色) 。
dwFlags
一组用于自定义对话框的函数和外观的标志。 有关详细信息,请参阅 Windows SDK 中的CHOOSECOLOR结构。
pParentWnd
指向对话框的父窗口或所有者窗口的指针。
除DoModal( )函数外,还有以下几个成员函数:
CColorDialog:: GetColor | 返回一个 COLORREF 结构,该结构包含选定颜色的值。 |
CColorDialog::GetSavedCustomColors | 检索用户创建的自定义颜色。 |
CColorDialog::SetCurrentColor | 强制当前颜色选择指定的颜色。 |
CCorlorDialog类有一个CHOOSECOLOR结构类型的公共数据成员m_cc, 可以使用 m_cc来实现,设置颜色对话框的初始选择颜色等。CHOOSECOLOR结构定义如下:
typedef struct {
DWORD lStructSize;
HWND hwndOwner;
HWND hInstance;
COLORREF rgbResult;
COLORREF * lpCustColors;
DWORD Flags;
LPARAM lCustData;
LPCCHOOKPROC lpfnHook;
LPCTSTR lpTemplateName;
} CHOOSECOLOR, *LPCHOOSECOLOR;
其成员功能描述如下(摘自MSDN Library):
lStructSize
Specifies the length, in bytes, of the structure.
hwndOwner
Handle to the window that owns the dialog box. This member can be any valid window handle, or it can be NULL if the dialog box has no owner.
hInstance
If the CC_ENABLETEMPLATEHANDLE flag is set in the Flags member, hInstance is a handle to a memory object containing a dialog box template. If the CC_ENABLETEMPLATE flag is set, hInstance is a handle to a module that contains a dialog box template named by the lpTemplateName member. If neither CC_ENABLETEMPLATEHANDLE nor CC_ENABLETEMPLATE is set, this member is ignored.
rgbResult
If the CC_RGBINIT flag is set, rgbResult specifies the color initially selected when the dialog box is created. If the specified color value is not among the available colors, the system selects the nearest solid color available. If rgbResult is zero or CC_RGBINIT is not set, the initially selected color is black. If the user clicks the OK button, rgbResult specifies the user's color selection.
To create a COLORREF color value, use the RGB macro.
lpCustColors
Pointer to an array of 16 COLORREF values that contain red, green, blue (RGB) values for the custom color boxes in the dialog box. If the user modifies these colors, the system updates the array with the new RGB values. To preserve new custom colors between calls to the ChooseColor function, you should allocate static memory for the array.
To create a COLORREF color value, use the RGB macro.
Flags
A set of bit flags that you can use to initialize the Color dialog box. When the dialog box returns, it sets these flags to indicate the user's input. This member can be a combination of the following flags.
Flag | Meaning |
---|---|
CC_ANYCOLOR | Causes the dialog box to display all available colors in the set of basic colors. |
CC_ENABLEHOOK | Enables the hook procedure specified in the lpfnHook member of this structure. This flag is used only to initialize the dialog box. |
CC_ENABLETEMPLATE | Indicates that the hInstance and lpTemplateName members specify a dialog box template to use in place of the default template. This flag is used only to initialize the dialog box. |
CC_ENABLETEMPLATEHANDLE | Indicates that the hInstance member identifies a data block that contains a preloaded dialog box template. The system ignores the lpTemplateName member if this flag is specified. This flag is used only to initialize the dialog box. |
CC_FULLOPEN | Causes the dialog box to display the additional controls that allow the user to create custom colors. If this flag is not set, the user must click the Define Custom Color button to display the custom color controls. |
CC_PREVENTFULLOPEN | Disables the Define Custom Colors button. |
CC_RGBINIT | Causes the dialog box to use the color specified in the rgbResult member as the initial color selection. |
CC_SHOWHELP | Causes the dialog box to display the Help button. The hwndOwner member must specify the window to receive the HELPMSGSTRING registered messages that the dialog box sends when the user clicks the Help button. |
CC_SOLIDCOLOR | Causes the dialog box to display only solid colors in the set of basic colors. |
lCustData
Specifies application-defined data that the system passes to the hook procedure identified by the lpfnHook member. When the system sends the WM_INITDIALOG message to the hook procedure, the message's lParam parameter is a pointer to the CHOOSECOLOR structure specified when the dialog was created. The hook procedure can use this pointer to get the lCustData value.
lpfnHook
Pointer to a CCHookProc hook procedure that can process messages intended for the dialog box. This member is ignored unless the CC_ENABLEHOOK flag is set in the Flags member.
lpTemplateName
Pointer to a null-terminated string that names the dialog box template resource in the module identified by the hInstance member. This template is substituted for the standard dialog box template. For numbered dialog box resources, lpTemplateName can be a value returned by the MAKEINTRESOURCE macro. This member is ignored unless the CC_ENABLETEMPLATE flag is set in the Flags member.
示例(基于演示文件对话框所创建的单文档工程):
1. 在IDR_MAINFRAME 菜单文件中新建“ColorDialogTest”菜单,及子菜单“Set Color”、“Draw Line”,如下:
2. 在视图类中添加变量mSelCorlor,如下:
3. 为“SetCorlor”添加事件处理程序,如下:
代码如下:
void CFileDialogTestView::OnSetColor()
{
// TODO: 在此添加命令处理程序代码
CColorDialog cdlg(mSelColor);
cdlg.m_cc.rgbResult = mSelColor;
if (cdlg.DoModal()==IDOK)
{
mSelColor = cdlg.GetColor();
}
}
4. 为菜单“Draw Line”添加事件处理函数,如下:
代码如下:
void CFileDialogTestView::OnDrawLine()
{
// TODO: 在此添加命令处理程序代码
drawType = 2;
}
5. 在视图类中添加变量endPoint及bDraw,如下:
6. 在OnLButtonDown(UINT nFlags, CPoint point)消息处理函数中加入“case 2:”的对应代码如下:
void CFileDialogTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this);
switch(drawType)
{
case 1:
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
CreateSolidCaret(tm.tmAveCharWidth / 8, tm.tmHeight);
//CreateSolidCaret(mLogfont.lfWidth/8, mLogfont.lfHeight);
SetCaretPos(point);
ShowCaret();
startPoint = point;
mstr.Empty();
break;
case 2:
startPoint = point;
break;
default:
break;
}
CView::OnLButtonDown(nFlags, point);
}
7. 在视图类中添加OnMouseMove(UINT nFlags, CPoint point)消息处理函数,代码如下:
void CFileDialogTestView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (bDraw == true)
{
CClientDC dc(this);
INT oldMode = dc.SetROP2(R2_NOT);
dc.MoveTo(startPoint);
dc.LineTo(endPoint);
endPoint = point;
dc.MoveTo(startPoint);
dc.LineTo(endPoint);
dc.SetROP2(oldMode);
}
CView::OnMouseMove(nFlags, point);
}
8.在视图类中添加OnLButtonUp(UINT nFlags, CPoint point)消息处理函数,代码如下:
void CFileDialogTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this);
switch(drawType)
{
case 1:
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
CreateSolidCaret(tm.tmAveCharWidth / 8, tm.tmHeight);
//CreateSolidCaret(mLogfont.lfWidth/8, mLogfont.lfHeight);
SetCaretPos(point);
ShowCaret();
startPoint = point;
mstr.Empty();
break;
case 2:
startPoint = point;
endPoint = point;
bDraw = true;
break;
default:
break;
}
CView::OnLButtonDown(nFlags, point);
}
9. 按Ctrl+F5 运行程序,然后点击SetColor菜单
设置颜色,如下:
10. 点击“Draw Line” 菜单,绘制直线,结果如下:
可见绘制直线的颜色,即是设置的颜色。