Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC CScrollView update problem when scrolling

MFC CScrollView update problem when scrolling

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    baumchen
    wrote on last edited by
    #1

    Hi, folks I met such a problem when implementing a CScrollView class. what i want to do is to show two images in one CScrollView instance, so I have the following code (below only shows one image in the left part of the window).

    void OnDraw(CDC* pDC)
    {
    CDC dcMem;
    CRect lRect;

    GetClientRect(lRect);
    lRect.NormalizeRect();

    dcMem.CreateCompatibleDC(pDC);

    CBitmap* pOldBitmap = (CBitmap*) dcMem.SelectObject(m_SourceBitmap);
    CPoint scrollPosition = GetScrollPosition();

    pDC->BitBlt(scrollPosition.x, scrollPosition.y, lRect.Width() / 2, lRect.Height(), &dcMem, scrollPosition.x, scrollPosition.y, SRCCOPY);

    DeleteDC(dcMem);
    }

    however, when I scroll the window, right half part of the window always has some problem, either too much background (scrolling left) or too few background (scrolling right). when I hide and show the window, everything is fine. alternatively, I tried calling Invalidate() from OnScroll, flicker happens because OnDraw is called multiple times. anyone got a solution? thanks! baum

    N 1 Reply Last reply
    0
    • B baumchen

      Hi, folks I met such a problem when implementing a CScrollView class. what i want to do is to show two images in one CScrollView instance, so I have the following code (below only shows one image in the left part of the window).

      void OnDraw(CDC* pDC)
      {
      CDC dcMem;
      CRect lRect;

      GetClientRect(lRect);
      lRect.NormalizeRect();

      dcMem.CreateCompatibleDC(pDC);

      CBitmap* pOldBitmap = (CBitmap*) dcMem.SelectObject(m_SourceBitmap);
      CPoint scrollPosition = GetScrollPosition();

      pDC->BitBlt(scrollPosition.x, scrollPosition.y, lRect.Width() / 2, lRect.Height(), &dcMem, scrollPosition.x, scrollPosition.y, SRCCOPY);

      DeleteDC(dcMem);
      }

      however, when I scroll the window, right half part of the window always has some problem, either too much background (scrolling left) or too few background (scrolling right). when I hide and show the window, everything is fine. alternatively, I tried calling Invalidate() from OnScroll, flicker happens because OnDraw is called multiple times. anyone got a solution? thanks! baum

      N Offline
      N Offline
      Nelek
      wrote on last edited by
      #2

      I don't know for sure if this will help you, but I used this (simplified version) and for me was working good.

      CClientDC dc(this); // Window to be drawn
      CDC dcNormMem; // To handle the bitmap
      dcNormMem.CreateCompatibleDC (&dc); // Assigning it to the window in compatible mode
      
      CString szTempText = "";
      CPoint cpTempPos, cpTempTextPos, cpScrollPoint = GetScrollPosition ();
      CBitmap bmNormIn;
      BITMAP bmNormTemp;
      
      if (bmNormIn.m\_hObject != NULL)
      	bmNormIn.DeleteObject ();		// Avoid duplicities
      
      // Load and select the bitmap to be placed
      bmNormIn.LoadBitmap (IDB\_NORMIN);  // Bitmap was in the resources
      bmNormIn.GetObject(sizeof(bmNormTemp), &bmNormTemp);	
      dcNormMem.SelectObject(&bmNormIn);
      
      // Correct Coordinates -> position on the screen
      cpTempPos.x = pDoc->m\_cmlInputSet\[nIn\].m\_cpInCoord.x - cpScrollPoint.x;
      cpTempPos.y = pDoc->m\_cmlInputSet\[nIn\].m\_cpInCoord.y - cpScrollPoint.y;
      		
      // Place the bitmap on the correct position
      dc.BitBlt(cpTempPos.x, cpTempPos.y, bmNormTemp.bmWidth, bmNormTemp.bmHeight, &dcNormMem, 0, 0, SRCCOPY);
      
      // Configuring and placing Name-Label of the Input
      szTempText = pDoc->m\_cmlInputSet\[nIn\].m\_szInName;
      dc.SetTextColor(RGB(0,0,0));
      dc.TextOut (cpTempPos.x, cpTempPos.y + SIGHEIGHT + 1, szTempText);
      

      .
      //... more code
      .
      dcNormMem.DeleteDC();
      ReleaseDC(&dc);

      and

      BOOL CMyView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
      {
      Invalidate ();
      UpdateWindow ();

      return CScrollView::OnScroll(nScrollCode, nPos, bDoScroll);
      

      }

      And for me was working good, I had up to 48 tiny Bitmaps on the screen and the Scroll of the window worked fine.

      Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpfull answers is nice, but saying thanks can be even nicer.

      B 1 Reply Last reply
      0
      • N Nelek

        I don't know for sure if this will help you, but I used this (simplified version) and for me was working good.

        CClientDC dc(this); // Window to be drawn
        CDC dcNormMem; // To handle the bitmap
        dcNormMem.CreateCompatibleDC (&dc); // Assigning it to the window in compatible mode
        
        CString szTempText = "";
        CPoint cpTempPos, cpTempTextPos, cpScrollPoint = GetScrollPosition ();
        CBitmap bmNormIn;
        BITMAP bmNormTemp;
        
        if (bmNormIn.m\_hObject != NULL)
        	bmNormIn.DeleteObject ();		// Avoid duplicities
        
        // Load and select the bitmap to be placed
        bmNormIn.LoadBitmap (IDB\_NORMIN);  // Bitmap was in the resources
        bmNormIn.GetObject(sizeof(bmNormTemp), &bmNormTemp);	
        dcNormMem.SelectObject(&bmNormIn);
        
        // Correct Coordinates -> position on the screen
        cpTempPos.x = pDoc->m\_cmlInputSet\[nIn\].m\_cpInCoord.x - cpScrollPoint.x;
        cpTempPos.y = pDoc->m\_cmlInputSet\[nIn\].m\_cpInCoord.y - cpScrollPoint.y;
        		
        // Place the bitmap on the correct position
        dc.BitBlt(cpTempPos.x, cpTempPos.y, bmNormTemp.bmWidth, bmNormTemp.bmHeight, &dcNormMem, 0, 0, SRCCOPY);
        
        // Configuring and placing Name-Label of the Input
        szTempText = pDoc->m\_cmlInputSet\[nIn\].m\_szInName;
        dc.SetTextColor(RGB(0,0,0));
        dc.TextOut (cpTempPos.x, cpTempPos.y + SIGHEIGHT + 1, szTempText);
        

        .
        //... more code
        .
        dcNormMem.DeleteDC();
        ReleaseDC(&dc);

        and

        BOOL CMyView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
        {
        Invalidate ();
        UpdateWindow ();

        return CScrollView::OnScroll(nScrollCode, nPos, bDoScroll);
        

        }

        And for me was working good, I had up to 48 tiny Bitmaps on the screen and the Scroll of the window worked fine.

        Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpfull answers is nice, but saying thanks can be even nicer.

        B Offline
        B Offline
        baumchen
        wrote on last edited by
        #3

        Nelek thanks for the reply. Do you have a counter to count how many times OnDraw is called per scroll? in my code, I found it was called 2 or 3 times per scroll, and it caused flicker when scrolling (when dragging the scrollbar). any clue? thanks!

        N 1 Reply Last reply
        0
        • B baumchen

          Nelek thanks for the reply. Do you have a counter to count how many times OnDraw is called per scroll? in my code, I found it was called 2 or 3 times per scroll, and it caused flicker when scrolling (when dragging the scrollbar). any clue? thanks!

          N Offline
          N Offline
          Nelek
          wrote on last edited by
          #4

          No, I didn't use a counter. For me the problem of flickering was when I was moving the signals or placing a conection between two signals because I was calling Invalidate and UpdateWindow inside OnMouseMove so I was expecting that fliccker, but with the clicks of mouse or when scrolling it was working fine. In the worst case, if you know that it is getting updated more times you can use a bool to draw only the first call (you can do it false when the mouse moves or at button release). I know it is not an elegant solution but at least can help while you search for the real issue. On the other hand, I am sorry I can not help you more. My previous laptop died and I have no MS in this one, I am speaking with my boss to Install MS 2008 but not have it yet. By the way... which version of VC are you using?

          Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpfull answers is nice, but saying thanks can be even nicer.

          B 1 Reply Last reply
          0
          • N Nelek

            No, I didn't use a counter. For me the problem of flickering was when I was moving the signals or placing a conection between two signals because I was calling Invalidate and UpdateWindow inside OnMouseMove so I was expecting that fliccker, but with the clicks of mouse or when scrolling it was working fine. In the worst case, if you know that it is getting updated more times you can use a bool to draw only the first call (you can do it false when the mouse moves or at button release). I know it is not an elegant solution but at least can help while you search for the real issue. On the other hand, I am sorry I can not help you more. My previous laptop died and I have no MS in this one, I am speaking with my boss to Install MS 2008 but not have it yet. By the way... which version of VC are you using?

            Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpfull answers is nice, but saying thanks can be even nicer.

            B Offline
            B Offline
            baumchen
            wrote on last edited by
            #5

            I am using a rather old vc, 2003. It is amazing to see that mspaint doesn't have any flicker when you drag the mouse and scroll it around. dunno how they achieved that. I tried creating a DDB bitmap, but it doesn't help.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups