Learn Arcengine (1)-zoom tool

Source: Internet
Author: User
Tags ipoint
 

Map operation Tools, most of the arcengine have ready-made, but these tools are not easy to use, such as the hint and name are in English, map operation without right button, can not and vs provided the tool bar control or Third-party toolbar control integration, or to achieve more flexible and convenient.

Let's start with the simplest zoom tool to implement the common tools for map manipulation.

Less nonsense, first on the code

   1:  using System;
   2:  using ESRI. Arcgis.carto;
   3:  using ESRI. Arcgis.geometry;
   4:  using ESRI. Arcgis.controls;
   5:  using ESRI. Arcgis.display;
   6:  using ESRI. Arcgis.systemui;
   7:  using ESRI. ArcGIS.ADF.CATIDs;
   8:  using System.Runtime.InteropServices;
   9:  
  Ten:  namespace TechFront.TPIS2.GisCommFun
  One:  {
  : Public      class Zoomin:icommand, Itool
  :      {
  14:         
  :          [DllImport ("Gdi32.dll")]
  :          static extern bool DeleteObject (IntPtr hobject);
  :  
  :          private System.Drawing.Bitmap M_bitmap;
  :          private IntPtr M_hbitmap;
  :          private Ihookhelper m_phookhelper;
  :          private Inewenvelopefeedback m_feedback;
  :          private IPoint m_point;
  :          private Boolean M_ismousedown;
  :          private System.Windows.Forms.Cursor m_zoomincur;
  :          private System.Windows.Forms.Cursor m_movezoomincur;
  Num:  
  : Public          ZoomIn ()
  :          {
  :              //load Resources
                string[] res = GetType (). Assembly.getmanifestresourcenames ();
  :              if (res. GetLength (0) > 0)
  :              {
                    M_bitmap = new System.Drawing.Bitmap (GetType (). Assembly.GetManifestResourceStream (GetType (), "Bmp.ZoomIn.bmp"));
  :                  if (m_bitmap!= null)
  :                  {
  A:                      m_bitmap. Maketransparent (M_bitmap. GetPixel (1, 1));
  Notoginseng:                      m_hbitmap = M_bitmap. Gethbitmap ();
  :                  }
  :              }
  :              m_phookhelper = new Hookhelperclass ();
  :          }
  :  
            ~zoomin ()
  :          {
  :              if (m_hbitmap.toint32 ()!= 0)
  :                  DeleteObject (M_HBITMAP);
  :  
                m_phookhelper = null;
  :              m_zoomincur = null;
  :              m_movezoomincur = null;
  Wuyi:          }
  I:  
            #region ICommand Members
  Si:  
  : Public          void OnClick ()
  A:          {
  :          }
  :  
  : Public          String Message
  %:          {
  %: Get              
  :              {
  A: Return                  "enlarge map";
  :              }
  :          }
  A:  
  : Public          int Bitmap
  :          {
  In: Get              
  :              {
  A: Return                  m_hbitmap.toint32 ();
  :              }
  A:          }
  The following:  
  "Public          void OnCreate" (Object hook)
  A:          {
  :              m_phookhelper.hook = Hook;
                m_zoomincur = new System.Windows.Forms.Cursor (GetType (). Assembly.GetManifestResourceStream (GetType (), "Cur.ZoomIn.cur"));
  :              m_movezoomincur = new System.Windows.Forms.Cursor (GetType (). Assembly.GetManifestResourceStream (GetType (), "Cur.MoveZoomIn.cur"));
  :          }
  Bayi:  
  Caption: Public          string
  %:          {
  : Get              
  :              {
  : Return                  "enlarge";
  :              }
  :          }
  :  
  : Public          String Tooltip
  A:          {
  : Get              
  M:              {
  : Return                  "enlarge";
  :              }
  :          }
  :  
  : Public          int HelpContextID
  :          {
M: Get              
A:              {
102:                  //TODO:  Add Zoomin.helpcontextid Getter Implementation
A: return                  0;
:              }
I:          }
The following:  
: Public          String Name
108:          {
109: Get              
A:              {
A: Return                  "ZoomIn";
112:              }
113:          }
114:  
: Public          bool Checked
116:          {
117: Get              
118:              {
119: Return                  false;
:              }
121:          }
122:  
123: Public          bool Enabled
124:          {
I: Get              
126:              {
127:                  if (m_phookhelper.focusmap = null) return false;
128:  
129: Return                  true;
130:              }
131:          }
132:  
HelpFile: Public          string
134:          {
135: Get              
136:              {
137:                  //TODO:  Add Zoomin.helpfile Getter Implementation
138: return                  null;
139:              }
140:          }
A:  
: Public          String Category
143:          {
144: Get              
145:              {
146: Return                  "Giscommfun";
147:              }
148:          }
149:  
A:          #endregion
151:  
152:          #region Itool Members
153:  
154: Public          void OnMouseDown (int button, int shift, int x, int y)
:          {
156:              if (M_phookhelper.activeview = null) return;
157:  
158:              //if The active view is a page layout
159:              if (M_phookhelper.activeview is ipagelayout)
160:              {
161:                  //create a point in map coordinates
162:                  IPoint ppoint = (IPoint) m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint (x, y);
163:  
164:                  //get The map if is within a data frame
165:                  IMap pMap = M_pHookHelper.ActiveView.HitTestMap (ppoint);
166:  
167:                  if (pMap = null) return;
The following:  
169://set the map to being the                  page layout ' s focus map
170:                  if (pMap!= m_phookhelper.focusmap)
171:                  {
172:                      m_pHookHelper.ActiveView.FocusMap = PMap;
173:                      M_pHookHelper.ActiveView.PartialRefresh (esriviewdrawphase.esriviewgraphics, NULL, NULL);
174:                  }
175:              }
176:              //create a point in map coordinates
177:              Iactiveview pactiveview = (iactiveview) m_phookhelper.focusmap;
178:              m_point = PActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint (x, y);
179:  
180:              M_ismousedown = true;
181:  
I:          }
183:  
184: Public          void OnMouseMove (int button, int shift, int x, int y)
185:          {
186:              if (!m_ismousedown) return;
187:  
188:              //get the Focus map
189:              Iactiveview Pactiveview = (iactiveview) m_phookhelper.focusmap;
190:  
191:              //start an envelope feedback
:              if (m_feedback = null)
193:              {
194:                  m_feedback = new Newenvelopefeedbackclass ();
195:                  m_feedback.display = Pactiveview.screendisplay;
196:                  M_feedback.start (M_point);
197:              }
198:  
199:              //move The envelope feedback
:              M_feedback.moveto (PActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint (x, y));
201:          }
The following:  
203: Public          void OnMouseUp (int button, int shift, int x, int y)
204:          {
205:              if (!m_ismousedown) return;
206:  
207:              //get the Focus map
Pactiveview:              Iactiveview = (iactiveview) m_phookhelper.focusmap;
209:  
210:              //if a envelope has not been tracked
211:              Ienvelope penvelope;
212:  
213:              if (m_feedback = null)
214:              {
215:                  //zoom in from mouse click
216:                  penvelope = pactiveview.extent;
217:                  Penvelope.expand (0.5, 0.5, true);
218:                  Penvelope.centerat (M_point);
219:              }
:              Else
221:              {
222:                  //stop The envelope feedback
223:                  Penvelope = M_feedback.stop ();
224:  
:                  //exit If the envelope height or width is 0
:                  if (penvelope.width = 0 | | penvelope.height = 0)
I:                  {
228:                      m_feedback = null;
229:                      M_ismousedown = false;
230:                  }
231:              }
232:              //set The new extent
233:              pactiveview.extent = Penvelope;
234:              //refresh the active view
235:              Pactiveview.refresh ();
236:              m_feedback = null;
237:              M_ismousedown = false;
238:          }
239:  
: Public          void OnKeyDown (int keycode, int shift)
241:          {
242:              if (m_ismousedown)
243:              {
244:                  if (keycode = 27)
245:                  {
246:                      M_ismousedown = false;
247:                      m_feedback = null;
248:                      M_pHookHelper.ActiveView.PartialRefresh (esriviewdrawphase.esriviewforeground, NULL, NULL);
249:                  }
:              }
251:          }
252:  
253: Public          void OnKeyUp (int keycode, int shift)
254:          {
255:              //TODO:  Add zoomin.onkeyup Implementation
256:          }
257:  
258: public          int Cursor
259:          {
I: Get              
261:              {
262:                  if (m_ismousedown)
263: Return                      m_moveZoomInCur.Handle.ToInt32 ();
264:                  Else
265: Return                      m_zoomInCur.Handle.ToInt32 ();
266:              }
267:          }
268:  
MB: public          bool OnContextMenu (int x, int y)
270:          {
271:              //TODO:  Add Zoomin.oncontextmenu Implementation
272: Return              false;
273:          }
274:  
275: Public          bool Deactivate ()
276:          {
277: Return              true;
278:          }
279:  
280: Public          void Refresh (int hdc)
281:          {
282:              //TODO:  Add Zoomin.refresh Implementation
283:          }
284:  
285: Public          void OnDblClick ()
286:          {
287:              //TODO:  Add Zoomin.ondblclick Implementation
A:          }
289:  
290:          #endregion
291:      }
292:  }

In fact, the amplification tool is mainly to achieve Itool onmousedown, OnMouseMove, onmouseup three methods, set ICommand caption,tooltip, message, bitmap attributes, Implement the OnCreate method.

The three methods of Itool can be known by name, and three methods are performed when the left mouse button is pressed, while the mouse button is moved.

The Caption property is the text that is displayed by the Settings tool; ToolTip is the information that prompts when you set the mouse over the tool; Bitmap is the icon information for the Set tool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.