Click here to Skip to main content
Click here to Skip to main content
Go to top

CZoomView

, 7 Oct 2004
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Roonglit Chareonsupkul
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

 
QuestionOnPaint Problem PinmemberCarl Banks14-Aug-14 21:10 
AnswerRe: OnPaint Problem PinmemberRoonglit Chareonsupkul15-Aug-14 3:54 
GeneralOnSize Problem PinmemberMFCPlus3-Jan-11 20:10 
GeneralRe: OnSize Problem PinmemberCarl Banks30-Aug-14 18:53 
GeneralCZoomView and drawing problem PinmemberHillClimer2-Sep-09 19:44 
GeneralThanks for your source PinmemberJongsun Kim20-Mar-07 10:10 
QuestionHow to streatch the image? PinmemberZeus 19811-Nov-06 0:17 
AnswerRe: How to streatch the image? PinmemberRoongrit Charoensupkul1-Nov-06 2:16 
GeneralRe: How to streatch the image? PinmemberZeus 19811-Nov-06 23:23 
GeneralRe: How to streatch the image? PinmemberRoongrit Charoensupkul2-Nov-06 2:02 
GeneralRe: How to streatch the image? PinmemberZeus 19812-Nov-06 13:27 
GeneralProblems with Zooming out an image because of LPtoDP PinmemberShwethaBR27-Jul-06 0:05 
GeneralZooming a picture PinmemberCosescu Bogdan17-Apr-06 1:52 
GeneralNice work,but.. Pinmemberucc80126-Jan-06 3:15 
QuestionHow to Zoom Pinmemberjayaseela24-Jan-06 19:07 
GeneralPrinting the view PinmemberAlberto_Canabal9-Jan-06 4:45 
GeneralRe: Printing the view PinmemberRoongrit Charoensupkul10-Jan-06 3:59 
GeneralRe: Printing the view PinmemberAlberto_Canabal10-Jan-06 6:15 
GeneralRe: Printing the view (ouch) PinmemberAlberto_Canabal19-Jan-06 0:32 
GeneralZooming Centering not perfect Pinmembervipinasda8-Jan-06 10:40 
GeneralRe: Zooming Centering not perfect Pinmembervipinasda8-Jan-06 10:52 
GeneralRe: Zooming Centering not perfect PinmemberRoongrit Charoensupkul10-Jan-06 3:56 
GeneralRe: Zooming Centering not perfect PinmemberVipin Aravind13-Jan-06 10:40 
GeneralRe: Zooming Centering not perfect PinmemberRoongrit Charoensupkul15-Jan-06 2:12 
GeneralScrolling PinmemberAlberto_Canabal5-Dec-05 2:51 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 8 Oct 2004
Article Copyright 2004 by Roonglit Chareonsupkul
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid