我正在用C ++编写Win32应用程序。我有一个静态窗口,我想更改背景颜色。
我遵循了此处的建议:how can I set static controls background color programmatically
但是对于我来说,WM_CTLCOLORSTATIC从未触发。您是否知道会如何发生?
这是我的一些代码:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case WM_CTLCOLORSTATIC:
hdc = reinterpret_cast<HDC>(wParam);
SetTextColor(hdc, RGB(2000, 50, 100));
SetBkColor(hdc, RGB(20, 150, 100));
if (!hBrushLabel) hBrushLabel = CreateSolidBrush(RGB(20, 150, 100));
return reinterpret_cast<LRESULT>(hBrushLabel);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_CREATE:
hBrushLabel = NULL;
AddBuyButtons(hWnd);
AddText(hWnd);
AddCartonesSlots(hWnd);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
if (hBrushLabel) DeleteObject(hBrushLabel);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
我检查过的变更函数称为:
DWORD WINAPI changecolor(HWND h)
{
if (hBrushLabel) {
DeleteObject(hBrushLabel);
hBrushLabel = NULL;
}
InvalidateRect(h, NULL, TRUE);
return 0;
}
希望你能帮助我!
答案 0 :(得分:1)
WM_CTLCOLORSTATIC
是它自己的窗口消息,但是您正在处理它,就好像它是WM_COMMAND
消息的菜单ID。您需要将case WM_CTLCOLORSTATIC
处理程序从内部switch
块移到外部switch
块:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
}
}
break;
// MOVED HERE!!!
case WM_CTLCOLORSTATIC:
hdc = reinterpret_cast<HDC>(wParam);
SetTextColor(hdc, RGB(2000, 50, 100));
SetBkColor(hdc, RGB(20, 150, 100));
if (!hBrushLabel) hBrushLabel = CreateSolidBrush(RGB(20, 150, 100));
return reinterpret_cast<LRESULT>(hBrushLabel);
break;
case WM_CREATE:
hBrushLabel = NULL;
AddBuyButtons(hWnd);
AddText(hWnd);
AddCartonesSlots(hWnd);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
if (hBrushLabel) DeleteObject(hBrushLabel);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}