Thread: How to avoid flickering in mfc

  1. #1
    Registered User
    Join Date
    Mar 2010

    How to avoid flickering in mfc

    Hi everyone,

    I am working in mfc SDI application,I have many drawing in that,while scrolling the window lot of flickering occurs,Somebody suggests to use Double buffering.But i dont know how to implement in my code.Can any one pls give me some samples,(Drawings drawn using function calling,getting the view pDC like that),..

    Thanking for any comments,


  2. #2
    Registered User
    Join Date
    Dec 2007

    if that link doesn't work just Google "mfc double buffering".

  3. #3
    train spotter
    Join Date
    Aug 2001
    near a computer
    Search here, I have posted code and details before. If you can't find it or have troubles post again (with code would help) and I will try to assist.


    Ensure you are handling the OnEraseBkgnd() and returning true.

    Ensure you are only calling for a paint when required (minimum amout of InvalidateRect() calls).

    Ensure you use InvalidateRect() with the minimum rectangle possible.

    Follow each InvalidateRect() with an UpdateWindow() [this causes the paint msg to be posted directly to the window's callback, not to the OS msg queue, speeding up paint.]

    Ensure your paint is not using the entire client rect and is using the rect / region returned in the PAINTSTRUCT or GetUpdateRect().

    //this code is a rough guide, not working code 
    //it is a quick cut and paste from multiple methods
    //I have a base class that does all the create and clean-up 
    //the individual drawing code is in the derived class
    //I also prefer to return an error than throw / ASSERT()
    //you will need to tailor this code to your needs.
    //in .h
    #define		WHITE					RGB(255,255,255)
    CBitmap		*m_ScrOrBmp;	//this si the original 1x1 black and white BMP we get in a created DC, we save it till we need to delete the DC
    CBitmap		m_ScrBmp;	//this is our created BMP with size equal to the client area and colour depth of the main screen
    CDC		m_ScrDC;		//this is the DC to hold our created BMP. We use this DC to do all the drawing, and then call for a paint to copy this DC to the screen
    //in cpp
    void CMyDialog::OnCreate()
    	CDC		*pDC;
    	if(!Rect.Width() || !Rect.Height())//window not yet sized
    		return ERROR;
    	//create a memory DC and bitmap
    	HDC	hdc=::GetDC(NULL);
    	m_ScrDC.Attach( ::CreateCompatibleDC(hdc));
    	//catch the returned BMP so we can return the DC to the original state before we delete it
    	//fill the client area with a solid white colour
    	CBrush		Brush;
    	//fill area
    	//clean up 
    void CMyDialog::OnPaint()
    	CPaintDC dc(this); // device context for painting
    	CRect			Rect;
    void CMyDialog::OnClose()
    	// test to ensure we do not try to release a NULL DC
    		//put the original BMP back
    		//now the created BMP is not selected into a DC we can delete it
    		//now the created DC is back to its original state we can delete it
    		//make sure our test is valid by setting the HDC member to NULL
    //add methods to draw to m_ScrDC and then call a paint msg
    //add a method to handle size changes, delete the current BMP and DC and create a new set the required size
    Last edited by novacain; 03-08-2010 at 12:28 AM. Reason: Added code as I had a few minutes, and clarity / typos
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  2. Release MFC Programs & Dynamic MFC DLL :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-18-2002, 06:42 PM
  3. Beginning MFC (Prosise) Part III - Now What? :: C++
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2002, 06:58 PM
  4. MFC is Challenging :: C++
    By kuphryn in forum C++ Programming
    Replies: 8
    Last Post: 02-05-2002, 01:33 AM
  5. screen flickering in MFC
    By MrBurns in forum Windows Programming
    Replies: 3
    Last Post: 09-01-2001, 01:42 PM

Tags for this Thread