第一步:右键菜单参数以及初始化
//右键菜单
// private ITOCControl2 pTocControl;
private IMapControl3 pMapControl;
// private IToolbarMenu pToolMenuMap;
private IToolbarMenu pToolMenuLayer;
private void rightMenu()
{
// 取得 MapControl 和 PageLayoutControl 的引用
/// pTocControl = (ITOCControl2)axTOCControl1.Object;
pMapControl = (IMapControl3)axMapControl1.Object;
// 创建菜单
///pToolMenuMap = new ToolbarMenuClass();
pToolMenuLayer = new ToolbarMenuClass();
pToolMenuLayer.AddItem(new zoomtolayer(), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);
pToolMenuLayer.AddItem(new Class1(), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);
pToolMenuLayer.AddItem(new 属性表(), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);
pToolMenuLayer.SetHook(pMapControl);
}
第二步:
添加axtoccontrol右击事件代码
private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
{
if (e.button != 2) return;
rightMenu();
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap map = null; ILayer layer = null;
object other = null; object index = null;
this.axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
if (item == esriTOCControlItem.esriTOCControlItemMap)
this.axTOCControl1.SelectItem(map, null);
else if (item == esriTOCControlItem.esriTOCControlItemLayer)
this.axTOCControl1.SelectItem(layer, null);
axMapControl1.CustomProperty = layer;
if (item == esriTOCControlItem.esriTOCControlItemMap)
{
MessageBox.Show("点击了Layers");
}
if (item == esriTOCControlItem.esriTOCControlItemLayer)
{
pToolMenuLayer.PopupMenu(e.x, e.y, this.axTOCControl1.hWnd);
}
}
第三步:
以属性表为例,展示basecommand类这个的代码
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;
namespace 创建与调用aoi书签
{
class 属性表 : BaseCommand
{
private IMapControl3 pMapControl;
public 属性表()
{
base.m_caption = "属性表";
}
public override void OnClick()
{
openattribute();
}
public override void OnCreate(object hook)
{
pMapControl = (IMapControl3)hook;
}
private void openattribute()
{
ILayer pLayer = (ILayer)pMapControl.CustomProperty;
databoard databoard = new databoard(pLayer);
databoard.Show();
}
}
}
第四步、属性表窗体
public partial class AttributeTable : Form
{
private IFeatureLayer pFeatureLayer = null;
public AttributeTable(IFeatureLayer _FeatureLayer)
{
InitializeComponent();
pFeatureLayer = _FeatureLayer;
}
private void AttributeTable_Load(object sender, EventArgs e)
{
IFields pFields;
pFields = pFeatureLayer.FeatureClass.Fields;//连接属性表的列
dataGridView1.ColumnCount = pFields.FieldCount;
for (int i = 0; i < pFields.FieldCount; i++)
{
string fldName = pFields.get_Field(i).Name;//字段名
dataGridView1.Columns[i].Name = fldName;
dataGridView1.Columns[i].ValueType = Type.GetType(ParseFieldType(pFields.get_Field(i).Type));
//列名和值类型
}
IFeatureCursor pFeatureCursor;//定义游标/指针
pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false);
IFeature pFeature;
pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
string[] fldValue = new string[pFields.FieldCount];
for (int i = 0; i < pFields.FieldCount; i++)
{
string fldName;
fldName = pFields.get_Field(i).Name;
//下代码判断是数值类型还是几何类型字段
if (fldName == pFeatureLayer.FeatureClass.ShapeFieldName)
{
fldValue[i] = pFeature.Shape.GeometryType.ToString().Remove(0,12);
}
else
fldValue[i] = pFeature.get_Value(i).ToString();
}
dataGridView1.Rows.Add(fldValue);//写入行值
pFeature = pFeatureCursor.NextFeature();
}
}
private string ParseFieldType(esriFieldType TableFieldType)
{
switch (TableFieldType)
{
case esriFieldType.esriFieldTypeBlob:
return "System.String";
case esriFieldType.esriFieldTypeDate:
return "System.DateTime";
case esriFieldType.esriFieldTypeDouble:
return "System.Double";
case esriFieldType.esriFieldTypeGeometry:
return "System.String";
case esriFieldType.esriFieldTypeGlobalID:
return "System.String";
case esriFieldType.esriFieldTypeGUID:
return "System.String";
case esriFieldType.esriFieldTypeInteger:
return "System.Int32";
case esriFieldType.esriFieldTypeOID:
return "System.String";
case esriFieldType.esriFieldTypeRaster:
return "System.String";
case esriFieldType.esriFieldTypeSingle:
return "System.Single";
case esriFieldType.esriFieldTypeSmallInteger:
return "System.Int32";
case esriFieldType.esriFieldTypeString:
return "System.String";
default:
return "System.String";
}
}
}