1、027TOCControl上实现右键【027】TOCControl上实现右键- 目录:A1 第一种方法:contextMenuStripA2 第二种方法:IToolbarMenu1A ICommandSubType 接口2A IToolbarMenu 接口- 第A1个 第一种方法:contextMenuStrip:第一步:新建另外一个窗体 首先要定义一个全局变量 ILayer。 窗体要带参数,以便将 ILayer 传递过来。 获取属性列表。 ILayer pLayer; /定义一个虚拟的ILayer public Form2(ILayer layer) InitializeComponent(
2、); pLayer = layer; /将赋值的layer再赋值给pLayer,这样pLayer就是实体了 private void Form2_Load(object sender, EventArgs e) IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer; IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false); IFeature pFeat
3、ure = pFeatureCursor.NextFeature(); IFields pFields = pFeatureClass.Fields; DataTable pTable = new DataTable(); for (int i = 0; i pFields.FieldCount; i+) /获取所有列 DataColumn pColumn = new DataColumn(pFields.get_Field(i).Name); pTable.Columns.Add(pColumn); while (pFeature != null) DataRow pRow = pTable
4、.NewRow(); for (int i = 0; i pFields.FieldCount; i+) /添加每一列的值 pRowi = pFeature.get_Value(i); pTable.Rows.Add(pRow); pFeature = pFeatureCursor.NextFeature(); dataGridView1.DataSource = pTable; 第二步:建立右键菜单项 添加一个 ContextMenuStrip 控件,然后增加几个菜单项。 特别注意加入一个菜单项为“显示属性”。 判断在什么情况下显示右键菜单。 ILayer m_Layer;/沟通图层 pri
5、vate void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e) if (e.button = 2) esriTOCControlItem Item = esriTOCControlItem.esriTOCControlItemNone; IBasicMap pBasicMap = null; ILayer pLayer = null; object other = null; object index = null; axTOCControl1.HitTest(e.x, e.y,
6、ref Item, ref pBasicMap, ref pLayer, ref other, ref index); /实现赋值 m_Layer = pLayer; if (Item = esriTOCControlItem.esriTOCControlItemLayer) /点击的是图层的话,就显示右键菜单 contextMenuStrip2.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y); /显示右键菜单,并定义其相对控件的位置,正好在鼠标出显示 private void 显示属性ToolStripMenuItem_Click
7、(object sender, EventArgs e) Form2 frm = new Form2(m_Layer); /定义窗体,注意窗体的参数 frm.Text = Attributes of + m_Layer.Name; /显示标题 frm.ShowDialog(); /以对话框形式显示窗体 效果如下所示:- 第A2个 第二种方法:IToolbarMenu:第一步:新建菜单框架:IToolbarMenu m_TOCMapMenu = new ToolbarMenu();IToolbarMenu m_TOCLayerMenu = new ToolbarMenu();第二步:在菜单框架中
8、加入菜单项,用 AddItem 方法实现:m_TOCLayerMenu.Caption = Sub Menu; /二级菜单设置名称m_TOCLayerMenu.AddItem(new ControlsMapZoomInToolClass(), 0, 0, false, esriCommandStyles.esriCommandStyleIconAndText);m_TOCLayerMenu.AddItem(new ControlsMapZoomInToolClass(), 0, 1, true, esriCommandStyles.esriCommandStyleIconAndText);m_
9、TOCLayerMenu.AddItem(new ControlsMapZoomInToolClass(), 0, 2, false, esriCommandStyles.esriCommandStyleIconAndText);m_TOCLayerMenu.SetHook(axMapControl1); /作用在 MapControl 上面m_TOCMapMenu.AddItem(new ControlsAddDataCommandClass(), 0, 0, false, esriCommandStyles.esriCommandStyleIconAndText);m_TOCMapMenu
10、.AddItem(new ControlsMapFullExtentCommand(), 0, 1, false, esriCommandStyles.esriCommandStyleIconAndText);m_TOCMapMenu.AddItem(new ControlsMapZoomInToolClass(), 0, 2, true, esriCommandStyles.esriCommandStyleIconAndText);m_TOCMapMenu.AddItem(new ControlsMapZoomOutToolClass(), 0, 3, false, esriCommandS
11、tyles.esriCommandStyleIconAndText);m_TOCMapMenu.AddItem(new ControlsPagePanToolClass(), 0, 4, false, esriCommandStyles.esriCommandStyleIconAndText);m_TOCMapMenu.AddSubMenu(m_TOCLayerMenu, 5, false);m_TOCMapMenu.SetHook(axMapControl1);第三步:TOCControl 的 MouseDown 事件:private void axTOCControl1_OnMouseDo
12、wn(object sender, ITOCControlEvents_OnMouseDownEvent e) esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone; IBasicMap map = null; ILayer layer = null; object other = null; object index = null; axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index); if (e
13、.button = 2) if (item = esriTOCControlItem.esriTOCControlItemMap) axTOCControl1.SelectItem(map, null); m_TOCMapMenu.PopupMenu(e.x, e.y, axTOCControl1.hWnd); 实现效果如下:public int AddItem ( object item, int SubType, int index, bool beginGroup, esriCommandStyles Style);第一个参数:菜单项的内容,功能实现。第二个参数:对于一个工具定义多个 t
14、ype 的时候,才会用到,每一个 int 代表一个新的实现。第三个参数:索引值,在菜单项上面显示的位置。默认为 -1,按书写顺序排序。第四个参数:是否开始一个新组,就是在其上面有一个“”的效果。第五个参数:显示样式。用 SubType 实现:首先:建立工具,这个时候类不仅要继承 BaseCommand 类,还要继承 ICommandSubType 接口- 第1A个 ICommandSubType 接口:Provides access to members that define a subtyped command.MembersDescriptionGetCountThe number of
15、 commands defined with this CLSID.SetSubTypeThe subtype of the command.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 ESRI.ArcGIS.SystemUI;namespace ESRI_01
16、 / / Summary description for ScaleThresholdCmd. / Guid(41b9d7ee-ba25-47d1-9644-f9eca4243c34) ClassInterface(ClassInterfaceType.None) ProgId(ESRI_01.ScaleThresholdCmd) public sealed class ScaleThresholdCmd : BaseCommand, ICommandSubType/要多继承一个接口! #region COM Registration Function(s) ComRegisterFuncti
17、on() 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 reg
18、isterType) / Required for ArcGIS Component Category Registrar support ArcGISCategoryUnregistration(registerType); / / TODO: Add any COM unregistration code here / #region ArcGIS Component Category Registrar generated code / / Required method for ArcGIS Component Category registration - / Do not modi
19、fy the contents of this method with the code editor. / private static void ArcGISCategoryRegistration(Type registerType) string regKey = string.Format(HKEY_CLASSES_ROOTCLSID0, registerType.GUID); ControlsCommands.Register(regKey); / / Required method for ArcGIS Component Category unregistration - /
20、Do not modify the contents of this method with the code editor. / private static void ArcGISCategoryUnregistration(Type registerType) string regKey = string.Format(HKEY_CLASSES_ROOTCLSID0, registerType.GUID); ControlsCommands.Unregister(regKey); #endregion #endregion private IHookHelper m_hookHelper
21、; private IMapControl4 m_mapControl; private long m_subType;/用来分类的 public ScaleThresholdCmd() #region Overriden Class Methods/重写的方法 / / Occurs when this command is created / / Instance of the application / public override string Caption/重写标题 get if (m_subType = 1) return 设置最大显示比例尺; else if (m_subTyp
22、e = 2) return 设置最小显示比例尺; else return 取消比例尺显示限制; public override bool Enabled/重写 Enabled 属性 get bool enabled = true;/默认情况下都是 true 的 ILayer layer = (ILayer)m_mapControl.CustomProperty;/获取所指的 layer,在 MouseDown 事件中有获取到这个 layer。 if (m_subType = 3)/如果选择的是 3,且最大和最小比例尺都没有设置,则为 false! if (layer.MaximumScale
23、= 0) & (layer.MinimumScale = 0) enabled = false; return enabled; public override void OnCreate(object hook)/重写创建方法 if (hook is IMapControl4) m_mapControl = (IMapControl4)hook;/给 m_mapControl 赋值 / / Occurs when this command is clicked / public override void OnClick()/重写单击方法 / TODO: Add ScaleThreshold
24、Cmd.OnClick implementation IMap map = m_mapControl.Map; ILayer layer = m_mapControl.CustomProperty as ILayer; if (m_subType = 1) layer.MaximumScale = map.MapScale;/为 1 的时候,实现最大显示比例尺 else if (m_subType = 2) layer.MinimumScale = map.MapScale;/为 2 的时候,实现最小显示比例尺 else if (m_subType = 3) layer.MaximumScal
25、e = 0;/为 3 的时候,去除这些 layer.MinimumScale = 0; #endregion #region ICommandSubType 成员/继承接口的话,就一定要实现接口了! public int GetCount()/重写 ICommandSubType 的 GetCount 方法 return 3; public void SetSubType(int SubType)/重写 ICommandSubType 的 SetSubType 方法 m_subType = SubType; #endregion 最后通过下面的 1、2、3 来实现即可!m_TOCLayerMe
26、nu.AddItem(new ScaleThresholdCmd(), 1, 2, true, esriCommandStyles.esriCommandStyleTextOnly);m_TOCLayerMenu.AddItem(new ScaleThresholdCmd(), 2, 3, false, esriCommandStyles.esriCommandStyleTextOnly);m_TOCLayerMenu.AddItem(new ScaleThresholdCmd(), 3, 4, false, esriCommandStyles.esriCommandStyleTextOnly);m_TOCLayerMenu.SetHook(axMapControl1);- 第2A个 IToolbarMenu
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1