ArcToolBox工具栏上添加自定义工具或命令

工具类:继承于BaseTool,若为命令,则继承于BaseCommand。

using ESRI.ArcGIS.ADF.BaseClasses;//该引用不同于AO9.2 using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using System.Windows.Forms; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Geodatabase; using System; using WorldMap; public sealed class SearchData : BaseTool//若为命令,继承于BaseCommand { IMapControl3 mapControl; private IHookHelper m_HookHelper = new HookHelperClass(); public SearchData() { //设置工具按钮的属性 base.m_caption = "Search Data"; base.m_toolTip = "Search Data"; base.m_message = "Search Data"; base.m_name = "Search Data"; base.m_cursor = Cursors.Help; try { base.m_bitmap = new System.Drawing.Bitmap(Application.StartupPath + @"/icon/search3.bmp"); } catch { MessageBox.Show(this.Name+Environment.NewLine+"BitMap path is wrong!"); } } public override void OnCreate(object hook) { m_HookHelper.Hook = hook; if (hook is IToolbarControl) { IToolbarControl toolbarControl = (IToolbarControl) hook; mapControl = (IMapControl3) toolbarControl.Buddy; } } public override bool Enabled { get { if(m_HookHelper.FocusMap == null) return false; return true; } } //在MapControl上的事件 public override void OnMouseDown(int Button, int Shift, int X, int Y) { //Identify功能 try { IPoint pPoint; IFeatureLayer pFeatureLayer; ISpatialFilter pSpatialFilter; IFeatureSelection pFeatureSelection; ICursor pCursor; IFeature pFeature; ISelectionSet pSelectionSet; pPoint = this.mapControl.ToMapPoint(X, Y); pFeatureLayer = (IFeatureLayer)this.mapControl.get_Layer(3); pFeatureSelection = (IFeatureSelection)pFeatureLayer; pSpatialFilter = new SpatialFilterClass(); pSpatialFilter.Geometry = (IGeometry)pPoint; pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; this.mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null); pFeatureSelection.SelectFeatures(pSpatialFilter, esriSelectionResultEnum.esriSelectionResultNew, true); this.mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null); pSelectionSet = pFeatureSelection.SelectionSet; pSelectionSet.Search(null, false, out pCursor); pFeature = (IFeature)pCursor.NextRow(); if (pFeature != null) { IFields pFields = pFeatureLayer.FeatureClass.Fields; DataForm dataForm = DataForm.NewForm(); dataForm.textBox1.Text = pFeature.get_Value(pFields.FindField("中文名称")).ToString(); dataForm.Show(); this.mapControl.FlashShape(pFeature.Shape, 3, 200, null); } } catch (Exception e) { MessageBox.Show(e.Message); } } }

工具或命令添加到ArcToolBar上:

axToolbarControl1.AddItem(new SearchData(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

效果图:

ps:从自定义工具或命令中访问MapControl,可以先从hook得到ArcToolBar,进而通过buddy属性得到mapcontrol。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值