我有一個窗口,點擊按鈕時會將整個窗口繪製成紅色,但是我的代碼繪製了整個窗口,即使是按鈕。當我調整窗口大小或導致重新繪製按鈕的窗口部分時,按鈕將返回。在繪製窗口後,我只能將按鈕繪製成純色,但這隻能讓它看起來像一個沒有任何東西的正方形。如何重新繪製Win32中的按鈕?
那麼我該如何繪製按鈕的默認顏色?
//WinMain
hmain = CreateWindowEx (0, L"Window", L"Window", WS_OVERLAPPEDWINDOW, 0, 0, 200, 200,
NULL, NULL, hinstance, NULL);
HWND hwnd2 = CreateWindowEx (0, L"Button", L"Red", WS_CHILD | BS_PUSHBUTTON, 0, 0, 50, 50,
hmain, (HMENU) redbtt, hinstance, NULL);
//Winmain
//WndProc
COLORREF red = RGB (255, 0, 0);
HBRUSH redbrush = CreateSolidBrush (red);
switch (msg) {
case WM_COMMAND: {
if (LOWORD (wparam) == redbtt) {
color = 'r';
RECT rect;
GetClientRect (hmain, &rect);
HDC hdc = GetDC (hmain);
FillRect (hdc, &rect, redbrush);
ReleaseDC (hmain, hdc);
/*GetClientRect ((HWND) lparam, &rect);
hdc = GetDC ((HWND) lparam);
COLORREF color = GetBkColor (hdc);
HBRUSH brush = CreateSolidBrush (color);
FillRect (hdc, &rect, brush);
ReleaseDC ((HWND) lparam, hdc);*/
}
break;
}
case WM_PAINT: {
HBRUSH brush;
if (color == ' ') {
brush = (HBRUSH) (COLOR_WINDOW + 1);
} else if (color == 'r') {
brush = redbrush;
}
PAINTSTRUCT ps;
HDC hdc = BeginPaint (hmain, &ps);
FillRect (hdc, &ps.rcPaint, brush);
EndPaint (hmain, &ps);
break;
}
//WndProc
這是什麼意思?這只是一個字符決定了WM_PAINT應該塗上什麼顏色。 – TreeTree 2010-08-26 22:38:45
這是一個全局變量。你聽說過全球變數的邪惡嗎?特別不喜歡Windows強制執行的事件驅動編程風格。 「顏色」與窗口強烈關聯,而不是程序的全局狀態以及它創建的所有*其他*窗口。 – 2010-08-26 22:49:42