win32sdk编程如何改变一个static控件上文字的字体和颜色
关于对话框中要处理的控件的ID是IDC_MYSTATIC
// “关于”框的消息处理程序。
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static HFONT hfont_static; // 自定义字体的句柄
static HWND hwnd_static; // Static 控件的句柄
switch (message)
{
case WM_INITDIALOG:
// 建立自定义字体
LOGFONT lgf;
GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof(lgf), &lgf);
lgf.lfWeight = FW_BOLD;
hfont_static = CreateFontIndirect(&lgf);
// 设置字体
hwnd_static = GetDlgItem(hDlg, IDC_MYSTATIC); //***
SendMessage(hwnd_static, WM_SETFONT, (WPARAM)hfont_static, (LPARAM)TRUE); //***
return TRUE;
case WM_CTLCOLORSTATIC:
// 改变文本颜色和背景颜色
if((HWND)lParam == hwnd_static)
{
SetTextColor((HDC)wParam, 0xff0000); //***
}
else
{
SetTextColor((HDC)wParam, GetSysColor(COLOR_WINDOWTEXT));
}
SetBkColor((HDC)wParam, GetSysColor(COLOR_BTNFACE));
SetWindowLong(hDlg, DWL_MSGRESULT, (LONG)TRUE);
return (LRESULT)GetSysColorBrush(COLOR_BTNFACE);
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
DeleteObject(hfont_static);
EndDialog(hDlg, LOWORD(wParam)); // 销毁自定义字体
return TRUE;
}
break;
}
return FALSE;
}
// “关于”框的消息处理程序。
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static HFONT hfont_static; // 自定义字体的句柄
static HWND hwnd_static; // Static 控件的句柄
switch (message)
{
case WM_INITDIALOG:
// 建立自定义字体
LOGFONT lgf;
GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof(lgf), &lgf);
lgf.lfWeight = FW_BOLD;
hfont_static = CreateFontIndirect(&lgf);
// 设置字体
hwnd_static = GetDlgItem(hDlg, IDC_MYSTATIC); //***
SendMessage(hwnd_static, WM_SETFONT, (WPARAM)hfont_static, (LPARAM)TRUE); //***
return TRUE;
case WM_CTLCOLORSTATIC:
// 改变文本颜色和背景颜色
if((HWND)lParam == hwnd_static)
{
SetTextColor((HDC)wParam, 0xff0000); //***
}
else
{
SetTextColor((HDC)wParam, GetSysColor(COLOR_WINDOWTEXT));
}
SetBkColor((HDC)wParam, GetSysColor(COLOR_BTNFACE));
SetWindowLong(hDlg, DWL_MSGRESULT, (LONG)TRUE);
return (LRESULT)GetSysColorBrush(COLOR_BTNFACE);
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
DeleteObject(hfont_static);
EndDialog(hDlg, LOWORD(wParam)); // 销毁自定义字体
return TRUE;
}
break;
}
return FALSE;
}