首先新建 Base Command,如下图:
在TOCControl 右键添加该菜单项,注意LayerPropertiesCmd() 前面的文件位置,为当前该类所在的项目位置,默认为LayerPropertiesCmd() 。
IToolbarMenu.AddItem()函数中的参数
第一个参数:菜单项的内容,功能实现。
第二个参数:对于一个工具定义多个 type 的时候,才会用到,每一个 int 代表一个新的实现。
第三个参数:索引值,在菜单项上面显示的位置。默认为 -1,按书写顺序排序。
第四个参数:是否开始一个新组,就是在其上面有一个“——”的效果。
第五个参数:显示样式。
//添加图层属性菜单项
m_menuLayer.AddItem(new AE_test1.class_AE.LayerPropertiesCmd(), -1, 2, true, esriCommandStyles.esriCommandStyleTextOnly);
原文中没有引用using ESRI.ArcGIS.ArcMapUI;因此标记下。
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using System.Windows.Forms;
using ESRI.ArcGIS.ArcMapUI;
namespace AE_test1.class_AE
{
/// <summary>
/// Command that works in ArcMap/Map/PageLayout
/// </summary>
[Guid("33766f65-aff7-45f1-8755-4ae854977568")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("AE_test1.class_AE.LayerPropertiesCmd")]
public sealed class LayerPropertiesCmd : 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);
MxCommands.Register(regKey);
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);
MxCommands.Unregister(regKey);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper = null;
IActiveView m_activeView = null;
IMapControl3 m_mapcontrol = null;
IFeatureLayer currentLayer = null;
public LayerPropertiesCmd()
{
base.m_category = "ControlsApplication";
base.m_caption = "图层属性";
base.m_message = "图层属性";
base.m_toolTip = "图层属性";
base.m_name = "LayerPropertiesCmd";
base.m_enabled = true;
try
{
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
MessageBox.Show("无效位图!!" + ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#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;
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
if (m_hookHelper.Hook is IToolbarControl)
{
IToolbarControl toolbarControl = m_hookHelper.Hook as IToolbarControl;
m_mapcontrol = (IMapControl3)toolbarControl.Buddy;
}
if (m_hookHelper.Hook is IMapControl3)
{
m_mapcontrol = m_hookHelper.Hook as IMapControl3;
}
if (m_mapcontrol != null)
{
currentLayer = m_mapcontrol.CustomProperty as IFeatureLayer;
m_activeView = m_mapcontrol.ActiveView;
}
if (currentLayer == null) return;
SetupFeaturePropertySheet(currentLayer);
}
#endregion
private bool SetupFeaturePropertySheet(ILayer layer)
{
if (layer == null) return false;
ESRI.ArcGIS.Framework.IComPropertySheet pComPropSheet;
pComPropSheet = new ESRI.ArcGIS.Framework.ComPropertySheet();
pComPropSheet.Title = layer.Name + " - 属性";
ESRI.ArcGIS.esriSystem.UID pPPUID = new ESRI.ArcGIS.esriSystem.UIDClass();
pComPropSheet.AddCategoryID(pPPUID);
// General....
ESRI.ArcGIS.Framework.IPropertyPage pGenPage = new ESRI.ArcGIS.CartoUI.GeneralLayerPropPageClass();
pComPropSheet.AddPage(pGenPage);
// Source
ESRI.ArcGIS.Framework.IPropertyPage pSrcPage = new ESRI.ArcGIS.CartoUI.FeatureLayerSourcePropertyPageClass();
pComPropSheet.AddPage(pSrcPage);
// Selection...
ESRI.ArcGIS.Framework.IPropertyPage pSelectPage = new ESRI.ArcGIS.CartoUI.FeatureLayerSelectionPropertyPageClass();
pComPropSheet.AddPage(pSelectPage);
// Display....
ESRI.ArcGIS.Framework.IPropertyPage pDispPage = new ESRI.ArcGIS.CartoUI.FeatureLayerDisplayPropertyPageClass();
pComPropSheet.AddPage(pDispPage);
// Symbology....
ESRI.ArcGIS.Framework.IPropertyPage pDrawPage = new ESRI.ArcGIS.CartoUI.LayerDrawingPropertyPageClass();
pComPropSheet.AddPage(pDrawPage);
// Fields...
ESRI.ArcGIS.Framework.IPropertyPage pFieldsPage = new ESRI.ArcGIS.CartoUI.LayerFieldsPropertyPageClass();
pComPropSheet.AddPage(pFieldsPage);
// Definition Query...
ESRI.ArcGIS.Framework.IPropertyPage pQueryPage = new ESRI.ArcGIS.CartoUI.LayerDefinitionQueryPropertyPageClass();
pComPropSheet.AddPage(pQueryPage);
// Labels....
ESRI.ArcGIS.Framework.IPropertyPage pSelPage = new ESRI.ArcGIS.CartoUI.LayerLabelsPropertyPageClass();
pComPropSheet.AddPage(pSelPage);
// Joins & Relates....
ESRI.ArcGIS.Framework.IPropertyPage pJoinPage = new ESRI.ArcGIS.ArcMapUI.JoinRelatePageClass();
pComPropSheet.AddPage(pJoinPage);
// Setup layer link
ESRI.ArcGIS.esriSystem.ISet pMySet = new ESRI.ArcGIS.esriSystem.SetClass();
pMySet.Add(layer);
pMySet.Reset();
// make the symbology tab active
pComPropSheet.ActivePage = 4;
// show the property sheet
bool bOK = pComPropSheet.EditProperties(pMySet, 0);
m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, m_activeView.Extent);
更新图例
//IMapSurround pMapSurround = null;
//ILegend pLegend = null;
//for (int i = 0; i < m_map.MapSurroundCount; i++)
//{
// pMapSurround = m_map.get_MapSurround(i);
// if (pMapSurround is ILegend)
// {
// pLegend = pMapSurround as ILegend;
// pLegend.AutoVisibility = true;
// pLegend.Refresh();
// }
//}
return (bOK);
}
}
}