Click here to Skip to main content
Licence Eclipse
First Posted 8 Jun 2004
Views 135,641
Bookmarked 58 times

CZoomView

By | 7 Oct 2004 | Article
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

About the Author

Roonglit Chareonsupkul

Web Developer

Thailand Thailand

Member

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.
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralOnSize Problem PinmemberMFCPlus20:10 3 Jan '11  
GeneralCZoomView and drawing problem PinmemberHillClimer19:44 2 Sep '09  
QuestionCannot get this to work... Pinmemberccaprani16:38 4 Aug '08  
AnswerRe: Cannot get this to work... PinmemberRoonglit Chareonsupkul6:24 7 Aug '08  
GeneralRe: Cannot get this to work... Pinmemberccaprani12:30 7 Aug '08  
GeneralRe: Cannot get this to work... PinmemberRoonglit Chareonsupkul17:25 9 Aug '08  
GeneralRe: Cannot get this to work... Pinmemberccaprani18:32 9 Aug '08  
GeneralThanks for your source PinmemberJongsun Kim10:10 20 Mar '07  
QuestionHow to streatch the image? PinmemberZeus 19810:17 1 Nov '06  
AnswerRe: How to streatch the image? PinmemberRoongrit Charoensupkul2:16 1 Nov '06  
GeneralRe: How to streatch the image? PinmemberZeus 198123:23 1 Nov '06  
GeneralRe: How to streatch the image? PinmemberRoongrit Charoensupkul2:02 2 Nov '06  
GeneralRe: How to streatch the image? PinmemberZeus 198113:27 2 Nov '06  
GeneralProblems with Zooming out an image because of LPtoDP PinmemberShwethaBR0:05 27 Jul '06  
GeneralZooming a picture PinmemberCosescu Bogdan1:52 17 Apr '06  
GeneralNice work,but.. Pinmemberucc8013:15 26 Jan '06  
QuestionHow to Zoom Pinmemberjayaseela19:07 24 Jan '06  
GeneralPrinting the view PinmemberAlberto_Canabal4:45 9 Jan '06  
GeneralRe: Printing the view PinmemberRoongrit Charoensupkul3:59 10 Jan '06  
GeneralRe: Printing the view PinmemberAlberto_Canabal6:15 10 Jan '06  
GeneralRe: Printing the view (ouch) PinmemberAlberto_Canabal0:32 19 Jan '06  
GeneralZooming Centering not perfect Pinmembervipinasda10:40 8 Jan '06  
GeneralRe: Zooming Centering not perfect Pinmembervipinasda10:52 8 Jan '06  
GeneralRe: Zooming Centering not perfect PinmemberRoongrit Charoensupkul3:56 10 Jan '06  
GeneralRe: Zooming Centering not perfect PinmemberVipin Aravind10:40 13 Jan '06  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 8 Oct 2004
Article Copyright 2004 by Roonglit Chareonsupkul
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid