locked
Flickering effect RRS feed

  • Question

  • Hello,

    Here is my code: https://github.com/emrecpp/VAF

    I want to fix :

    • Refresh effect(i think flickering): http://sendvid.com/djz6eez9


    Thanks :)





    Sunday, February 11, 2018 3:24 PM

All replies

  • Can you tell us exactly what is the problem you are facing here. And for flickering which usually happen due to paint operation on your window did you tried using WM_ERASEBKGND or double buffer technique . There are so many article on google for this.

    Thanks


    Rupesh Shukla

    • Proposed as answer by Baron Bi Monday, February 12, 2018 8:05 AM
    Monday, February 12, 2018 12:09 AM
  • I tried WM_ERASEBKGND but it did not work.

    You can see my codes in here:

    https://github.com/emrecpp/VAF/tree/master/VAF


    struct bilgiler { string ad ="Emre Demircan"; int yıl = 2000; string fb = "fb.com/DeatSlayer"; }bilgi;

    Monday, February 12, 2018 3:18 PM
  • it will be good as well , if you specify what version of Visual studio and window you are using here. I had tried using your code but unfortunately i don't have any capturing device for it and that's why i am not able to run it. Did you tried double buffering . Or did you tried to debug your code to make sure whether it is flickering or you are sending message to window for repaint etc.

    Thanks


    Rupesh Shukla


    • Edited by Pintu Shukla Thursday, February 15, 2018 4:55 PM
    Tuesday, February 13, 2018 4:23 PM
  • it will be good as well , if you specify what version of Visual studio and window you are using here. I had tried using your code but unfortunately i don't have any capturing device for it and that's why i am not able to run it. Did you tried double buffering . Or did you tried to debug your code to make sure whether it is flickering or you are sending message to window for repaint etc.

    Thanks


    Rupesh Shukla

    I am drawing rectangle, did you look at my codes?

    struct bilgiler { string ad ="Emre Demircan"; int yıl = 2000; string fb = "fb.com/DeatSlayer"; }bilgi;

    Wednesday, February 14, 2018 1:06 PM
  • Here is a good (old) double buffering class: https://www.codeproject.com/Articles/33/Flicker-Free-Drawing-In-MFC

    HTH, Guido

    Wednesday, February 14, 2018 1:21 PM
  • Here is a good (old) double buffering class: https://www.codeproject.com/Articles/33/Flicker-Free-Drawing-In-MFC

    HTH, Guido

    I can't compile it and I did not understand :/

    Do you have simple example?

    Thanks


    struct bilgiler { string ad ="Emre Demircan"; int yıl = 2000; string fb = "fb.com/DeatSlayer"; }bilgi;

    Thursday, March 1, 2018 3:07 PM
  • Here is a good (old) double buffering class: https://www.codeproject.com/Articles/33/Flicker-Free-Drawing-In-MFC

    I can't compile it and I did not understand :/

    Do you have simple example?


    You only need this good CMemDC of author Keith Rule.

    Header file for memdc.h:

    #ifndef _MEMDC_H_
    #define _MEMDC_H_
     
    //////////////////////////////////////////////////
    // CMemDC - memory DC
    //
    // Author: Keith Rule
    // Email:  keithr@europa.com
    // Copyright 1996-2002, Keith Rule
    //
    // You may freely use or modify this code provided this
    // Copyright is included in all derived versions.
    //
    // History - 10/3/97 Fixed scrolling bug.
    //               Added print support. - KR
    //
    //       11/3/99 Fixed most common complaint. Added
    //            background color fill. - KR
    //
    //       11/3/99 Added support for mapping modes other than
    //            MM_TEXT as suggested by Lee Sang Hun. - KR
    //
    //       02/11/02 Added support for CScrollView as supplied
    //             by Gary Kirkham. - KR
    //
    // This class implements a memory Device Context which allows
    // flicker free drawing.
     
    class CMemDC : public CDC {
    private:       
        CBitmap    m_bitmap;        // Offscreen bitmap
        CBitmap*       m_oldBitmap; // bitmap originally found in CMemDC
        CDC*       m_pDC;           // Saves CDC passed in constructor
        CRect      m_rect;          // Rectangle of drawing area.
        BOOL       m_bMemDC;        // TRUE if CDC really is a Memory DC.
    public:
        
        CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
        {
            ASSERT(pDC != NULL); 
     
            // Some initialization
            m_pDC = pDC;
            m_oldBitmap = NULL;
            m_bMemDC = !pDC->IsPrinting();
     
            // Get the rectangle to draw
            if (pRect == NULL) {
                 pDC->GetClipBox(&m_rect);
            } else {
                 m_rect = *pRect;
            }
     
            if (m_bMemDC) {
                 // Create a Memory DC
                 CreateCompatibleDC(pDC);
                 pDC->LPtoDP(&m_rect);
     
                 m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), 
                                                      m_rect.Height());
                 m_oldBitmap = SelectObject(&m_bitmap);
     
                 SetMapMode(pDC->GetMapMode());
     
                 SetWindowExt(pDC->GetWindowExt());
                 SetViewportExt(pDC->GetViewportExt());
     
                 pDC->DPtoLP(&m_rect);
                 SetWindowOrg(m_rect.left, m_rect.top);
            } else {
                 // Make a copy of the relevent parts of the current 
                 // DC for printing
                 m_bPrinting = pDC->m_bPrinting;
                 m_hDC       = pDC->m_hDC;
                 m_hAttribDC = pDC->m_hAttribDC;
            }
     
            // Fill background 
            FillSolidRect(m_rect, pDC->GetBkColor());
        }
        
        ~CMemDC()      
        {          
            if (m_bMemDC) {
                 // Copy the offscreen bitmap onto the screen.
                 m_pDC->BitBlt(m_rect.left, m_rect.top, 
                               m_rect.Width(),  m_rect.Height(),
                      this, m_rect.left, m_rect.top, SRCCOPY);            
                 
                 //Swap back the original bitmap.
                 SelectObject(m_oldBitmap);        
            } else {
                 // All we need to do is replace the DC with an illegal
                 // value, this keeps us from accidentally deleting the 
                 // handles associated with the CDC that was passed to 
                 // the constructor.              
                 m_hDC = m_hAttribDC = NULL;
            }       
        }
        
        // Allow usage as a pointer    
        CMemDC* operator->() 
        {
            return this;
        }       
     
        // Allow usage as a pointer    
        operator CMemDC*() 
        {
            return this;
        }
    };
     
    #endif

    In your app, change OnEraseBackground:

    BOOL CMyView::OnEraseBkgnd(CDC* pDC) 
    {
          return FALSE;
    }

    and insert CMemDC in e.g. your OnDraw function:

    void CMyView::OnDraw(CDC* pthedc)
    {
    	CMemDC pDC(pthedc);  // insert this
    	// .. do drawing with pDC
    }

    This will avoid flickering.

    If your compiler complains the class name CMemDC, then rename it to CMyMemDC.

    Regards, Guido


    Friday, March 2, 2018 10:11 AM
  • Here is a good (old) double buffering class: https://www.codeproject.com/Articles/33/Flicker-Free-Drawing-In-MFC

    I can't compile it and I did not understand :/

    Do you have simple example?


    You only need this good CMemDC of author Keith Rule.

    Header file for memdc.h:

    #ifndef _MEMDC_H_
    #define _MEMDC_H_
     
    //////////////////////////////////////////////////
    // CMemDC - memory DC
    //
    // Author: Keith Rule
    // Email:  keithr@europa.com
    // Copyright 1996-2002, Keith Rule
    //
    // You may freely use or modify this code provided this
    // Copyright is included in all derived versions.
    //
    // History - 10/3/97 Fixed scrolling bug.
    //               Added print support. - KR
    //
    //       11/3/99 Fixed most common complaint. Added
    //            background color fill. - KR
    //
    //       11/3/99 Added support for mapping modes other than
    //            MM_TEXT as suggested by Lee Sang Hun. - KR
    //
    //       02/11/02 Added support for CScrollView as supplied
    //             by Gary Kirkham. - KR
    //
    // This class implements a memory Device Context which allows
    // flicker free drawing.
     
    class CMemDC : public CDC {
    private:       
        CBitmap    m_bitmap;        // Offscreen bitmap
        CBitmap*       m_oldBitmap; // bitmap originally found in CMemDC
        CDC*       m_pDC;           // Saves CDC passed in constructor
        CRect      m_rect;          // Rectangle of drawing area.
        BOOL       m_bMemDC;        // TRUE if CDC really is a Memory DC.
    public:
        
        CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
        {
            ASSERT(pDC != NULL); 
     
            // Some initialization
            m_pDC = pDC;
            m_oldBitmap = NULL;
            m_bMemDC = !pDC->IsPrinting();
     
            // Get the rectangle to draw
            if (pRect == NULL) {
                 pDC->GetClipBox(&m_rect);
            } else {
                 m_rect = *pRect;
            }
     
            if (m_bMemDC) {
                 // Create a Memory DC
                 CreateCompatibleDC(pDC);
                 pDC->LPtoDP(&m_rect);
     
                 m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), 
                                                      m_rect.Height());
                 m_oldBitmap = SelectObject(&m_bitmap);
     
                 SetMapMode(pDC->GetMapMode());
     
                 SetWindowExt(pDC->GetWindowExt());
                 SetViewportExt(pDC->GetViewportExt());
     
                 pDC->DPtoLP(&m_rect);
                 SetWindowOrg(m_rect.left, m_rect.top);
            } else {
                 // Make a copy of the relevent parts of the current 
                 // DC for printing
                 m_bPrinting = pDC->m_bPrinting;
                 m_hDC       = pDC->m_hDC;
                 m_hAttribDC = pDC->m_hAttribDC;
            }
     
            // Fill background 
            FillSolidRect(m_rect, pDC->GetBkColor());
        }
        
        ~CMemDC()      
        {          
            if (m_bMemDC) {
                 // Copy the offscreen bitmap onto the screen.
                 m_pDC->BitBlt(m_rect.left, m_rect.top, 
                               m_rect.Width(),  m_rect.Height(),
                      this, m_rect.left, m_rect.top, SRCCOPY);            
                 
                 //Swap back the original bitmap.
                 SelectObject(m_oldBitmap);        
            } else {
                 // All we need to do is replace the DC with an illegal
                 // value, this keeps us from accidentally deleting the 
                 // handles associated with the CDC that was passed to 
                 // the constructor.              
                 m_hDC = m_hAttribDC = NULL;
            }       
        }
        
        // Allow usage as a pointer    
        CMemDC* operator->() 
        {
            return this;
        }       
     
        // Allow usage as a pointer    
        operator CMemDC*() 
        {
            return this;
        }
    };
     
    #endif

    In your app, change OnEraseBackground:

    BOOL CMyView::OnEraseBkgnd(CDC* pDC) 
    {
          return FALSE;
    }

    and insert CMemDC in e.g. your OnDraw function:

    void CMyView::OnDraw(CDC* pthedc)
    {
    	CMemDC pDC(pthedc);  // insert this
    	// .. do drawing with pDC
    }

    This will avoid flickering.

    If your compiler complains the class name CMemDC, then rename it to CMyMemDC.

    Regards, Guido


    I am creating form in console, my application is not windows form application.


    struct bilgiler { string ad ="Emre Demircan"; int yıl = 2000; string fb = "fb.com/DeatSlayer"; }bilgi;

    Saturday, March 3, 2018 3:36 PM
  • Do you use stdafx.h before you include this header? There afxwin.h is included.
    Monday, March 5, 2018 7:00 AM
  • Do you use stdafx.h before you include this header? There afxwin.h is included.

    I have not stdafx.h and afxwin.h, I think I should download kit, but which one?

    

    struct bilgiler { string ad ="Emre Demircan"; int yıl = 2000; string fb = "fb.com/DeatSlayer"; }bilgi;

    Monday, March 5, 2018 2:46 PM
  • Ok, you want to use double buffering in Win32 app.

    Have a look here: https://stackoverflow.com/questions/1541492/how-to-code-for-reducing-flicker-need-your-help-with-code-win32-vc-flicker

    Change your code for WM_PAINT as described in the link:

    hdc = BeginPaint(hwnd,&ps); // actual screen DC
    
    hdcBuffer = CreateCompatibleDC (hdc)  // OFF screen DC
    
    hBitmapBuffer = CreateCompatibleBitmap (hdc,BitmapWidth, BitmapHeight);  // create memory bitmap for that off screen DC
    
    SelectObject(hdcBuffer,hBitmapBuffer); // Select the created memory bitmap into the OFF screen DC
    
    /* Then do your painting job using hdcBuffer over off screen DC */
    
    BitBlt(hdc,0,0,BitmapWidth,BitmapHeight,hdcBuffer,0,0,SRCCOPY); // copy the content of OFF screen DC to actual screen DC
    
    DeleteDC (hdcBuffer); // Release the OFF screen DC
    
    DeleteObject (hBitmapBuffer); // Free the memory for bitmap
    
    EndPaint(hwnd,&ps); // Release the actual screen DC

    Then add WM_ERASEBKGND message and only return TRUE.

    Regards, Guido

    Tuesday, March 6, 2018 7:04 AM
  • Ok, you want to use double buffering in Win32 app.

    Have a look here: https://stackoverflow.com/questions/1541492/how-to-code-for-reducing-flicker-need-your-help-with-code-win32-vc-flicker

    Change your code for WM_PAINT as described in the link:

    hdc = BeginPaint(hwnd,&ps); // actual screen DC
    
    hdcBuffer = CreateCompatibleDC (hdc)  // OFF screen DC
    
    hBitmapBuffer = CreateCompatibleBitmap (hdc,BitmapWidth, BitmapHeight);  // create memory bitmap for that off screen DC
    
    SelectObject(hdcBuffer,hBitmapBuffer); // Select the created memory bitmap into the OFF screen DC
    
    /* Then do your painting job using hdcBuffer over off screen DC */
    
    BitBlt(hdc,0,0,BitmapWidth,BitmapHeight,hdcBuffer,0,0,SRCCOPY); // copy the content of OFF screen DC to actual screen DC
    
    DeleteDC (hdcBuffer); // Release the OFF screen DC
    
    DeleteObject (hBitmapBuffer); // Free the memory for bitmap
    
    EndPaint(hwnd,&ps); // Release the actual screen DC

    Then add WM_ERASEBKGND message and only return TRUE.

    Regards, Guido

    Thanks a lot for your reply.

    I couldn't :/ Can you do and give me source, https://github.com/emrecpp/VAF/tree/master/VAF

    Thankful!


    struct bilgiler { string ad ="Emre Demircan"; int yıl = 2000; string fb = "fb.com/DeatSlayer"; }bilgi;

    Tuesday, March 6, 2018 2:23 PM