c++ - WM_CTLCOLORSTATIC 永远不会在 WIN32 应用程序中触发
问题描述
我正在用 C++ 做一个 Win32 应用程序。我有一个静态窗口,我想更改背景的颜色。
我遵循了这里的建议:如何以编程方式设置静态控件背景颜色
但在我的情况下,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;
}
希望你能帮我!
解决方案
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;
}
推荐阅读
- .net - 如何在 Bot 框架 v4 中响应 OnturnAsync 中的任务/获取
- node.js - TypeScript 编译器 (tsc) 在 Raspberry PI 上没有响应
- django - Django daphne asgi:Django 只能处理 ASGI/HTTP 连接,不能处理 websocket
- reactjs - 当我使用钩子单击 h1 标签进行反应时如何获取另一个组件?
- javascript - 如何更改插槽的样式
- sql - Oracle Apex - 如何使用动态操作更新更改操作的值列表?
- typescript - 为什么使用 cordova google-plus 插件会弹出两次“确认您的选择”?
- python - 快速替换字符串中的字符并检查子字符串是否为回文
- apache-spark - Cassandra + Spark 执行器超融合
- android - 如何从 Firestore 服务器获取时间戳,而无需担心设备时钟与 Firestore 不同步的情况