Click here to Skip to main content
15,891,529 members
Articles / Desktop Programming / MFC
Article

CZoomView

Rate me:
Please Sign up or sign in to vote.
4.44/5 (29 votes)
7 Oct 2004Eclipse1 min read 212.8K   7.1K   68   66
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.

License

This article, along with any associated source code and files, is licensed under The Eclipse Public License 1.0


Written By
Web Developer
Thailand Thailand
Roonglit is a senior analyst programmer at DST International (Thailand) Ltd. He graduated from Chulalongkorn University. He's been programming since 2000. His programming experience includes C/C++, OpenGL, DirectX, Java, MFC, ASP.NET, PHP. He has worked on both Windows and Linux Platform.


Comments and Discussions

 
GeneralRe: scrolling to correct position Pin
Roonglit Chareonsupkul4-Jan-05 6:01
Roonglit Chareonsupkul4-Jan-05 6:01 
QuestionHow to draw line Pin
sp_kip@mail.ru23-Dec-04 10:42
sp_kip@mail.ru23-Dec-04 10:42 
AnswerRe: How to draw line Pin
Roonglit Chareonsupkul23-Dec-04 18:57
Roonglit Chareonsupkul23-Dec-04 18:57 
GeneralRe: How to draw line Pin
sp_kip@mail.ru24-Dec-04 0:52
sp_kip@mail.ru24-Dec-04 0:52 
GeneralRe: How to draw line Pin
sp_kip@mail.ru24-Dec-04 1:14
sp_kip@mail.ru24-Dec-04 1:14 
GeneralRequest Pin
Member 26654617-Nov-04 20:48
Member 26654617-Nov-04 20:48 
GeneralRe: Request Pin
Roonglit Chareonsupkul21-Nov-04 20:09
Roonglit Chareonsupkul21-Nov-04 20:09 
GeneralFit print/preview Pin
jcdemo19-Oct-04 19:47
jcdemo19-Oct-04 19:47 
How would you go about adding a function "fit to window", and then print/preview that would also fit to the page as is on the screen?

Jean-Claude
GeneralZooming Text Pin
Martin Hinchy16-Sep-04 14:48
Martin Hinchy16-Sep-04 14:48 
GeneralRe: Zooming Text Pin
Roonglit Chareonsupkul7-Oct-04 19:40
Roonglit Chareonsupkul7-Oct-04 19:40 
GeneralRe: Zooming Text Pin
Martin Hinchy7-Oct-04 19:53
Martin Hinchy7-Oct-04 19:53 
GeneralRe: Zooming Text Pin
Roonglit Chareonsupkul7-Oct-04 22:17
Roonglit Chareonsupkul7-Oct-04 22:17 
GeneralRe: Zooming Text Pin
Martin Hinchy10-Oct-04 12:17
Martin Hinchy10-Oct-04 12:17 
QuestionHow to draw a DIB? Pin
degodevel15-Jun-04 2:32
degodevel15-Jun-04 2:32 
AnswerRe: How to draw a DIB? Pin
Roonglit Chareonsupkul17-Jun-04 1:00
Roonglit Chareonsupkul17-Jun-04 1:00 
QuestionDouble-buffering? Pin
compiler10-Jun-04 6:50
compiler10-Jun-04 6:50 
AnswerRe: Double-buffering? Pin
Roonglit Chareonsupkul10-Jun-04 16:48
Roonglit Chareonsupkul10-Jun-04 16:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.