Click here to Skip to main content
15,892,674 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.9K   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

 
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 
GeneralRe: Printing the view (ouch) Pin
Alberto_Canabal19-Jan-06 0:32
Alberto_Canabal19-Jan-06 0:32 
GeneralZooming Centering not perfect Pin
vipinasda8-Jan-06 10:40
vipinasda8-Jan-06 10:40 
Hi,
I tried your code. It's good, appreciate it. But there is a
bug after certain stage of zooming.

It centers the rubberband area properly when I do once.
From second time onwards, centering is not proper and there is a bug. The rubber band area isn't centered on the client area.
Fixing that bug would make this article more useful.

Thanks
Vipin - MVP


GeneralRe: Zooming Centering not perfect Pin
vipinasda8-Jan-06 10:52
vipinasda8-Jan-06 10:52 
GeneralRe: Zooming Centering not perfect Pin
Roonglit Chareonsupkul10-Jan-06 3:56
Roonglit Chareonsupkul10-Jan-06 3:56 
GeneralRe: Zooming Centering not perfect Pin
Vipin Aravind13-Jan-06 10:40
Vipin Aravind13-Jan-06 10:40 
GeneralRe: Zooming Centering not perfect Pin
Roonglit Chareonsupkul15-Jan-06 2:12
Roonglit Chareonsupkul15-Jan-06 2:12 
GeneralScrolling Pin
Alberto_Canabal5-Dec-05 2:51
Alberto_Canabal5-Dec-05 2:51 
GeneralRe: Scrolling Pin
Roonglit Chareonsupkul5-Dec-05 22:29
Roonglit Chareonsupkul5-Dec-05 22:29 
GeneralAdding child control Pin
Shafeeque O.K.5-Oct-05 9:10
Shafeeque O.K.5-Oct-05 9:10 
GeneralSetViewportOrg Pin
Member 147945128-Jun-05 22:23
Member 147945128-Jun-05 22:23 
GeneralHiMetric or other mapping.. Pin
malotator29-Apr-05 7:39
malotator29-Apr-05 7:39 
GeneralRe: HiMetric or other mapping.. Pin
WarChildWTS14-Sep-05 10:59
WarChildWTS14-Sep-05 10:59 
Generalzooming Pin
sreejith ss nair28-Apr-05 20:28
sreejith ss nair28-Apr-05 20:28 
GeneralRe: zooming Pin
Roonglit Chareonsupkul2-May-05 5:58
Roonglit Chareonsupkul2-May-05 5:58 
Generalneed a help Pin
Naren Neelamegam1-Apr-05 3:10
Naren Neelamegam1-Apr-05 3:10 
GeneralRe: need a help Pin
Roonglit Chareonsupkul4-Apr-05 6:00
Roonglit Chareonsupkul4-Apr-05 6:00 
GeneralRe: need a help Pin
Naren Neelamegam5-Apr-05 21:35
Naren Neelamegam5-Apr-05 21:35 
GeneralRe: need a help Pin
Roonglit Chareonsupkul5-Apr-05 23:50
Roonglit Chareonsupkul5-Apr-05 23:50 
GeneralRe: need a help Pin
hairy_hats27-Jul-05 4:04
hairy_hats27-Jul-05 4:04 

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.