Drawing problem, getting flickers
All the program does, is create a plain window and draw a couple of rectangles in it. When the window is resized the rectangles are redrawn to match the size of the window, pretty simple.
The trouble is when you resize the window with the contents showing. The 'Show Window contents while dragging' effect is turned on.
When this happens, the window gets bombarded with redraw messages. In the attempt to draw the rectangles you get this flickering of the window's contents. You may need to compile and run the program to see this.
Any ideas how I can get the contents to draw correctly under these conditions? I know its possible to disable the show contents setting for the program, but it seems to me that this shouldn't be necessary.
Here is the code I'm working with:
Code:
#include <windows.h>
LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hiInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode)
{
WNDCLASS wcClass;
HWND Handle;
MSG msgMessage;
char pcClassName[] = "MDLN-GUI";
wcClass.hInstance = hiInst;
wcClass.lpszClassName = pcClassName;
wcClass.lpfnWndProc = WinProc;
wcClass.style = CS_DBLCLKS;
wcClass.hIcon = LoadIcon(0, "MW_ICON");
wcClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wcClass.lpszMenuName = NULL;
wcClass.cbClsExtra = 0;
wcClass.cbWndExtra = 0;
wcClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
if (RegisterClass(&wcClass) == 0)
{
return;
}
Handle = CreateWindow("MDLN-GUI", "", WS_OVERLAPPEDWINDOW, 100, 100, 200, 200, NULL, NULL, hiInst, NULL);
UpdateWindow(Handle);
ShowWindow(Handle, SW_SHOW);
while(GetMessage(&msgMessage, NULL, 0, 0) != 0)
{
TranslateMessage(&msgMessage);
DispatchMessage(&msgMessage);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
//case (WM_MOVE) :
case (WM_SIZE) :
{
InvalidateRect(hwnd, NULL, FALSE);
break;
}
case (WM_PAINT) :
{
PAINTSTRUCT sPaint;
RECT sRect;
LOGBRUSH sBrush;
HBRUSH hBrush;
BeginPaint(hwnd, &sPaint);
GetClientRect(hwnd, &sRect);
sBrush.lbStyle = BS_SOLID;
sBrush.lbColor = RGB(0, 255, 0);
sBrush.lbHatch = 0;
sRect.top += 25;
sRect.left += 25;
sRect.bottom -= 25;
sRect.right -= 25;
hBrush = CreateBrushIndirect(&sBrush);
FillRect(sPaint.hdc, &sRect, hBrush);
DeleteObject(hBrush);
sBrush.lbStyle = BS_SOLID;
sBrush.lbColor = RGB(0, 0, 255);
sBrush.lbHatch = 0;
sRect.top += 25;
sRect.left += 25;
sRect.bottom -= 25;
sRect.right -= 25;
hBrush = CreateBrushIndirect(&sBrush);
FillRect(sPaint.hdc, &sRect, hBrush);
DeleteObject(hBrush);
EndPaint(hwnd, &sPaint);
return 0;
}
case (WM_DESTROY) :
{
PostQuitMessage(0);
break;
}
default :
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
return 0;
}