设置静态文本颜色 Win32 [英] Set static text color Win32
问题描述
我正在制作一个控制对话框的dll.我喜欢让某个区域有红色文字.这段代码确实可以编译,但是没有看到效果.这里是 dialogProc 完成的区域:
LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){开关(消息){案例 WM_INITDIALOG:CheckDlgButton(hDlg, IDC_CHECK, FALSE);EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);返回真;案例 WM_COMMAND:开关 (LOWORD(wParam)){案例IDC_CHECK:如果(IsDlgButtonChecked(hDlg,IDC_CHECK)){EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);EnableWindow(GetDlgItem(hDlg, IDCANCEL), FALSE);}别的{EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);EnableWindow(GetDlgItem(hDlg, IDCANCEL), TRUE);}休息;案例IDOK:{EndDialog(hDlg, TRUE);返回假;}案例IDCANCEL:{EndDialog(hDlg, FALSE);返回假;}案例 WM_CTLCOLORSTATIC://为我们的 URL 设置文本的颜色if ((HWND)lParam == GetDlgItem(hDlg,IDC_WARNING)){//我们将要绘制静态//在 (HDC)lParam 中设置文本颜色SetBkMode((HDC)wParam,TRANSPARENT);SetTextColor((HDC)wParam, RGB(255,0,0));返回(BOOL)CreateSolidBrush(GetSysColor(COLOR_MENU));}返回真;}}返回假;}
WM_CTLCOLORSTATIC 是一条独立于 WM_COMMAND 的消息.您希望对消息的处理似乎是正确的,只是对消息的检查在您对 WM_COMMAND 特定项目的检查中.尝试重新组织外部 switch 语句.也许类似于以下内容:
LRESULT CALLBACK DialogProc(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam){开关(消息){案例 WM_INITDIALOG://...休息;案例 WM_COMMAND:开关 (LOWORD(wParam)){案例IDC_CHECK://...休息;案例IDOK://...休息;案例IDCANCEL://...休息;}休息;案例 WM_CTLCOLORSTATIC://为我们的 URL 设置文本的颜色如果((HWND)lParam == GetDlgItem(hDlg,IDC_WARNING)){//我们将要绘制静态//在 (HDC)lParam 中设置文本颜色SetBkMode((HDC)wParam,TRANSPARENT);SetTextColor((HDC)wParam, RGB(255,0,0));//注意:根据 selbie 所指出的文档,GetSolidBrush 会泄漏 GDI 句柄.返回 (BOOL)GetSysColorBrush(COLOR_MENU);}休息;}返回假;}
另请注意,当 wParam 应该为 WM_CTLCOLORSTATIC 提供 HDC 时,过滤 WM_COMMAND 的 wParam 参数会有点奇怪.
I am making a dll that controls a dialogue box. I like to get a certain area to have red text. This code does compile, but the effect is not seen. Here is the area where the dialogProc is done:
LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
CheckDlgButton(hDlg, IDC_CHECK, FALSE);
EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_CHECK:
if (IsDlgButtonChecked(hDlg, IDC_CHECK))
{
EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
EnableWindow(GetDlgItem(hDlg, IDCANCEL), FALSE);
}
else
{
EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
EnableWindow(GetDlgItem(hDlg, IDCANCEL), TRUE);
}
break;
case IDOK:
{
EndDialog(hDlg, TRUE);
return FALSE;
}
case IDCANCEL:
{
EndDialog(hDlg, FALSE);
return FALSE;
}
case WM_CTLCOLORSTATIC:
// Set the colour of the text for our URL
if ((HWND)lParam == GetDlgItem(hDlg,IDC_WARNING))
{
// we're about to draw the static
// set the text colour in (HDC)lParam
SetBkMode((HDC)wParam,TRANSPARENT);
SetTextColor((HDC)wParam, RGB(255,0,0));
return (BOOL)CreateSolidBrush (GetSysColor(COLOR_MENU));
}
return TRUE;
}
}
return FALSE;
}
WM_CTLCOLORSTATIC is a separate message from WM_COMMAND. Your desired handling of the message appears to be correct except that the check for the message is inside your check for WM_COMMAND specific items. Try reorganizing your outer switch statement. Perhaps something like the following:
LRESULT CALLBACK DialogProc(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
// ...
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_CHECK:
// ...
break;
case IDOK:
// ...
break;
case IDCANCEL:
// ...
break;
}
break;
case WM_CTLCOLORSTATIC:
// Set the colour of the text for our URL
if ((HWND)lParam == GetDlgItem(hDlg, IDC_WARNING))
{
// we're about to draw the static
// set the text colour in (HDC)lParam
SetBkMode((HDC)wParam,TRANSPARENT);
SetTextColor((HDC)wParam, RGB(255,0,0));
// NOTE: per documentation as pointed out by selbie, GetSolidBrush would leak a GDI handle.
return (BOOL)GetSysColorBrush(COLOR_MENU);
}
break;
}
return FALSE;
}
Also note that it would be kinda weird to filter WM_COMMAND's wParam argument when wParam is supposed to provide the HDC for WM_CTLCOLORSTATIC.
WM_CTLCOLORSTATIC Notification at MSDN
这篇关于设置静态文本颜色 Win32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!