Click here to Skip to main content
13,056,352 members (108,994 online)
Click here to Skip to main content
Add your own
alternative version


68 bookmarked
Posted 8 Jun 2004


Rate this:
Please Sign up or sign in to vote.
A class that provides "zoom" feature


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);
    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;
        m_rubberBand.SetRect(m_ptStart, m_ptStart);
    CZoomView::OnLButtonDown(nFlags, point);


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


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
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.

You may also be interested in...

Comments and Discussions

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

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