我对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);
}
并预先感谢。
捕获WM_KEYDOWN
消息。如果我按下Caps Lock键(检查参数),请通过调用InvalidateRect
使窗口重绘自身。