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

这篇具有很好参考价值的文章主要介绍了MFC 通用对话框之颜色对话框。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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”,如下:

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

2. 在视图类中添加变量mSelCorlor,如下: 

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

 3. 为“SetCorlor”添加事件处理程序,如下:

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

 代码如下:

void CFileDialogTestView::OnSetColor()
{
	// TODO: 在此添加命令处理程序代码
	CColorDialog cdlg(mSelColor);
	cdlg.m_cc.rgbResult = mSelColor;
	if (cdlg.DoModal()==IDOK)
	{
		mSelColor = cdlg.GetColor();
	}
}

4. 为菜单“Draw Line”添加事件处理函数,如下:

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

代码如下:

void CFileDialogTestView::OnDrawLine()
{
	// TODO: 在此添加命令处理程序代码
	drawType = 2;
}

5. 在视图类中添加变量endPoint及bDraw,如下:

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

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

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菜单

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

设置颜色,如下:

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

10. 点击“Draw Line” 菜单,绘制直线,结果如下:

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

 可见绘制直线的颜色,即是设置的颜色。文章来源地址https://www.toymoban.com/news/detail-457631.html

到了这里,关于MFC 通用对话框之颜色对话框的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • MFC(十三)多个对话框

    1.打开类视图,右键项目---类向导--添加Cpropsheet类,命名为mypropsheet CPropertySheet 是 MFC 的一个类,用于创建包含多个属性页的对话框。它可以使用内置的向导模式,向用户显示“下一步”和“上一步”按钮 2.在Cmypropsheet.h中,声明三个属性类的对象  3.在构造函数里,把三个对

    2024年02月08日
    浏览(13)
  • MFC 对话框与控件的使用

    MFC 对话框与控件的使用

    1、 熟悉 Windows 对话框应用程序开发的基本过程; 2、 学习标准控件的使用; 3、 学些 CommonDialog ActiveX 控件的使用; 4、 练习较复杂的交互式操作的控制流程; 5、 练习文件输入/输出流的使用。 实现一个具有一定实用价值的通讯录程序设计。界面如图所示 ①界面的基本设计

    2024年02月05日
    浏览(13)
  • MFC 给对话框添加图片背景

    MFC 给对话框添加图片背景

    在windows开发当中做界面的主要技术之一就是使用MFC,通常我们看到的QQ,360,暴风影音这些漂亮的界面都可以用MFC来实现。今天我们来说一下如何用MFC美化对话框,默认情况下,对话框的背景如下: 那么,我们如何将它的背景变成如下界面呢,而且还要保留对话框的移动功能,

    2024年02月06日
    浏览(18)
  • MFC为对话框资源添加类

    MFC为对话框资源添加类

    VC6新建一个对话框类型的工程; 建立之后资源中默认有2个对话框,一个是主对话框,About这个是默认建立的关于版权信息的; 然后主对话框有对应的.h和.cpp文件;可以在其中进行编程;   默认建立的有一个 关于 对话框;   在资源中新插入一个对话框,IDD_DIALOG1是对话框I

    2024年02月01日
    浏览(11)
  • MFC--对话框的一个画图控件-HighSpeedChart

    MFC--对话框的一个画图控件-HighSpeedChart

    下载链接在最下面  (免费) 可在vs2022,vs2019等vs版本使用  成果展示 下载好文件后,将  整个文件夹,添加到项目中,  1.之后,将控件拖到对话框里,修改属性到与图片相同            2.为对话框添加类,我这里添加的是  3.为控件增加变量,因为不是VS自带的控件 4.点

    2024年02月04日
    浏览(13)
  • Opencv显示图片(MFC基于对话框窗口)

    Opencv显示图片(MFC基于对话框窗口)

    #1.编辑对话框按钮,修改各个按钮窗口ID和绑定变量 picture 控件 “打开图片”按钮控件 #2.编写显示图片函数 void DrawMat(cv::Mat img, UINT nID); .h文件进行声明 .cpp进行定义 #3.定义CString fun_LoadImg();函数 .h文件进行声明 .cpp文件进行定义 #4.双击“打开图片”按钮 系统自动弹出响应函数

    2024年04月15日
    浏览(15)
  • MFC 单文档弹出对话框 标题图标

    创建MFC时 1、应用程序类型:以单个文档-项目样式选择MFC标准 菜单关联对话框  2、菜单关联-资源视图中的Menu-IDR_MAINFRAME(此处看想关联谁,如果是想关联一个对话框,那就先创建一个对话框再进行关联,如果是想关联一个类,可以看函数在哪个类下面从而进行关联) a.关联

    2024年02月06日
    浏览(13)
  • MFC基于对话框使用dll进行多语言切换

    MFC基于对话框使用dll进行多语言切换

    title: MFC基于对话框使用dll进行多语言切换 categories:[MFC] tags:[音视频编程] date: 2021/12/15 作者:hackett 微信公众号:加班猿 Qt使用qm文件切换有两种加载方式,比较容易一些 在资源文件中加载(这个比较好): 优点: 在程序发布时不用把最新的.qm文件拷贝到加载路径中,降低了修

    2024年02月08日
    浏览(13)
  • qt学习:QT对话框+颜色+文件+字体+输入

    qt学习:QT对话框+颜色+文件+字体+输入

    目录 概述 继承图 QColorDialog 颜色对话框 QFileDialog 文件对话框 保存文件对话框 QFontDialog 字体对话框 QInputDialog 输入对话框 对于对话框的功能,在GUI图形界面开发过程,使用是非常多,那么Qt也提供了丰富的对话框类 QDialog是所有对话框的基类 QWidget  QDialog QColorDialog 颜色对话框

    2024年01月21日
    浏览(41)
  • MFC为资源对话框添加消息处理函数和初始化控件

    MFC为资源对话框添加消息处理函数和初始化控件

    现在我VC6新建了一个对话框工程;又在资源添加了一个新的对话框,并为新的对话框添加了名为CTestDlg的类; 在主对话框的cpp文件包含#include \\\"TestDlg.h\\\"; 在主对话框的cpp文件的OnInitDialog()成员函数中,添加2句,     CTestDlg tdlg;     tdlg.DoModal(); 就可以弹出这个对话框; 在新

    2024年01月18日
    浏览(13)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包