【发布时间】:2020-05-27 19:32:36
【问题描述】:
我对 c++ 有点陌生,我正在尝试创建一个 gui 应用程序来告诉我我的大写锁定是否处于活动状态。我已经设置了基本 UI,它按计划启动(通过颜色向我显示我的锁定状态),但我无法在运行时更改窗口颜色。
这是我的代码:
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
bool state = false;
switch (uMsg) {
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
if ((GetKeyState(VK_CAPITAL) & 0x0001) != 0) {
FillRect(hdc, &ps.rcPaint, CreateSolidBrush(RGB(0, 255, 0)));
}
else {
FillRect(hdc, &ps.rcPaint, CreateSolidBrush(RGB(255, 0, 0)));
}
EndPaint(hwnd, &ps);
}
case WM_KEYUP:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
if ((GetKeyState(VK_CAPITAL) & 0x0001) != 0) {
FillRect(hdc, &ps.rcPaint, CreateSolidBrush(RGB(0, 255, 0)));
}
else {
FillRect(hdc, &ps.rcPaint, CreateSolidBrush(RGB(255, 0, 0)));
}
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
提前致谢。
【问题讨论】:
-
不是那种给用户很大启发的app,只有窗口有焦点才能得到WM_KEYxxx通知。使用 SetTimer()。