地图操作的工具,大部分ArcEngine都有现成的,但是这些工具不太好用,比如提示和名称都是英文,地图操作中没有右键,无法和vs提供的工具条控件或第三方工具条控件集成等,还是自己实现比较灵活方便。
我们就从最简单的放大工具开始来实现地图操作常用的工具吧!
废话少说,先上代码
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:
10: namespace TechFront.TPIS2.GisCommFun
11: {
12: public class ZoomIn : ICommand, ITool
13: {
14:
15: [DllImport("gdi32.dll")]
16: static extern bool DeleteObject(IntPtr hObject);
17:
18: private System.Drawing.Bitmap m_bitmap;
19: private IntPtr m_hBitmap;
20: private IHookHelper m_pHookHelper;
21: private INewEnvelopeFeedback m_feedBack;
22: private IPoint m_point;
23: private Boolean m_isMouseDown;
24: private System.Windows.Forms.Cursor m_zoomInCur;
25: private System.Windows.Forms.Cursor m_moveZoomInCur;
26:
27: public ZoomIn()
28: {
29: //Load resources
30: string[] res = GetType().Assembly.GetManifestResourceNames();
31: if (res.GetLength(0) > 0)
32: {
33: m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(GetType(), "Bmp.ZoomIn.bmp"));
34: if (m_bitmap != null)
35: {
36: m_bitmap.MakeTransparent(m_bitmap.GetPixel(1, 1));
37: m_hBitmap = m_bitmap.GetHbitmap();
38: }
39: }
40: m_pHookHelper = new HookHelperClass();
41: }
42:
43: ~ZoomIn()
44: {
45: if (m_hBitmap.ToInt32() != 0)
46: DeleteObject(m_hBitmap);
47:
48: m_pHookHelper = null;
49: m_zoomInCur = null;
50: m_moveZoomInCur = null;
51: }
52:
53: #region ICommand Members
54:
55: public void OnClick()
56: {
57: }
58:
59: public string Message
60: {
61: get
62: {
63: return "放大地图";
64: }
65: }
66:
67: public int Bitmap
68: {
69: get
70: {
71: return m_hBitmap.ToInt32();
72: }
73: }
74:
75: public void OnCreate(object hook)
76: {
77: m_pHookHelper.Hook = hook;
78: m_zoomInCur = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream(GetType(), "Cur.ZoomIn.cur"));
79: m_moveZoomInCur = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream(GetType(), "Cur.MoveZoomIn.cur"));
80: }
81:
82: public string Caption
83: {
84: get
85: {
86: return "放大";
87: }
88: }
89:
90: public string Tooltip
91: {
92: get
93: {
94: return "放大";
95: }
96: }
97:
98: public int HelpContextID
99: {
100: get
101: {
102: // TODO: Add ZoomIn.HelpContextID getter implementation
103: return 0;
104: }
105: }
106:
107: public string Name
108: {
109: get
110: {
111: return "ZoomIn";
112: }
113: }
114:
115: public bool Checked
116: {
117: get
118: {
119: return false;
120: }
121: }
122:
123: public bool Enabled
124: {
125: get
126: {
127: if (m_pHookHelper.FocusMap == null) return false;
128:
129: return true;
130: }
131: }
132:
133: public string HelpFile
134: {
135: get
136: {
137: // TODO: Add ZoomIn.HelpFile getter implementation
138: return null;
139: }
140: }
141:
142: public string Category
143: {
144: get
145: {
146: return "GisCommFun";
147: }
148: }
149:
150: #endregion
151:
152: #region ITool Members
153:
154: public void OnMouseDown(int button, int shift, int x, int y)
155: {
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 the point is within a data frame
165: IMap pMap = m_pHookHelper.ActiveView.HitTestMap(pPoint);
166:
167: if (pMap == null) return;
168:
169: //Set the map to be 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:
182: }
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
192: 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
200: m_feedBack.MoveTo(pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y));
201: }
202:
203: public void OnMouseUp(int button, int shift, int x, int y)
204: {
205: if (!m_isMouseDown) return;
206:
207: //Get the focus map
208: IActiveView pActiveView = (IActiveView)m_pHookHelper.FocusMap;
209:
210: //If an 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: }
220: else
221: {
222: //Stop the envelope feedback
223: pEnvelope = m_feedBack.Stop();
224:
225: //Exit if the envelope height or width is 0
226: if (pEnvelope.Width == 0 || pEnvelope.Height == 0)
227: {
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:
240: 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: }
250: }
251: }
252:
253: public void OnKeyUp(int keyCode, int shift)
254: {
255: // TODO: Add ZoomIn.OnKeyUp implementation
256: }
257:
258: public int Cursor
259: {
260: get
261: {
262: if (m_isMouseDown)
263: return m_moveZoomInCur.Handle.ToInt32();
264: else
265: return m_zoomInCur.Handle.ToInt32();
266: }
267: }
268:
269: 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
288: }
289:
290: #endregion
291: }
292: }
其实,放大工具 主要是实现Itool的OnMouseDown、OnMouseMove、OnMouseUp三个方法,设置ICommand的Caption,Tooltip、Message、Bitmap属性,实现OnCreate方法。
Itool的三个方法由名称就可以知道,三个方法分别是在鼠标按下左键时、按下左键移动时、鼠标弹起时执行。
Caption属性是设置工具显示的文本;Tooltip是设置鼠标移到工具上时,提示的信息;Bitmap是设置工具的图标信息