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

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

}
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上:

 

 

效果图:

 

 

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

 

 

转载自:https://blog.csdn.net/congxn/article/details/5676274

You may also like...

退出移动版