MFC颜色对话框使用
来源:互联网 发布:mac音乐制作软件有哪些 编辑:程序博客网 时间:2024/06/05 21:09
可以使用CColorDialog打开选择如下颜色对话框
其构造函数如下:
);
- Flags:
- 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 Color button.
- CC_RGBINIT
- Causes the dialog box to use the color specified in the
rgbResult member as the initial color selection. - 使用指定的颜色作为对话框打开时的初始颜色(可以在构造函数指定初始颜色或使用会话框的m_cc.rgbResult 来指定)
- 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.
最后可以通过GetColor()函数得到COLORREF类型的颜色,再用GetXValue得到具体的颜色值。
如使用如下
void CEnvParaTab::OnBnClickedButtonClearco
{
}
颜色对话框大家肯定也不陌生,我们可以打开它选择需要的颜色,简单说,它的作用就是用来选择颜色。MFC中提供了CColorDialog类封装了颜色对话框的所有操作,我们可以通过它显示颜色对话框,并获取颜色对话框中选择的颜色。颜色对话框跟字体对话框一样,也是一种模态对话框。
CColorDialog类的构造函数
CColorDialog(
COLORREF clrInit = 0,
DWORD dwFlags = 0,
CWnd* pParentWnd = NULL
);
参数说明:
clrInit:默认选择颜色的颜色值,类型为COLORREF,实际上就是unsigned long类型。如果没有设置它的值,则默认为RGB(0,0,0),即黑色。
注:RGB(r,g,b)是宏,可以计算颜色值。括号中的三个值分别为红、绿、蓝分量的值。
dwFlags:自定义颜色对话框功能和外观的属性值。详情可在MSDN中查阅。
pParentWnd:颜色对话框的父窗口的指针。
获取颜色对话框中所选颜色值
我们使用颜色对话框的最终目的还是要获得在颜色对话框中选择的颜色值。为此CColorDialog类的成员函数GetColor()能够很好的实现我们的要求。GetColor()函数的原型为:
COLORREF GetColor( ) const;
它返回所选颜色的COLORREF值。
如果我们想获得R、G、B各分量的值呢?可以根据GetColor得到的COLORREF颜色值,通过使用GetRValue、GetGValue和GetBValue三个宏获得。GetRValue的语法形式为:
BYTE GetRValue(DWORD rgb);
参数rgb就是COLORREF颜色值,返回值即是R分量值。其他两个宏的形式与之类似。例如,GetColor()函数返回的COLORREF为10000,则R分量值就是GetRValue(10000)。
颜色对话框应用实例
鸡啄米下面给大家做一个颜色对话框的小例子。此例要实现的功能简单介绍下:生成一个对话框,对话框中放置一个“颜色选择”按钮,四个静态文本框和四个编辑框。四个静态文本框分别显示Color:、R:、G:、B:,每个静态文本框后面跟一个编辑框,分别用来显示颜色对话框中选择的颜色值和所选颜色值的红色分量、绿色分量、蓝色分量。
以下是实例创建的步骤:
1.创建一个基于对话框的MFC工程,名字为“Example19”。
2.在自动生成的主对话框IDD_EXAMPLE19_DIALOG的模板中,删除“TODO: Place dialog controls here.”静态文本框,添加一个按钮,ID设为IDC_COLOR_BUTTON,Caption设为“颜色选择”,用于显示颜色对话框来选择颜色。再添加四个静态文本框,ID分别为IDC_COLOR_STATIC、IDC_R_STATIC、IDC_G_STATIC、IDC_B_STATIC,Caption分别设为“Color:”、“R:”、“G:”、“B:”,然后每个静态文本框后添加一个编辑框,四个编辑框的ID分别为IDC_COLOR_EDIT、IDC_R_EDIT、IDC_G_EDIT、IDC_B_EDIT,分别用来显示颜色对话框中选择的颜色值和所选颜色值的红色分量、绿色分量、蓝色分量。
3.为按钮IDC_COLOR_BUTTON添加点击消息的消息处理函数CExample19Dlg::OnBnClickedColorButton()。
4.修改消息处理函数CExample19Dlg::OnBnClickedColorButton()如下:
- void CExample19Dlg::OnBnClickedColorButton()
- {
- // TODO: Add your control notification handler code here
- COLORREF color = RGB(255, 0, 0); // 颜色对话框的初始颜色为红色
- CColorDialog colorDlg(color); // 构造颜色对话框,传入初始颜色值
- if (IDOK == colorDlg.DoModal()) // 显示颜色对话框,并判断是否点击了“确定”
- {
- color = colorDlg.GetColor(); // 获取颜色对话框中选择的颜色值
- SetDlgItemInt(IDC_COLOR_EDIT, color); // 在Color编辑框中显示所选颜色值
- SetDlgItemInt(IDC_R_EDIT, GetRValue(color)); // 在R编辑框中显示所选颜色的R分量值
- SetDlgItemInt(IDC_G_EDIT, GetGValue(color)); // 在G编辑框中显示所选颜色的G分量值
- SetDlgItemInt(IDC_B_EDIT, GetBValue(color)); // 在B编辑框中显示所选颜色的B分量值
- }
- }
5.最后编译运行程序,在结果对话框中点击“颜色选择”按钮,弹出颜色对话框。初始状态下,选择框在红色上,我们选另一种颜色,此时的颜色对话框如下:
点“确定”,主对话框上的四个编辑框中分别显示了选择的颜色值、R分量、G分量和B分量:
我们在实际开发中,可以用获取到的颜色值来设置其他对象的颜色,使用还是很方便的。
关于颜色对话框就讲到这里了。其实各种对话框的使用都有很多相似之处,相信大家越来越熟悉了。最后还是欢迎大家继续关注鸡啄米的VS2010/MFC入门教程。
- MFC颜色对话框使用
- MFC颜色对话框的使用
- MFC颜色对话框的使用
- MFC颜色对话框的使用
- MFC中颜色选择对话框的使用
- MFC颜色对话框
- MFC 颜色对话框
- MFC对话框颜色设置
- MFC设置对话框、字体对话框、颜色对话框
- MFC设置对话框、字体对话框、颜色对话框
- mfc 改变对话框背景颜色
- mfc 改变对话框背景颜色
- MFC 调用颜色选择对话框
- MFC改变对话框字体颜色
- 用MFC创建颜色对话框
- MFC-字体和颜色对话框
- MFC显示颜色选择对话框
- MFC:颜色字体对话框及控件颜色
- eclipse插件FindBugs使用笔记
- hibernate无法自动创建表的原因整理
- 解决Tomcat v8.0 Server at localhost failed to start.
- C++笔试题
- Java集合collection编程题及答案
- MFC颜色对话框使用
- MYSQL使用inner join 进行 查询,删除,修改
- NOI库8756 砝码称重V2
- 需求:MYSQL表中一个字段用逗号分隔,需要查出来split拆分并依次存入另一个表的一个字段中
- JAVA设计模式(1) —<创建型>工厂方法模式(Factory Method)
- Chrome Developer Tools之网络监控与调优
- Fragment
- Unicode Entity Codes for Math
- 1041_考试座位号