Click here to Skip to main content
15,922,155 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 214.3K   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

 
Questionhow to set the origin to center of view? Pin
bootflag1-Sep-15 16:43
bootflag1-Sep-15 16:43 
AnswerRe: how to set the origin to center of view? Pin
Roonglit Chareonsupkul1-Sep-15 18:15
Roonglit Chareonsupkul1-Sep-15 18:15 
QuestionOnPaint Problem Pin
Carl Banks14-Aug-14 21:10
Carl Banks14-Aug-14 21:10 
AnswerRe: OnPaint Problem Pin
Roonglit Chareonsupkul15-Aug-14 3:54
Roonglit Chareonsupkul15-Aug-14 3:54 
GeneralOnSize Problem Pin
MFCPlus3-Jan-11 20:10
MFCPlus3-Jan-11 20:10 
GeneralRe: OnSize Problem Pin
Carl Banks30-Aug-14 18:53
Carl Banks30-Aug-14 18:53 
GeneralCZoomView and drawing problem Pin
HillClimer2-Sep-09 19:44
HillClimer2-Sep-09 19:44 
QuestionCannot get this to work... Pin
ccaprani4-Aug-08 16:38
ccaprani4-Aug-08 16:38 
AnswerRe: Cannot get this to work... Pin
Roonglit Chareonsupkul7-Aug-08 6:24
Roonglit Chareonsupkul7-Aug-08 6:24 
GeneralRe: Cannot get this to work... Pin
ccaprani7-Aug-08 12:30
ccaprani7-Aug-08 12:30 
GeneralRe: Cannot get this to work... Pin
Roonglit Chareonsupkul9-Aug-08 17:25
Roonglit Chareonsupkul9-Aug-08 17:25 
GeneralRe: Cannot get this to work... Pin
ccaprani9-Aug-08 18:32
ccaprani9-Aug-08 18:32 
GeneralThanks for your source Pin
Jongsun Kim20-Mar-07 10:10
Jongsun Kim20-Mar-07 10:10 
QuestionHow to streatch the image? Pin
Zeus 19811-Nov-06 0:17
Zeus 19811-Nov-06 0:17 
AnswerRe: How to streatch the image? Pin
Roonglit Chareonsupkul1-Nov-06 2:16
Roonglit Chareonsupkul1-Nov-06 2:16 
GeneralRe: How to streatch the image? Pin
Zeus 19811-Nov-06 23:23
Zeus 19811-Nov-06 23:23 
GeneralRe: How to streatch the image? Pin
Roonglit Chareonsupkul2-Nov-06 2:02
Roonglit Chareonsupkul2-Nov-06 2:02 
GeneralRe: How to streatch the image? Pin
Zeus 19812-Nov-06 13:27
Zeus 19812-Nov-06 13:27 
GeneralProblems with Zooming out an image because of LPtoDP Pin
ShwethaBR27-Jul-06 0:05
ShwethaBR27-Jul-06 0:05 
GeneralZooming a picture Pin
Cosescu Bogdan17-Apr-06 1:52
Cosescu Bogdan17-Apr-06 1:52 
GeneralNice work,but.. Pin
ucc80126-Jan-06 3:15
ucc80126-Jan-06 3:15 
QuestionHow to Zoom Pin
jayaseela24-Jan-06 19:07
jayaseela24-Jan-06 19:07 
GeneralPrinting the view Pin
Alberto_Canabal9-Jan-06 4:45
Alberto_Canabal9-Jan-06 4:45 
GeneralRe: Printing the view Pin
Roonglit Chareonsupkul10-Jan-06 3:59
Roonglit Chareonsupkul10-Jan-06 3:59 
GeneralRe: Printing the view Pin
Alberto_Canabal10-Jan-06 6:15
Alberto_Canabal10-Jan-06 6:15 

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.