CZoomCtrl: A Picture Control with Zooming and Scrolling

CZoomCtrl: A Picture Control with Zooming and Scrolling

By  25 Mar 2008
 

Introduction

I'm working on a dialog to display a type of chart. If there are a lot of data points, the display will become dense, and the user will want to zoom in for a closer look. To support this, I need an owner-draw child control with a virtual space, viewport, and scrollbars -- like a CWnd equivalent of CScrollView. I figured I could easily find such a thing, or whip one up using the scroll functions of CWnd.

Days pass. I didn't find one. I now have a solution which is simple, but took some work, so I thought I'd put it out here in case it might save time for someone else.

Background

Handling scrollbars is messy. The CWnd functions will show them, but you have to deal with all the user actions yourself (see the large blob of code in viewscrl.cpp). I tried leveraging other scrollbar handlers, like making the control a subclass of CEdit, then tried CScrollView. I downloaded a copy of ScroomDialog (by miteshpandey on CodeGuru), which is that blob of CScrollView code lifted and adapted to work on a CDialog.

None of this worked very well as the basis for a CStatic picture control. But then, I found a useful classCScrollHelper (by nschan on CodeProject) which manages scrollbars without much baggage. Thanks nschan! My slightly-modified version of your class is included here.

Using the code

CZoomCtrl is a subclass of CWnd, easily dropped into a dialog as a CStatic. It has a CScrollHelper it manages internally. Your job is to provide a draw method and a zoom factor, and the control handles the rest.

The delivery mechanism is a little dialog app (VS2003) with a zoom control, and two buttons:

  • Zoom enlarges the picture by a factor of 1.2, larger with each click. Scrollbars appear on the first click.
  • Fit goes back to the default: full drawing is scaled to fit the window.

pic1.JPG

pic2.JPG

To use the control in your own dialog:

  1. Add these four files to your project: zoomctrl.cpp/hScrollHelper.cpp/h.
  2. Create a subclass of CZoomCtrl and override the Draw method:
    class CMyZoomCtrl : public CZoomCtrl
     { ...
      virtual void Draw(CDC *pDC)
      {
          // fill background
          CRect rClient;
          GetClientRect(rClient);
          pDC->FillRect(&rClient, &CBrush(RGB(255,255,255)));
     
          // define virtual coords
          CRect rVirt(0, 0, 5000, 5000);
          PrepDC(pDC, rVirt, rClient);
     
          // do your drawing
          pDC->MoveTo(0, 0);
          pDC->LineTo(5000, 5000);
      }
     };

    Your Draw method has to do three things: erase the background, set up the virtual coordinate system, and draw your goods. Virtual coordinates are defined by a rectangle with origin at zero. These can be whatever you like, but if you don't want your drawing to distort, the virtual rect should be the same shape as the client. You must call PrepDC before drawing in these co-ords.

  3. Add a picture control (CStatic) to your dialog. It doesn't need special properties -- the default Frame type is OK. Add a border if you like. Give it an ID, like IDC_ZOOM_CTRL.
  4. Add a member to your dialog h and cpp files:
    include "zoomctrl.h"
    class CMyDialog
    {    ...
        CZoomCtrl m_zoomCtrl;
    
    void CMyDialog::DoDataExchange(CDataExchange* pDX)
    {    ...
        DDX_Control(pDX, IDC_ZOOM_CTRL, m_zoomCtrl);
  5. At this point, you're ready to try it out. But soon, you will want to give the user a way to zoom and unzoom. Here's how this looks for the zoom button of the demo app:
    void CZoomCtrlDemoDlg::OnBnClickedZoom()
     {
      m_zoomFactor *= 1.2;
      m_zoomCtrl.SetZoomFactor(m_zoomFactor);
      m_zoomCtrl.AdjustScrollbars();
      m_zoomCtrl.Invalidate();
     }
    

    A member of the dialog keeps track of its zoom factor. Unzoom sets it back to 1.0. Yours could be fancier.


    转自:http://www.codeproject.com/Articles/24659/CZoomCtrl-A-Picture-Control-with-Zooming-and-Scrol

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值