Sign in to follow this  
maya18222

[win32] WM_PAINT and window flicker

Recommended Posts

Im trying to prevent window flicker when painting my windows. In the same WM_PAINT case, I have rectangles being drawn with smaller rectangles inside of them. But when the window is resized you see flickers of the colour of the larger rectangle that should be being blocked by the smaller rectangle. As an example, I have the following: -
switch(msg)
	{
		case WM_PAINT:
		{
			HDC          hdc;
			PAINTSTRUCT  ps;

			hdc = BeginPaint(hWnd, &ps);

				RECT r = GetClientRect();

				SelectObject( hdc, Red);
				Rectangle(ps.hdc, r.left, r.top, r.right, r.bottom); 

				SelectObject( hdc, Black);
				Rectangle(ps.hdc, r.left, r.top, r.right, r.bottom); 

			
			EndPaint(hWnd, &ps);
			return 0;
		}

		case WM_ERASEBKGND:
		{	
			return 1;
		}
}

So, as you can see, when the window needs drawing, I fill the entire window with red and then the same with black. And the background drawing has been disabled. When testing this, I thought you wouldnt be able to see the red, as the black completly covers it, and the window isnt redrawn until you EndPaint(). But Im wrong. When resizing the window, you do see flickers of red. How do I code it so that I can do all my drawing, and you dont see it entil the end of the function? ie rather than
WM_PAINT:
  DrawRedRect(); PaintWindow();
  DrawBlackRect(); PaintWindow();
return 0;

I want
WM_PAINT:
  DrawRedRect(); 
  DrawBlackRect(); 
  PaintWindow();
return 0;

Is this possible?

Share this post


Link to post
Share on other sites
Thanks for your replies.

Evil Steve, could you expand a little? I take it you mean storing a HDC that is the size of the windows client area, and rendering into that. Do you mean to store a hdc for all windows and controls? or just the main window?

Im thinking something along the lines of this, with only one hdc for the very main window, which gets passed to the children to render to


void TopHierarchyWindow::OnWindowUpdate()
{
// do painting
OnPaint();

// recursivly update all children
for( all child windows )
{
// pass hdc to child
child[i]OnWindowUpdate(hdc);
}
}

void TopHierarchyWindow::OnResize()
{
Resize hdc;
}




Share this post


Link to post
Share on other sites
Here's some code that shows a simple way to do it. It creates a DC and a bitmap to draw to, and then blits that to the window. You can move the setup code out and keep the DC/bitmap around for the duration of the program, and it should work fine to use the same for all windows, as long as the bitmap is large enough to fit the largest one.

case WM_SIZE:
InvalidateRect(hWnd, NULL, TRUE);
return 0;

case WM_PAINT: {
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
if(hDC != NULL) {
RECT rect;
GetClientRect(hWnd, &rect);

HDC hDCMem = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rect.right, rect.bottom);
SelectObject(hDCMem, hBitmap);

FillRect(hDCMem, &rect, (HBRUSH)(COLOR_BTNFACE+1));
FillRect(hDCMem, &rect, (HBRUSH)(COLOR_BACKGROUND+1));

BitBlt(hDC, 0, 0, rect.right, rect.bottom, hDCMem, 0, 0, SRCCOPY);

DeleteObject(hBitmap);
DeleteDC(hDCMem);

EndPaint(hWnd, &ps);
}
} return 0;

case WM_ERASEBKGND:
return 1;

Share this post


Link to post
Share on other sites
Quote:
Original post by Erik Rufelt
Here's some code that shows a simple way to do it. It creates a DC and a bitmap to draw to, and then blits that to the window. You can move the setup code out and keep the DC/bitmap around for the duration of the program, and it should work fine to use the same for all windows, as long as the bitmap is large enough to fit the largest one.
*** Source Snippet Removed ***
Yup, this is what I mean. Although I'd create the HDC and HBITMAP in WM_CREATE, and resize the HBITMAP (Re-create it) in WM_SIZE if your window is resizable.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this