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组件式开发及应用》 科学出版社

转载于:https://www.cnblogs.com/DayDreamEveryWhere/archive/2012/06/27/2565954.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 ArcEngine 中编写 ITool 工具获取鼠标框选内容,可以按照以下步骤进行: 1. 实现 ITool 接口,重写 OnMouseDown、OnMouseMove、OnMouseUp 等方法,以响应鼠标事件。 2. 在 OnMouseDown 方法中记录鼠标按下时的位置,并在 OnMouseUp 方法中记录鼠标抬起时的位置,计算出鼠标框选的矩形范围。 3. 在 OnMouseMove 方法中判断鼠标是否处于框选状态,如果是,则根据框选范围执行相应的操作,比如选择要素、高亮显示等。 4. 在 ITool 工具中可以通过 IMap 接口和 ISelection 接口来获取当前地图和选择集对象,从而实现对地图和图层上要素的操作。 以下是一个简单的示例代码: ```c# public class SelectionTool : ITool { private IMap m_map; private bool m_isSelecting; private IEnvelope m_selectionEnvelope; private ISelection m_selection; public void OnMouseDown(int button, int shift, int x, int y) { if (button == 1) { m_isSelecting = true; m_selectionEnvelope = new EnvelopeClass(); m_selectionEnvelope.XMin = x; m_selectionEnvelope.YMin = y; m_selectionEnvelope.XMax = x; m_selectionEnvelope.YMax = y; } } public void OnMouseMove(int button, int shift, int x, int y) { if (m_isSelecting) { m_selectionEnvelope.XMax = x; m_selectionEnvelope.YMax = y; IGeometry selectionGeometry = m_selectionEnvelope as IGeometry; m_map.SelectByShape(selectionGeometry, null, false); m_selection = m_map.FeatureSelection; m_map.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null); } } public void OnMouseUp(int button, int shift, int x, int y) { if (m_isSelecting) { m_isSelecting = false; m_selectionEnvelope.XMax = x; m_selectionEnvelope.YMax = y; IGeometry selectionGeometry = m_selectionEnvelope as IGeometry; m_map.SelectByShape(selectionGeometry, null, false); m_selection = m_map.FeatureSelection; m_map.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null); } } public void OnCreate(IMap map) { m_map = map; } public bool Enabled { get { return true; } } public int Cursor { get { return (int)esriSystemMouseCursor.esriSystemMouseCursorCrosshair; } } public void OnKeyDown(int keyCode, int shift) { } public void OnKeyUp(int keyCode, int shift) { } public string Name { get { return "SelectionTool"; } } public void OnDoubleClick() { } public void OnContextMenu(int x, int y) { } public void Refresh(int hDC) { } public void OnDeactivate() { } public int HelpContextID { get { return 0; } } public string HelpFile { get { return ""; } } public string Message { get { return "Use the mouse to select features."; } } public void OnActivate() { } public void OnFocus(int x, int y) { } public void OnBlur() { } } ``` 在上述代码中,我们实现了一个简单的 ITool 工具,用于在地图上进行鼠标框选,并将框选的要素进行选择。在 OnMouseMove 方法中,我们通过 IMap 接口的 SelectByShape 方法来进行要素选择,并通过 PartialRefresh 方法来更新地图的显示。在实际开发中,我们可以根据具体需求对代码进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值