CColorDialog類封裝了顏色對(duì)話框,此類允許您將顏色選擇對(duì)話框合并到應(yīng)用程序中。顏色對(duì)話框就像畫家的調(diào)色板一樣,可顯示系統(tǒng)定義的顏色列表,用戶可以從列表中選擇或創(chuàng)建特定顏色。構(gòu)造一個(gè)CColorDialog類對(duì)象后,可用DoModal( )函數(shù)來顯示顏色對(duì)話框。
CColorDialog
的構(gòu)造函數(shù)原型如下:
CColorDialog(COLORREF clrInit = 0, DWORD dwFlags = 0, CWnd* pParentWnd = NULL);
參數(shù)
clrInit
默認(rèn)顏色選擇。 如果未指定任何值,則默認(rèn)值為 RGB (0,0,0) (黑色) 。
dwFlags
一組用于自定義對(duì)話框的函數(shù)和外觀的標(biāo)志。 有關(guān)詳細(xì)信息,請(qǐng)參閱 Windows SDK 中的CHOOSECOLOR結(jié)構(gòu)。
pParentWnd
指向?qū)υ捒虻母复翱诨蛩姓叽翱诘闹羔槨?/p>
除DoModal( )函數(shù)外,還有以下幾個(gè)成員函數(shù):
CColorDialog:: GetColor | 返回一個(gè)?COLORREF ?結(jié)構(gòu),該結(jié)構(gòu)包含選定顏色的值。 |
CColorDialog::GetSavedCustomColors | 檢索用戶創(chuàng)建的自定義顏色。 |
CColorDialog::SetCurrentColor | 強(qiáng)制當(dāng)前顏色選擇指定的顏色。 |
CCorlorDialog類有一個(gè)CHOOSECOLOR結(jié)構(gòu)類型的公共數(shù)據(jù)成員m_cc, 可以使用?m_cc來實(shí)現(xiàn),設(shè)置顏色對(duì)話框的初始選擇顏色等。CHOOSECOLOR結(jié)構(gòu)定義如下:
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.
示例(基于演示文件對(duì)話框所創(chuàng)建的單文檔工程):
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”添加事件處理函數(shù),如下:
代碼如下:
void CFileDialogTestView::OnDrawLine()
{
// TODO: 在此添加命令處理程序代碼
drawType = 2;
}
5.?在視圖類中添加變量endPoint及bDraw,如下:
6. 在OnLButtonDown(UINT nFlags, CPoint point)消息處理函數(shù)中加入“case 2:”的對(duì)應(yīng)代碼如下:
void CFileDialogTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值
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)消息處理函數(shù),代碼如下:
void CFileDialogTestView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值
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)消息處理函數(shù),代碼如下:
void CFileDialogTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值
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 運(yùn)行程序,然后點(diǎn)擊SetColor菜單
設(shè)置顏色,如下:
10. 點(diǎn)擊“Draw Line” 菜單,繪制直線,結(jié)果如下:
文章來源:http://www.zghlxwxcb.cn/news/detail-457631.html
?可見繪制直線的顏色,即是設(shè)置的顏色。文章來源地址http://www.zghlxwxcb.cn/news/detail-457631.html
到了這里,關(guān)于MFC 通用對(duì)話框之顏色對(duì)話框的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!