65.9K
CodeProject is changing. Read more.
Home

CZoomView

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.44/5 (27 votes)

Jun 9, 2004

Eclipse

1 min read

viewsIcon

218552

downloadIcon

7127

A class that provides "zoom" feature

Introduction

This class handles zooming feature based on CScrollView. It provides functions to set the scale of the application easily.

Using the code

Create a Doc/View application by using application wizard. Change your CView class to inherit from CZoomView instead of CView or CScrollView. And that's it, your application has zoom feature. When you want to set the scale of your application, you just call SetZoomScale() method.

void CDemoZoomView::OnViewZoomin() 
{
    SetZoomScale(m_zoomFactor + 1.0f);
}

void CDemoZoomView::OnViewZoomout() 
{
    SetZoomScale(m_zoomFactor - 1.0f);
}

Points of Interest

When I started to develop the application that wanted zoom feature, I see other guys have to create scale variable and multiply this variable in all drawing functions. It is not easy to use. So, I tried to find how to set scale in one place and that applies to all drawing code. Fortunately, there are some mapping modes that can set ratio between viewport and window area. And MM_ISOTROPIC is the answer. We can set the ratio by calling SetWindowExt() and SetViewPortExt().

int CZoomView::SetMapMode(CDC* pDC)
{
    int previousMode = pDC->SetMapMode(MM_ISOTROPIC);
    pDC->SetWindowExt(100,100);
    pDC->SetViewportExt(FloatToInt(100*m_zoomFactor),FloatToInt(100*m_zoomFactor));
    
    return previousMode;
}

SetWindowExt() and SetViewPortExt() are the functions of the CDC class. If we want them easy to use, the user should not know what we do with the instance of the CDC class. So, my CZoomView has the instance of CDC class. This instance will be sent through OnDraw() function. The user will call normal drawing functions and the zoom feature will apply automatically.

Logical point and Device point

Because CZoomView is based on CScrollView, so there are logical points and device points to concern. CZoomView provides DPtoLP and LPtoDP functions. User can use it as usual with CDC instance.

void CDemoZoomView::OnLButtonDown(UINT nFlags, CPoint point) 
{
    if (m_bSelectMode == FALSE) 
    {
        m_bSelectMode = TRUE;
        m_ptStart = point;
        DPtoLP(&m_ptStart);
        m_rubberBand.SetRect(m_ptStart, m_ptStart);
        Invalidate(FALSE);
    }
    
    CZoomView::OnLButtonDown(nFlags, point);
}

History

  • 30 July 2004
    • Reduce unnecessary bitmap allocation in flicker-free handling.
  • 5 June 2004
    • Added CZoomView that you may used instead of CScrollView class.