SelectByShape 工具的实现

  1  public sealed class SelectByShapeTool : BaseTool
  2     {
  3         #region COM Registration Function(s)
  4         [ComRegisterFunction()]
  5         [ComVisible(false)]
  6         static void RegisterFunction(Type registerType)
  7         {
  8             // Required for ArcGIS Component Category Registrar support
  9             ArcGISCategoryRegistration(registerType);
 10 
 11             //
 12             // TODO: Add any COM registration code here
 13             //
 14         }
 15 
 16         [ComUnregisterFunction()]
 17         [ComVisible(false)]
 18         static void UnregisterFunction(Type registerType)
 19         {
 20             // Required for ArcGIS Component Category Registrar support
 21             ArcGISCategoryUnregistration(registerType);
 22 
 23             //
 24             // TODO: Add any COM unregistration code here
 25             //
 26         }
 27 
 28         #region ArcGIS Component Category Registrar generated code
 29         /// <summary>
 30         /// Required method for ArcGIS Component Category registration -
 31         /// Do not modify the contents of this method with the code editor.
 32         /// </summary>
 33         private static void ArcGISCategoryRegistration(Type registerType)
 34         {
 35             string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
 36             ControlsCommands.Register(regKey);
 37 
 38         }
 39         /// <summary>
 40         /// Required method for ArcGIS Component Category unregistration -
 41         /// Do not modify the contents of this method with the code editor.
 42         /// </summary>
 43         private static void ArcGISCategoryUnregistration(Type registerType)
 44         {
 45             string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
 46             ControlsCommands.Unregister(regKey);
 47 
 48         }
 49 
 50         #endregion
 51         #endregion
 52 
 53         private IHookHelper m_hookHelper;
 54         private INewEnvelopeFeedback m_Feedback;
 55         private IPoint m_point;
 56         private bool isMouseDown = false;
 57         IMap pMap;
 58 
 59         public SelectByShapeTool()
 60         {
 61             //
 62             // TODO: Define values for the public properties
 63             //
 64             base.m_category = "CustomTool"; //localizable text 
 65             base.m_caption = "拉框选择";  //localizable text 
 66             base.m_message = "拉框选择";  //localizable text
 67             base.m_toolTip = "拉框选择";  //localizable text
 68             base.m_name = "SelectByShape";   //unique id, non-localizable (e.g. "MyCategory_MyTool")
 69             try
 70             {
 71                 //
 72                 // TODO: change resource name if necessary
 73                 //
 74                 string bitmapResourceName = GetType().Name + ".bmp";
 75                 base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
 76                 base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
 77             }
 78             catch (Exception ex)
 79             {
 80                 System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
 81             }
 82         }
 83 
 84         #region Overridden Class Methods
 85 
 86         /// <summary>
 87         /// Occurs when this tool is created
 88         /// </summary>
 89         /// <param name="hook">Instance of the application</param>
 90         public override void OnCreate(object hook)
 91         {
 92             if (m_hookHelper == null)
 93                 m_hookHelper = new HookHelperClass();
 94 
 95             m_hookHelper.Hook = hook;
 96             // TODO:  Add SelectByShapeTool.OnCreate implementation
 97         }
 98 
 99         /// <summary>
100         /// Occurs when this tool is clicked
101         /// </summary>
102         public override void OnClick()
103         {
104             // TODO: Add SelectByShapeTool.OnClick implementation
105         }
106 
107         public override void OnMouseDown(int Button, int Shift, int X, int Y)
108         {
109             if (m_hookHelper.ActiveView == null) return;
110             //如果是视图
111             if (m_hookHelper.ActiveView is IActiveView)
112             {
113                 IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.
114                     DisplayTransformation.ToMapPoint(X, Y) as IPoint;
115                 pMap = m_hookHelper.ActiveView.HitTestMap(pPoint);
116                 if (pMap != m_hookHelper.FocusMap)
117                 {
118                     m_hookHelper.ActiveView.FocusMap = pMap;
119                     m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.
120                         esriViewGraphics, null, null);
121                 }
122             }
123             //在焦点地图上创建一个点
124             IActiveView pac = m_hookHelper.FocusMap as IActiveView;
125             m_point = pac.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y) as IPoint;
126             isMouseDown = true;
127         }
128 
129         public override void OnMouseMove(int Button, int Shift, int X, int Y)
130         {
131             if (!isMouseDown) return;
132             //得到焦点图
133             IActiveView pac = m_hookHelper.FocusMap as IActiveView;
134             //初始化一个范围
135             if (m_Feedback == null)
136             {
137                 m_Feedback = new NewEnvelopeFeedbackClass();
138                 m_Feedback.Display = pac.ScreenDisplay;
139                 m_Feedback.Start(m_point);
140             }
141             //设置鼠标样式
142 
143             //画范围
144             m_Feedback.MoveTo(pac.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
145         }
146 
147         public override void OnMouseUp(int Button, int Shift, int X, int Y)
148         {
149             //设置鼠标样式
150 
151             if (!isMouseDown) return;
152             //IEnvelope pEnv;
153             IEnvelope pFeedEnv;
154             IGeometry pGeo;  //查询用到图形,可能为点或者矩形
155             //得到焦点地图
156             IActiveView Pac = m_hookHelper.FocusMap as IActiveView;
157             //如果不画范围
158             if (m_Feedback == null)
159             {
160                 return;
161             }
162             else
163             {
164                 //停止画框
165                 pFeedEnv = m_Feedback.Stop();
166                 pGeo = pFeedEnv as IGeometry;
167             }
168 
169             ISelectionEnvironment pSelectEnv = new SelectionEnvironmentClass();
170             //改变默认选择集的颜色
171             pSelectEnv.DefaultColor = MySymbology.GetRgbColor(110, 120, 210, 200);
172             //选择要素,把它放入选择集中
173             pMap = m_hookHelper.FocusMap;
174             pMap.SelectByShape(pGeo, pSelectEnv, false);
175             Pac.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
176 
177             isMouseDown = false;
178             m_Feedback = null;
179             m_point = null;
180         }
181         #endregion
182     }

参考资料:《ArcGIS  Engine组件式开发及应用》 科学出版社

posted @ 2012-06-27 15:59  DayDreamInGIS  阅读(478)  评论(0编辑  收藏  举报