Scroll problem with zooming in CScrollView
I've been two days at this problem and can't figure it out - I'd really
appreciate your advice and time to help me :)
The problem is this:
My application draws a road and shows vehicles moving on it. When scrolled
right and I zoom in, the scrollbars move to the left, back towards the
origin. Conversly, when I zoom out they scroll to the right. As you can see
from the code below I have tried adjusting for the viewport origin, but
still no luck.
Some more information:
My logical coords have their origin at the start and centre of the road. I
therefore set my viewport origin at (left border, Rect.Height()/2). To scale
I use a scale factor times my draw area. I am not using Codeguru's CZoomView
because I need the above special considerations.
Here is the code I have:
void CTrafficView::OnPrepareDC(CDC *pDC, CPrintInfo *pInfo){CScrollView::OnPrepareDC(pDC,pInfo); pDC->SetMapMode(MM_ANISOTROPIC); // for scaling
pDC->SetWindowExt(m_DrawArea); // in logical units int xExtent = m_Scale *
m_DrawArea.cx; int yExtent = m_Scale * m_DrawArea.cy;pDC->SetViewportExt(xExtent,yExtent); // keep the road divider in the centre of screen // by moving the
viewport origin GetClientRect(&m_ClientRect); // the height of the windowCPointviewOrg; viewOrg.y = m_ClientRect.Height()/2; // - yExtent/2; viewOrg.x =
m_Border_Lhs*m_Scale; viewOrg -= GetDeviceScrollPosition(); // so scrolling
accounted for pDC->SetViewportOrg(viewOrg);}void
CTrafficView::ResetScrollBars(){ // set up device context and new scroll
sizes CClientDC aDC(this); OnPrepareDC(&aDC); CSize temp = m_DrawArea; //
since we don't want drawArea changed aDC.LPtoDP(&temp);SetScrollSizes(MM_TEXT,temp);}void CTrafficView::OnToolsZoomin() { // move scrolls bars to centre
of logical view CPoint pt; GetClientRect(&m_ClientRect); pt.x =
(m_ClientRect.Width() / 2); pt.y = (m_ClientRect.Height() / 2);CClientDCaDC(this); OnPrepareDC(&aDC); CPoint vp = aDC.GetViewportOrg(); pt = pt -
vp; // adjust device point to viewport pointCScrollView::ScrollToPosition(pt); //scroll to viewport point m_Scale += 0.2; // MAGIC NUMBER - should it be
an option? ResetScrollBars(); Invalidate(); // draw it}
Thanks so much for any help you can give - I have to present my program at a
meeting with the client tomorrow :(