MFC 通用对话框之颜色对话框

21 篇文章 6 订阅

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.

FlagMeaning
CC_ANYCOLORCauses the dialog box to display all available colors in the set of basic colors.
CC_ENABLEHOOKEnables the hook procedure specified in the lpfnHook member of this structure. This flag is used only to initialize the dialog box.
CC_ENABLETEMPLATEIndicates 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_ENABLETEMPLATEHANDLEIndicates 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_FULLOPENCauses 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_PREVENTFULLOPENDisables the Define Custom Colors button.
CC_RGBINITCauses the dialog box to use the color specified in the rgbResult member as the initial color selection.
CC_SHOWHELPCauses 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_SOLIDCOLORCauses 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” 菜单,绘制直线,结果如下:

 可见绘制直线的颜色,即是设置的颜色。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bill66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值