CScrollView
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: CScrollView

  1. #1
    Join Date
    Jan 2002
    Location
    Bologna - Italy
    Posts
    10

    CScrollView

    I've created a view derived from CScrollView. In this view I want to display a graph with, on the left, a scale. The graph must scroll, the scale no. I don't know how obtain this: I've implement graph and scale but both of them scroll.

    Thank you



  2. #2
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    2,147

    Re: CScrollView


    Draw your graph, then draw the scales offset by the current scroll amount.

    You can get the current scroll amount by using:

    SCROLLINFO si;

    GetScrollInfo(SB_HORZ, &si, SIF_POS);
    int XPos = si.nPos;

    GetScrollInfo(SB_VERT, &si, SIF_POS);
    int YPos = si.nPos;




    Hope this helps,

    - Nigel



  3. #3
    Join Date
    Jan 2002
    Location
    Bologna - Italy
    Posts
    10

    Re: CScrollView

    Draw your graph, then draw the scales offset by the current scroll amount.

    Thank you NigelQ.
    I've have another problem:
    I've put that code in OnDraw but View is not automatically update.
    I try to insert this code:

    if (pDoc->scroll!=scll.x)
    {
    Invalidate();
    pDoc->scroll=scll.x;
    }

    but the scale flicks oh the background when I scroll the View.
    What do you mean with "offset by the current scroll amount"?
    I try to put on "OnPrepareDC":

    CRect rct;
    GetClientRect(&rct);
    CPoint p1=rct.TopLeft();
    CPoint p2=rct.BottomRight();
    int largh=p2.x-p1.x;
    CSize wv;
    wv.cx=largh/7;
    wv.cx=0;
    SetScaleToFitSize(wv);

    but that code doesn't work;

    Thank you again.



  4. #4
    Join Date
    Aug 2008
    Posts
    8

    Re: CScrollView

    I wanted to try something similar to this by putting adding code to OnVScroll and OnHScroll but I couldn't determine what measures the offset. Where do you put the code

    SCROLLINFO si;

    GetScrollInfo(SB_HORZ, &si, SIF_POS);
    int XPos = si.nPos;

    GetScrollInfo(SB_VERT, &si, SIF_POS);
    int YPos = si.nPos;


    I'm having a problem with scrolling in which the rectangles I have drawn will incorrectly draw over the already existing rectangles when I scroll in either direction. Also, the visible rectangles are completely redrawn when I minimize and then restore the window but as if the origin moved upon scrolling. 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?

  5. #5
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: CScrollView

    Hello,

    The implementation of CScrollView allows a developer to use a view in a zoomed way.
    Nice and perfect as long the implementation details fit your requirements.

    If not (for instance if someone wants to scroll only a part of the data or if someone wants to use
    double buffering for flicker free drawing) you need to go to the implementation details of this
    MFC class (or maybe you choose to implement scrolling your own way).

    Anyway:
    I've got a sample application which:
    - inherits the views from CScrollView
    - uses double buffering (for flicker free drawing)
    - has a scale (of course not to be scrolled)
    - is working (!).

    The approach is not really good from a point of a software engineer:
    I did not create a CScrollAndDoubleBufferedView class.

    Instead I use CScrollView to handle the scrolling stuff itself but in my OnDraw handler...
    ... oh oh! ...

    Actually I threw away the implementation of CScrollView.

    Since I use double buffering I do draw in a device context which is a bitmap.
    After finishing my drawing I BitBlt this bitmap (which is in a memory DC) onto the screen. Of course I do not create a virtual drawing buffer of the size of the whole (virtual) screen, but of the actual window size.

    All implementation of double buffer is included in
    Code:
    #ifdef ENABLE_MEMDC
     ... // this is the double buffering stuff
    #endif ENABLE_MEMDC
    so you can easily find out the details.

    Now when I'm drawing in the scrolled view I have to take care of the scrolling myself.
    Instead writing
    Code:
       pDC->LineTo(120, 220);
    I have to write
    Code:
       pUseDC->LineTo(120 - GetScrollPosition().x, 220 - GetScrollPosition().y);
    ( in "real world" examples 120 and 220 are variables coming from the documents data).

    If I want to draw something not to be scrolled (the headline, the scales, ...) I simply do not have to subtract
    the scolling position.

    Implementing I realized that because of the double buffering the (alredy optimized by MFC) OnHScroll, OnVScroll
    and OnMouseWheel handlers do not work correctly.
    I simply added a call to Invalidate() after I called the original handlers.
    This worked fine except that there was flickering...
    That's why I called LockWindowUpdate before the original handler is ivoked and UnlockWindowUpdate() after.
    No flickering occured anymore.

    The sample app is written in German language, that's why I insert a short manual ( ) here:

    - Add rectangles (nodes) to the document simply by drawing them with the left mouse key.
    - To check the performance: Try Ctrl+Z: This will insert 100 random rectangles.

    Known bugs:
    - Mouse wheel scrolling and opening a new rectangle at the same time produce pixel spam...
    - Not implemented: Deleting/Removing rectangles from the document...
    - The rectangles far left (0 to 50 pix) or far top (0 to 50 pix) are never visible.
    (There must be an additional offset to show the scales, I've been to laizy ...)
    - No serializing is implemented...

    Any comments to this solution of the scrolling view problem are welcome...

    With regards
    Programartist

    Attachment: VC project with pre-compiled executable included.
    Attached Files Attached Files

  6. #6
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: CScrollView

    Hello,

    I encountered a problem while calling LockWindowUpdate() and/or UnlockWindowUpdate(): At least under Win XP SP 2 the desktop (or other underlaying windows) flicker while scrolling in my app.

    Searching a solution I found this:

    I overwrote the virtual CScrollView:OnScrollBy(...) function. I put in the original code from CScrollView::OnScrollBy(...) and commented out the call to ScrollWindow(). As in the previous (published here before) version I call Invalidate() at the end of all calls to OnVScroll(), OnHScroll() and OnMouseWheel().

    This is now (at least under Win XP Sp 2) flicker free.

    With regards
    Programartist

    Attachment: Upgraded project

    Edit: Added how I did "implement" the overwritten OnScrollBy(..) function.
    Attached Files Attached Files
    Last edited by ProgramArtist; August 26th, 2008 at 03:15 AM. Reason: Added how I did "implement" the overwritten OnScrollBy(..) function.

  7. #7
    Join Date
    Oct 2017
    Posts
    1

    Re: CScrollView

    attachments can not be downloaded

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)