当前位置:网站首页>MFC General dialog color dialog
MFC General dialog color dialog
2022-06-12 04:19:00 【Bill66】
CColorDialog Class encapsulates the color dialog , This class allows you to incorporate color selection dialogs into your application . The color dialog is like a painter's palette , The system defined color list can be displayed , Users can select or create specific colors from the list . Construct a CColorDialog Class object , You can use DoModal() Function to display the color dialog .
CColorDialog The constructor prototype for is as follows :
CColorDialog(COLORREF clrInit = 0, DWORD dwFlags = 0, CWnd* pParentWnd = NULL);
Parameters
clrInit
Default color selection . If no value is specified , The default value is RGB (0,0,0) ( black ) .
dwFlags
A set of flags for customizing the functions and appearance of the dialog box . For more information , see also Windows SDK Medium CHOOSECOLOR structure .
pParentWnd
Pointer to the parent window or owner window of the dialog .
except DoModal( ) Out of function , There are also the following member functions :
| CColorDialog:: GetColor | Return to one COLORREF structure , This structure contains the values for the selected color . |
| CColorDialog::GetSavedCustomColors | Retrieve user created custom colors . |
| CColorDialog::SetCurrentColor | Force the current color to select the specified color . |
CCorlorDialog Class has one. CHOOSECOLOR Public data members of structure types m_cc, have access to m_cc To achieve , Set the initial selection color of the color dialog box, etc .CHOOSECOLOR The structure is defined as follows :
typedef struct {
DWORD lStructSize;
HWND hwndOwner;
HWND hInstance;
COLORREF rgbResult;
COLORREF * lpCustColors;
DWORD Flags;
LPARAM lCustData;
LPCCHOOKPROC lpfnHook;
LPCTSTR lpTemplateName;
} CHOOSECOLOR, *LPCHOOSECOLOR;
Its member functions are described as follows ( Excerpt from 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 COLORREFcolor 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 COLORREFcolor 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 DefineCustomColor button to display the custom color controls. |
| CC_PREVENTFULLOPEN | Disables the DefineCustomColors 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.
Example ( Single document project created based on presentation file dialog box ):
1. stay IDR_MAINFRAME New in menu file “ColorDialogTest” menu , And submenu “Set Color”、“Draw Line”, as follows :

2. Add variables to the view class mSelCorlor, as follows :

3. by “SetCorlor” Add event handler , as follows :

The code is as follows :
void CFileDialogTestView::OnSetColor()
{
// TODO: Add command handler code here
CColorDialog cdlg(mSelColor);
cdlg.m_cc.rgbResult = mSelColor;
if (cdlg.DoModal()==IDOK)
{
mSelColor = cdlg.GetColor();
}
}4. For the menu “Draw Line” Add event handler , as follows :

The code is as follows :
void CFileDialogTestView::OnDrawLine()
{
// TODO: Add command handler code here
drawType = 2;
}
5. Add variables to the view class endPoint And bDraw, as follows :

6. stay OnLButtonDown(UINT nFlags, CPoint point) Add... To the message processing function “case 2:” The corresponding code of is as follows :
void CFileDialogTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add message handler code and / Or call the default value
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. Add in view class OnMouseMove(UINT nFlags, CPoint point) Message handler , The code is as follows :
void CFileDialogTestView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add message handler code and / Or call the default value
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. Add in view class OnLButtonUp(UINT nFlags, CPoint point) Message handler , The code is as follows :
void CFileDialogTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add message handler code and / Or call the default value
if(drawType==2)
drawType = 0;
if (bDraw)
bDraw = false;
CClientDC dc(this);
INT oldMode = dc.SetROP2(R2_NOT);
dc.MoveTo(startPoint);
dc.LineTo(endPoint);
dc.SetROP2(oldMode);
CPen pen(PS_SOLID, 1, mSelColor);
CPen* pOldPen = dc.SelectObject(&pen);
dc.MoveTo(startPoint);
dc.LineTo(endPoint);
dc.SelectObject(pOldPen);
CView::OnLButtonUp(nFlags, point);
}9. Press Ctrl+F5 Run the program , And then click SetColor menu

Set the color , as follows :
10. Click on “Draw Line” menu , Draw a straight line , give the result as follows :

The color of the drawn line is visible , That is, the set color .
边栏推荐
- Recommended system cleaning tools, cocktail Download
- 【C语言】变量本质分析
- R language plot visualization: plot visualization of basic 2D histogram, custom setting of color of 2D histogram, and histogram visualization of binary distribution (basic 2D histogram)
- 疫情数据分析平台工作报告【6】可视化绘图
- leetcode797. 所有可能的路径(中等)
- Work report on epidemic data analysis platform [7] Alibaba cloud related
- What does hard work mean to you?
- Plot visualization in R language: visualize the scatter diagram of the actual value and the predicted value of the regression model, analyze the prediction efficiency of the regression model, distingu
- Esp32c3 remote serial port
- Eight fallacies of distributed computing
猜你喜欢

SQL Safe Backup显示器和缩放字体的支持

Enterprise Architect v16

MongoDB精华总结

【高效】最强开发工具Ctool编译踩坑

Kill session? This cross domain authentication solution is really elegant!

1. Mx6ull learning notes (III) - busybox creates root file system

DS18B20数字温度计 (一) 电气特性, 供电和接线方式

PyTorch中的Sequential、ModuleList和ModuleDict用法总结

Street lighting IOT technology scheme, esp32-s3 chip communication application, intelligent WiFi remote control

Zabbix6.0新功能Geomap 地图标记 你会用吗?
随机推荐
[automation] generate xlsx report based on openstack automated patrol deployed by kolla
Zabbix6.0新功能Geomap 地图标记 你会用吗?
SQL safe backup display and zoom font support
2.28 (defect filling) data type conversion exception handling part multi threading
数据库新建表,以前没问题的,今天
[SC] OpenService FAILED 5: Access is denied.
智能面板WiFi联动技术,ESP32无线芯片模组,物联网WiFi通信应用
webpack---优化_缓存
Construction case of Expressway Precast Beam Yard (with scheme text)
Recommended system cleaning tools, cocktail Download
怎样拥有心灵的平和?获得一颗全新的心灵
[mysql][mysql 8.0 compressed package installation method]
EN in Spacey_ core_ web_ SM installation problems
无线物联网WiFi模块方案,ESP32-S3芯片技术,助力设备智能化
成功解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
PyTorch中的Sequential、ModuleList和ModuleDict用法总结
请用递归的方法计算下列函数的值:px(x,n)=x-x^2 +x^3- x^4+… ((-1)n-1)(xn) n>0 **输入格式要求:“%lf%d“ 提示信息:“Enter X and N:”
mysqld: Can‘t create directory ‘D: oftinstall\mysql57 (Errcode: 2 - No such file or directory)
[MySQL] MySQL installation
R language plot visualization: plot visualization grouped violin plot in R with plot