Thursday, August 26, 2010

How to repaint a push button in Win32?

Programmer Question

I have a window with a button that paints the entire window red when clicked, but my code paints the whole window, even the button. When I resize the window or cause the part of the window where the button is to repaint, the button will return. I've only been able to paint the button a solid color after painting the window, but that only makes it look like a square with nothing in it.



So how do I paint the button its default color?



//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


Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails