利用ArcEngine进行GIS软件开发时,TOCControl控件本身没有提供右键菜单功能,需要开发人员编写代码实现,具体实现有如下两种方式:
1. 利用C#自带的contextMenuStrip来实现;
①在contextMenuStrip的OnClick事件中编写相应显示代码;
②在axTOCControl的OnMouseDown事件中将对应事件与axTOCControl挂钩;
contextMenuStrip.Show(axTOCControl1, new Point(e.x, e.y));
2. 利用AE自定义菜单功能实现;
①自定义OpenAttributeTable类,基于BaseCommand
在新创建的OpenAttributeTable中重写OnClick事件
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System.Data;
namespace 校园信息管理系统
{
/// <summary>
/// Summary description for OpenAttributeTable.
/// </summary>
[Guid("a87c4340-dc1e-4210-a557-e8cbadedb6fb")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("校园信息管理系统.OpenAttributeTable")]
public sealed class OpenAttributeTable : BaseCommand
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper;
private ILayer m_layer;
public OpenAttributeTable(ILayer pLayer)//构造函数,用来传参
{
//
// TODO: Define values for the public properties
//
base.m_category = "打开属性表"; //localizable text
base.m_caption = "打开属性表"; //localizable text
base.m_message = "打开属性表"; //localizable text
base.m_toolTip = "打开属性表"; //localizable text
base.m_name = "打开属性表"; //unique id, non-localizable (e.g. "MyCategory_MyCommand")
m_layer = pLayer;
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overridden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;
if (m_hookHelper == null)
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
// TODO: Add other initialization code
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()//重载Click函数,实现自定义功能
{
// TODO: Add OpenAttributeTable.OnClick implementation
IFeatureLayer pFeatureLayer = m_layer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
DataTable dt = new DataTable();
if (pFeatureClass != null)
{
DataColumn dc;
for (int i = 0; i < pFeatureClass.Fields.FieldCount; i++)
{
dc = new DataColumn(pFeatureClass.Fields.get_Field(i).Name);
dt.Columns.Add(dc);
}
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
IFeature pFeature = pFeatureCursor.NextFeature();
DataRow dr;
while (pFeature != null)
{
dr = dt.NewRow();
for (int j = 0; j < pFeatureClass.Fields.FieldCount; j++)
{
if (pFeatureClass.FindField(pFeatureClass.ShapeFieldName) == j)
{
dr[j] = GeoMapLoad.getShapeType(pFeatureClass.ShapeType);
}
else
{
dr[j] = pFeature.get_Value(j).ToString();
}
}
dt.Rows.Add(dr);
pFeature = pFeatureCursor.NextFeature();
}
AttributeTable attributeTable = new AttributeTable();
attributeTable.Show();
attributeTable.AttributeTableDataGridView.DataSource = dt;
}
}
#endregion
}
}
②在MainFrm中将新创建的菜单与axMapControl挂钩
创建全局变量
private ITOCControl2 m_TOCControl;
private IToolbarMenu m_menuLayer;
在窗体加载函数中初始化
m_TOCControl = (ITOCControl2)axTOCControl1.Object;
m_menuLayer = new ToolbarMenuClass();
m_menuLayer.SetHook(axMapControl1);
在TOCControl控件的OnMouseDown事件中弹出菜单
if (pItem == esriTOCControlItem.esriTOCControlItemLayer)
{
m_menuLayer.AddItem(new OpenAttributeTable(pLayer), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);
m_menuLayer.PopupMenu(e.x, e.y, m_TOCControl.hWnd);
//如果不传参,这句话可以放在窗体加载事件中,放在这要注意移除菜单项,否则每点击一次添加相同的菜单项
m_menuLayer.RemoveAll();
}