Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a simple Win32 program which paints rectangles stored in a linked list. In the WM_PAINT message the program paints all the nodes (rectangles) in the linked list in a loop. The problem is that the program flickers a bit. I found the program is annoying to use. I am using double buffering now, but it has less results that i expected. Here's my code :

C++
case WM_PAINT:
 
    // Get DC for window
    hdc = BeginPaint(hwnd, &ps);
 
    // Create an off-screen DC for double-buffering
    hdcMem = CreateCompatibleDC(hdc);
    hbmMem = CreateCompatibleBitmap(hdc, win_width, win_height);
 
    hOld   = SelectObject(hdcMem, hbmMem);
 
    // Draw into hdcMem here
	while(/*Linked list*/)
	{
		Rectangle(/*...*/);
		//.... other lines of code
	}
 
    // Transfer the off-screen DC to the screen
    BitBlt(hdc, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
 
    // Free-up the off-screen DC
    SelectObject(hdcMem, hOld);
    DeleteObject(hbmMem);
    DeleteDC    (hdcMem);
 
    EndPaint(hwnd, &ps);
    return 0;


What's wrong with the above code ? Is there a better way to do paintings like this using GDI ?
Posted
Comments
«_Superman_» 19-May-13 22:14pm    
As mentioned in the solution below, add a WM_ERASEBKGND handler and return a non-zero value like TRUE.
This way you simple draw over the existing image instead of first erasing the existing image before drawing.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900