How do you stop all that flicker when you're drawing? I think you need to use a Memory DC right? If that is true then how the heck would you blit two Memory DCs to the screen(because if you do it twice the first blitted DC will be erased)?
Thanks.
wildfrog
November 12th, 2007, 06:54 PM
because if you do it twice the first blitted DC will be erased?Well, that is the point ;)
You prepare the image on an 'offscreen' surface first, and then when it's complete you blit the whole image to the screen. In one operation.
This way the user won't see you draw the scrreen line by line, sprite by sprite etc. and the flickering will dissapear.
- petter
.pcbrainbuster
November 12th, 2007, 07:09 PM
Could you show an example code please :)
henky@nok.co.id
November 13th, 2007, 12:56 AM
You can find the examples at Code Guru Articles about flicker free (http://www.codeguru.com/cpp/misc/misc/flickerfreedrawing/).
Note: What ever you want to know, please search first about the topics
at this forum before posting new thread.
.pcbrainbuster
November 14th, 2007, 03:49 PM
Aren't they in another framework? I'm workin' with Win32. :)
Thanks.
JamesSchumacher
November 14th, 2007, 04:04 PM
// BELOW are initialized in WM_CREATE and reinitialized if WM_SIZE
HBITMAP PersistenceBitmap;
SIZE BitmapSize;
INT_PTR __stdcall MyWindowProcess(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
if (message == WM_CREATE)
{
// do above mentioned
...
}
else if (message == WM_PAINT)
{
PAINTSTRUCT ps;
// then check if there is any rect part that needs painted on the main
// window. InvalidateRect(hWnd,NULL,TRUE) will do the whole client area
// same for RedrawWindow(hWnd,NULL,NULL,RDW_INVALIDATE | RDW_ERASE);
if (ps.fErase != 0)
{
const long nWidth = ps.rcPaint.right - ps.rcPaint.left;
const long nHeight = ps.rcPaint.bottom - ps.rcPaint.top;
Using this method, when you need to redraw the window, you can draw the entire window in a memory DC, however, enforce only redrawing a portion of the window by using InvalidateRect(). The same can be done with RedrawWindow. This is done by providing a pointer to a RECT structure specifying a portion of the client area.
Attached is a full working example.
.pcbrainbuster
November 18th, 2007, 02:57 PM
I'm still confused :( Would you mind to create a program that draws a line when a user holds down the mouse where the line does not flicker?
Thanks.
Marc G
November 19th, 2007, 05:40 AM
See attachment. It is a small project that allows the user to draw a rectangle. I took a rectangle because it's much more obvious when it's flickering. In the tools menu you can enable/disable double buffering so you see the difference.
.pcbrainbuster
November 19th, 2007, 12:17 PM
Well I'm a little confused :( What part exactly does the double buffering?
Thanks.
Marc G
November 20th, 2007, 05:30 AM
The WM_PAINT part.
All painting should be done in response to WM_PAINT.
jegonz
February 13th, 2009, 09:54 AM
Hello!
MarkG thank you very much for the code.
I have problem with Flickering as well and was trying to adapt your code to my application, but I'm not very good with programming.
What I'm trying to do is to draw different poligons points depending on a serial port input. I have everything working, but the flickering is there. I guess, this is because I'm using a panel to draw the images and everytime the points change, I call the refresh() fucntion. I tried the double buffering, I think, but anyways if I call the refresh function it erases everything and redraws again, so the flickering is still there.
I can't find the way to draw from outside the panel_onpaint event to the panel, it always gives me an error that the object is not reference to an object.
this is my code in the panel_onpaint event with the "double buffer" attempt. I was trying to adapt your code, but still can't figure it out. I will keep trying, and any help will be apreciated!
Color aColor = Color::Black;
SolidBrush^ aBrush = gcnew SolidBrush(aColor);
Point point1_I = Point(poly_points_X_index[0],poly_points_Y_index[0]);
Point point2_I = Point(poly_points_X_index[1],poly_points_Y_index[1]);
Point point3_I = Point(poly_points_X_index[2],poly_points_Y_index[2]);
Point point4_I = Point(poly_points_X_index[3],poly_points_Y_index[3]);
array<Point>^ points_index1 = {point1_I,point2_I,point3_I,point4_I};
Point point1_I2 = Point(poly_points_X_index2[0],poly_points_Y_index2[0]);
Point point2_I2 = Point(poly_points_X_index2[1],poly_points_Y_index2[1]);
Point point3_I2 = Point(poly_points_X_index2[2],poly_points_Y_index2[2]);
Point point4_I2 = Point(poly_points_X_index2[3],poly_points_Y_index2[3]);
array<Point>^ points_index2 = {point1_I2,point2_I2,point3_I2,point4_I2};
Point point1_T = Point(poly_points_X_Thumb[0],poly_points_Y_Thumb[0]);
Point point2_T = Point(poly_points_X_Thumb[1],poly_points_Y_Thumb[1]);
Point point3_T = Point(poly_points_X_Thumb[2],poly_points_Y_Thumb[2]);
Point point4_T = Point(poly_points_X_Thumb[3],poly_points_Y_Thumb[3]);
array<Point>^ points_thumb1 = {point1_T,point2_T,point3_T,point4_T};
Point point1_T2 = Point(poly_points_X_Thumb2[0],poly_points_Y_Thumb2[0]);
Point point2_T2 = Point(poly_points_X_Thumb2[1],poly_points_Y_Thumb2[1]);
Point point3_T2 = Point(poly_points_X_Thumb2[2],poly_points_Y_Thumb2[2]);
Point point4_T2 = Point(poly_points_X_Thumb2[3],poly_points_Y_Thumb2[3]);
array<Point>^ points_thumb2 = {point1_T2,point2_T2,point3_T2,point4_T2};
You are using managed code, my example is for unmanaged C++.
The principle is the same I guess, but I cannot help you with this. Please ask in the managed C++ section of codeguru.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.