CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2001
    Location
    JiangSu,China
    Posts
    63

    a simple problem about CDC

    SDI, ScrollView, add OnPaint() for scrollview
    in OnPaint()
    Code:
    dc.FillSolidRect(CRect(0, 0, 78, 30), RGB(0xff, 0, 0xff));
    the problem is when I scroll the view, the background will become dirty.
    how to avoid this?

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: a simple problem about CDC

    Originally posted by niry
    the problem is when I scroll the view, the background will become dirty.
    Dirty? What do you mean by dirty? Anyway, in an MFC app which uses doc/view, you should do the drawing in OnDraw(), not OnPaint().

  3. #3
    Join Date
    Jul 2001
    Location
    JiangSu,China
    Posts
    63

    Re: Re: a simple problem about CDC

    Originally posted by gstercken
    Dirty? What do you mean by dirty? Anyway, in an MFC app which uses doc/view, you should do the drawing in OnDraw(), not OnPaint().
    dirty: the background is painted by the same color as the rect

    yes, in OnDraw, drawing is performed in device screen, so scroll will not cause background ditry
    But in OnPaint, drawing is in logic screen. The drawing area disgards current scroll position. so will make background ditry when scrolling.
    I 've solved the problem, that is throught call Invalidate to force the client area invalidate.
    you have helped me solving lots of question, really thanks

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Re: Re: a simple problem about CDC

    Originally posted by niry
    yes, in OnDraw, drawing is performed in device screen, so scroll will not cause background ditry
    But in OnPaint, drawing is in logic screen. The drawing area disgards current scroll position. so will make background ditry when scrolling.
    You probably mean device units and logical units. But that's not the reason: It is rather that CView's implementation of OnPaint() call's OnPrepareDC() before calling OnDraw(), which does a lot of additional things in CScrollView's implementation. That's why you shouldn't implement OnPaint() yourself, or you'll never get CScrollView working correctly.

  5. #5
    Join Date
    Jul 2001
    Location
    JiangSu,China
    Posts
    63

    Re: Re: Re: Re: a simple problem about CDC

    Originally posted by gstercken
    You probably mean device units and logical units. But that's not the reason: It is rather that CView's implementation of OnPaint() call's OnPrepareDC() before calling OnDraw(), which does a lot of additional things in CScrollView's implementation. That's why you shouldn't implement OnPaint() yourself, or you'll never get CScrollView working correctly.
    I 've get the scrollview working correctly
    Call Invalidate in OnPaint()
    I know OnPrepareDC(), it just transform logic units to device units, if in CView, it does nothing.

  6. #6
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Re: Re: Re: Re: a simple problem about CDC

    Originally posted by niry
    I 've get the scrollview working correctly
    Call Invalidate in OnPaint()
    No, no, no. It just seems to work correctly. And you should never, ever call Invalidate() in OnPaint()!. If you handle OnPaint() as you are doing in a SCrollView, you'll get a lot of problems later, believe me.
    Originally posted by niry
    I know OnPrepareDC(), it just transform logic units to device units
    No, that's not true, it does a lot more: It does some sanity checks in debug builds, it sets the mapping mode as needed for scrolling, handles scale-to-fit, sets the viewport origin and extension, and corretly sets up the DC for printing.
    Originally posted by niry
    if in CView, it does nothing.
    That's not true either: It handles the page count when OnDraw() is called during printing.

    I don't understand why you keep doing it the wrong way. Just move your drawing code to OnDraw(), remove your OnPaint() implementation, and your code will work fine - also in the future, and for printing.

  7. #7
    Join Date
    Aug 2008
    Posts
    8

    Re: a simple problem about CDC

    I'm having a similar problem where I'm trying to draw rectangles but they incorrectly draw over themselves when I scroll in either direction. I call my function to draw the rectangles in OnDraw. It works to redraw the rectangles when I minimize and restore a window but the origin moves upon scrolling as if there scrolling never happened. My code to draw the rectangles is

    CRect TempRect = pDoc->GetNode(i); // The points for the rectangle
    OnPrepareDC(pDC);
    pDC->DPtoLP(&TempRect);
    pDC->Rectangle(TempRect);

    and my code to set scroll sizes is in OnInitialUpdate as

    CSize Workspace(1000, 1000);
    SetScrollSizes(MM_LOENGLISH, Workspace);

    I have tried a variety of things in OnPrepareDC to fix including setting mapping modes and setting the origin to 0,0.

    Any ideas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured