Click here to Skip to main content
Licence CPOL
First Posted 6 Sep 2008
Views 14,900
Downloads 143
Bookmarked 18 times

Flicker free drawing without using double buffer

By ringphone | 6 Sep 2008 | Unedited contribution
Flicker free drawing without using double buffer
15 votes, 78.9%
1
3 votes, 15.8%
2

3
1 vote, 5.3%
4

5
1.18/5 - 19 votes
1 removed
μ 1.25, σa 1.31 [?]

Introduction

This article discuss how to flicker free drawing but not using double buffer.

Explanation

The picture shows a window,drawing 4 overlapped rects,and always stay at right-bottom corner.without using double buffer,when resize the window,it flickers.
Here is the drawing order(in normal way):
do.gif
every draw action is covered prev drawing,so it flickers,because there is overlapped part.If we can draw with no overlap part,reverse the drawing order like this:
dv.gif
there is no overlapped part,so it will never flicker.but how can we draw an none-rect part?use the function ExcludeClipRect,draw a rect,call ExcludeClipRect to exclude this rect,so next draw will not cover this area,so there is no overlapped part and will not flicker.

Using the code

First,in WM_ERASEBKGND,just return TRUE.then in WM_PAINT:

		case WM_PAINT:
		{
			PAINTSTRUCT ps;
			RECT rc,rcDraw;

			GetClientRect(hwnd,&rc);
			BeginPaint(hwnd,&ps);
			
			for(int i=0;i<4;i++)
			{
				rcDraw.left = rc.right - 200 - 50*i;
				rcDraw.right = rc.right - 50*i;
				rcDraw.top = rc.bottom - 200 - 50*i;
				rcDraw.bottom = rc.bottom - 50*i;
				FillRect(ps.hdc,&rcDraw,g_brushRect[i]);
				ExcludeClipRect(ps.hdc,rcDraw.left,rcDraw.top,rcDraw.right,rcDraw.bottom);
			}
			FillRect(ps.hdc,&rc,g_brushBkg);
			EndPaint(hwnd,&ps);
			return 0;
		}
		

Each time you draw a rect,call ExcludeClipRect to exclude this rect area for drawing,at last,draw the background,and it's flicker free.

License

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

About the Author

ringphone



China China

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberetking007200417:10 30 Dec '08  
GeneralPoor Research PinPopularmemberVitaly Tomilov15:29 6 Sep '08  
Quote: "...it flickers, because there is overlapped part".
 
It appears you don't even know what's causing the flickering Smile | :)
 
Flickering is caused by the fact that drawing directly into the screen is way slower than into memory because too much data is processed over and over again on each level between all filters from the application to the graphic-rendering engine, and that happens on every change made on the screen.
 
This is why double buffering is necessary, because it summarizes the complete sequence of drawing into one single bitmap output that goes into the screen. There are a few methods for optimizing double buffering, but it remains to be essential for most tasks.
 
At least 99% of drawing is way more complex than just some rectangles, so the example is really no good either, it's not practical.
 
Back to the square one, mate, try to understand the essence of drawing via double buffering, so then you will see that your method is no substitute for it at all.
 
Plus, an article like this should be a little deeper into math, i'd say, calculating the real optimization, because numbers are important here, and which you didn't do either.
 
Regards,
Vitaly
 
Professional System Library on www.prosyslib.org

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 6 Sep 2008
Article Copyright 2008 by ringphone
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid