一起学习ArcEngine(3)缩小

继承自定义基类ToolBase,代码如下,比以前清爽多了!

   1:      /// <summary>
   2:      /// 地图缩小工具
   3:      /// </summary>
   4:      public class ZoomOut : ToolBase
   5:      {
   6:          private INewEnvelopeFeedback m_feedBack;
   7:          private IPoint m_point;
   8:          private Boolean m_isMouseDown;
   9:   
  10:           #region 实例化
  11:          public ZoomOut()
  12:              :base("ZoomOut")  
  13:          {
  14:              if (m_cursor == null) m_cursor = getCursor(esriSystemMouseCursor.esriSystemMouseCursorZoomOut);
  15:          }
  16:        
  17:          public ZoomOut(AxMapControl mapCtl) 
  18:              : base(mapCtl, "ZoomOut") 
  19:          {
  20:              if (m_cursor == null) m_cursor = getCursor(esriSystemMouseCursor.esriSystemMouseCursorZoomOut);
  21:          }
  22:   
  23:          public ZoomOut(AxPageLayoutControl plCtl)
  24:              : base(plCtl, "ZoomOut")
  25:          {
  26:              if (m_cursor == null) m_cursor = getCursor(esriSystemMouseCursor.esriSystemMouseCursorZoomOut);
  27:          }
  28:          #endregion
  29:   
  30:          protected override void initContextMenu()
  31:          {
  32:              ContextMenu cmenu = new ContextMenu();
  33:              MenuItem m = new MenuItem();
  34:              m.Text = "结束";
  35:              m.Click += new EventHandler(m_Click);
  36:              cmenu.MenuItems.Add(m);
  37:              m_contextMenu = cmenu;
  38:   
  39:          }
  40:   
  41:          void m_Click(object sender, EventArgs e)
  42:          {
  43:              if (m_pControl is AxMapControl)
  44:              {
  45:                  AxMapControl am = m_pControl as AxMapControl;
  46:                  am.CurrentTool = null;
  47:              }
  48:          }
  49:   
  50:   
  51:          public override void OnMouseDown(int Button, int Shift, int X, int Y)
  52:          {
  53:              if (m_pActiveView == null) return;
  54:              if (Button == 2)
  55:              {
  56:                  ShowContextMenu(X, Y);
  57:                  return;
  58:              }
  59:   
  60:              m_point = m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
  61:              m_cursor = getCursor(ESRI.ArcGIS.SystemUI.esriSystemMouseCursor.esriSystemMouseCursorZoomOut);
  62:              m_isMouseDown = true;
  63:   
  64:          }
  65:   
  66:          public override void OnMouseMove(int Button, int Shift, int X, int Y)
  67:          {
  68:              if (!m_isMouseDown) return;
  69:   
  70:              //Start an envelope feedback
  71:              if (m_feedBack == null)
  72:              {
  73:                  m_feedBack = new NewEnvelopeFeedbackClass();
  74:                  m_feedBack.Display = m_pActiveView.ScreenDisplay;
  75:                  m_feedBack.Start(m_point);
  76:              }
  77:              //Move the envelope feedback
  78:              m_feedBack.MoveTo(m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
  79:          }
  80:   
  81:          public override void OnMouseUp(int Button, int Shift, int X, int Y)
  82:          {
  83:              if (!m_isMouseDown) return;
  84:   
  85:              IEnvelope pEnvelope;
  86:              IEnvelope pFeedEnvelope;
  87:   
  88:              double newWidth, newHeight;
  89:   
  90:              //If an envelope has not been tracked
  91:              if (m_feedBack == null)
  92:              {
  93:                  //Zoom out from the mouse click
  94:                  pEnvelope = m_pActiveView.Extent;
  95:                  pEnvelope.Expand(2, 2, true);
  96:                  pEnvelope.CenterAt(m_point);
  97:              }
  98:              else
  99:              {
 100:                  //Stop the envelope feedback
 101:                  pFeedEnvelope = m_feedBack.Stop();
 102:   
 103:                  //Exit if the envelope height or width is 0
 104:                  if (pFeedEnvelope.Width == 0 || pFeedEnvelope.Height == 0)
 105:                  {
 106:                      m_feedBack = null;
 107:                      m_isMouseDown = false;
 108:                  }
 109:   
 110:                  newWidth = m_pActiveView.Extent.Width * (m_pActiveView.Extent.Width / pFeedEnvelope.Width);
 111:                  newHeight = m_pActiveView.Extent.Height * (m_pActiveView.Extent.Height / pFeedEnvelope.Height);
 112:   
 113:                  //Set the new extent coordinates
 114:                  pEnvelope = new EnvelopeClass();
 115:                  pEnvelope.PutCoords(m_pActiveView.Extent.XMin - ((pFeedEnvelope.XMin - m_pActiveView.Extent.XMin) * (m_pActiveView.Extent.Width / pFeedEnvelope.Width)),
 116:                      m_pActiveView.Extent.YMin - ((pFeedEnvelope.YMin - m_pActiveView.Extent.YMin) * (m_pActiveView.Extent.Height / pFeedEnvelope.Height)),
 117:                      (m_pActiveView.Extent.XMin - ((pFeedEnvelope.XMin - m_pActiveView.Extent.XMin) * (m_pActiveView.Extent.Width / pFeedEnvelope.Width))) + newWidth,
 118:                      (m_pActiveView.Extent.YMin - ((pFeedEnvelope.YMin - m_pActiveView.Extent.YMin) * (m_pActiveView.Extent.Height / pFeedEnvelope.Height))) + newHeight);
 119:              }
 120:   
 121:              //Set the new extent
 122:              m_pActiveView.Extent = pEnvelope;
 123:   
 124:              //Refresh the active view
 125:              m_pActiveView.Refresh();
 126:              m_feedBack = null;
 127:              m_isMouseDown = false;
 128:          }
 129:   
 130:          public void OnKeyDown(int keyCode, int shift)
 131:          {
 132:              if (m_isMouseDown)
 133:              {
 134:                  if (keyCode == 27) //ESC key
 135:                  {
 136:                      m_feedBack = null;
 137:                      m_isMouseDown = false;
 138:                      m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null);
 139:                  }
 140:              }
 141:          }
 142:      }
posted @ 2009-09-07 21:22  liuyh208  阅读(1701)  评论(5编辑  收藏  举报