Scrolling and Zooming in a CDialog-Derived Class

Recently, I needed to implement scrolling and zooming in a property page. The property page was too specific, so I thought why not implement them in a CDialog-derived class and present it here in the form of an article?

Now, where do I look for code? As a matter of fact, the CScrollView class, which implements scrolling, already exists. So, I literally copied code from CScrollView into my CDialog-derived class. However, I had to make some minor adjustments to make it work with the CDialog-derived class.

For scrolling, CScrollView works in conjunction with a private MFC class called _AFX_MOUSEANCHORWND that is derived from CWnd. You can find the class declaration and implementation of _AFX_MOUSEANCHORWND in "viewscrl.cpp" that can be found inside MFC's src folder. Now, _AFX_MOUSEANCHORWND works specifically with CScrollView class; this is evident from its Create function declaration.

BOOL Create(CScrollView* pParent);

In the function above, you can see the the pParent's type is CScrollView. Initially, I thought of deriving my own mouse anchor wnd class from _AFX_MOUSEANCHORWND, but because the class is declared in an implementation file (viewscrl.cpp) and if I included a .cpp file, it might create more hassles than necessary, I chose instead to create an entirely new class, DlgMouseAnchorWnd (derived from CWnd), that works with CDialog.

Hence, my Create function now looks like this:

BOOL Create(CDialog* pParent)

Throughout the whole code, the afxData variable is used. This is a global data that my MFC uses internally. So, to use this variable, you have to include a file called "afximpl.h" appropriately in your "stdafx.h".

Note: This file is not present in the usual MFC's include folder. So, you might need to provide the path to the "afximpl.h" file in Additional Include Directories in project settings.

For the scrolling to work, you just have to call the SetScrollSizes function from the OnInitDialog method the same way that you call SetScrollSizes from the OnInitialUpdate function for the CScrollView class.

Now for zooming, there is the OnZoom public function that is declared as shown below:

void OnZoom(int x, int y, double ZoomFactor);

If you supply ZoomFactor 1.0, it neither zooms in nor zooms out.

For zooming in, use a ZoomFactor greater than 1.0; for zooming out, use a ZoomFactor of less than 1.0. You can, in fact, zoom out so much that the scroll bars turn invisible and by zooming in again, you can see the scroll bars.

I have handled the left mouse button click and right mouse button click to do the zooming. When you click the left mouse button, it does zoom in, and upon right-clicking, it does zoom out.

The following are the two functions:

void CScroomDialogDlg::OnLButtonDown(UINT nFlags, CPoint point)
   // TODO: Add your message handler code here and/or call default
   OnZoom(point.x, point.y, 1.4);
   CDialog::OnLButtonDown(nFlags, point);
}

void CScroomDialogDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
   // TODO: Add your message handler code here and/or call default
   OnZoom(point.x, point.y, 0.714285);
   CDialog::OnRButtonDown(nFlags, point);
}

I hope you enjoyed the article. The demo application and the source are provided here. Thank you!!!



About the Author

Mitesh Pandey

I love object oriented languages. I have know-how in the following fields: Core C++, Visual C++, STL, MFC, COM, ATL, DirectX, Managed C++, C#, PHP, MySQL, OpenGL.

Downloads

Comments

  • nice..

    Posted by kirants on 01/29/2008 02:42pm

    I found this hack interesting. It would have been useful if you could have explained a little about the scrollview internals , the class architecture and what roles they all play in achieving the functionality. Perhaps, you can update with this info when you have time. Keep up the good work !

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • A global data storage provider whose business is booming needed a best-in-class data center to serve as the backbone of its technical operations going forward—and it needed it delivered within a year.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds