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.
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":
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
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
( 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.
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.
Last edited by ProgramArtist; August 26th, 2008 at 02:15 AM.
Reason: Added how I did "implement" the overwritten OnScrollBy(..) function.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.