Thread: How to avoid flickering in mfc

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    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,

    Shiva..

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Google

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

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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.

    Generally.....

    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().


    Code:
    //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()
    {
    	BITMAP		BMP;
    	CDC		*pDC;
    
    	GetClientRect(Rect);
    	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));
    	m_ScrBmp.Attach(::CreateCompatibleBitmap(hdc,Rect.Width(),Rect.Height()));
    
    	//catch the returned BMP so we can return the DC to the original state before we delete it
    	m_ScrOrBmp=m_ScrDC.SelectObject(&m_ScrBmp);
    
    	//fill the client area with a solid white colour
    	CBrush		Brush;
    	Brush.CreateSolidBrush(WHITE);
    
    	//fill area
    	m_ScrDC.FillRect(&Rect,&Brush);
    
    	//clean up 
    	::ReleaseDC(NULL,hdc);
    	Brush.DeleteObject();
    }
    
    void CMyDialog::OnPaint()
    {
    	CPaintDC dc(this); // device context for painting
    	CRect			Rect;
    	GetUpdateRect(Rect,0);
    	if(IsRectEmpty(Rect))
    		GetClientRect(Rect);
    	dc.BitBlt(Rect.left,Rect.top,Rect.Width(),Rect.Height(),&m_ScrDC,Rect.left,Rect.top,SRCCOPY);
    }
    
    void CMyDialog::OnClose()
    {
    	// test to ensure we do not try to release a NULL DC
    	if(m_ScrDC->m_hDC)
    	{
    		//put the original BMP back
    		m_ScrDC->SelectObject(m_ScrOrBmp);
    
    		//now the created BMP is not selected into a DC we can delete it
    		m_ScrBmp->DeleteObject();
    
    		//now the created DC is back to its original state we can delete it
    		m_ScrDC->DeleteDC();
    
    		//make sure our test is valid by setting the HDC member to NULL
    		m_ScrDC->m_hDC=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