1、027TOCControl上实现右键【027 】TOCControl 上实现右键-目录:A1 第一种方法: contextMenuStripA2 第二种方法: IToolbarMenu1AICommandSubType接口2AIToolbarMenu接口-第 A1 个第一种方法:contextMenuStrip:第一步:新建另外一个窗体首先要定义一个全局变量窗体要带参数,以便将ILayer ILayer。传递过来。获取属性列表。ILayer pLayer;/定义一个虚拟的ILayerpublicForm2(ILayer layer)InitializeComponent();pLayer =
2、layer;/将赋值的layer再赋值给pLayer,这样pLayer就是实体了privatevoidForm2_Load( objectsender, EventArgs e)IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);IFeature pFeature = pFeatureCursor
3、.NextFeature();IFields pFields = pFeatureClass.Fields;DataTable pTable =new DataTable();for ( int i =0; i pFields.FieldCount; i+)/获取所有列DataColumn pColumn = pTable.Columns.Add(pColumn);new DataColumn(pFields.get_Field(i).Name);while(pFeature !=null)DataRow pRow = pTable.NewRow();for ( int i = 0; i pF
4、ields.FieldCount; i+)/添加每一列的值pRowi = pFeature.get_Value(i);pTable.Rows.Add(pRow);pFeature = pFeatureCursor.NextFeature();dataGridView1.DataSource = pTable;第二步:建立右键菜单项添加一个 ContextMenuStrip 控件,然后增加几个菜单项。特别注意加入一个菜单项为 “显示属性 ”。判断在什么情况下显示右键菜单。ILayer m_Layer; / 沟通图层private void axTOCControl1_OnMouseDown( o
5、bjectITOCControlEvents_OnMouseDownEvent e)sender,if(e.button =2)esriTOCControlItem Item =esriTOCControlItem.esriTOCControlItemNone;IBasicMap pBasicMap =ILayer pLayer = nullobject other = nullobject index = nullaxTOCControl1.HitTest(e.x, e.y,pLayer, ref other, ref index);m_Layer = pLayer;null;ref/ 实现
6、赋值Item,refpBasicMap,refif(Item = esriTOCControlItem.esriTOCControlItemLayer)/点击的是图层的话,就显示右键菜单contextMenuStrip2.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y);/ 显示右键菜单,并定义其相对控件的位置,正好在鼠标出显示privatevoid显示属性ToolStripMenuItem_Click(objectsender,EventArgs e)Form2 frm =new Form2(m_Layer);/ 定义窗体,注意窗体
7、的参数frm.Text =Attributes of + m_Layer.Name;/ 显示标题frm.ShowDialog();/ 以对话框形式显示窗体效果如下所示:- 第 A2 个 第二种方法: IToolbarMenu :第一步:新建菜单框架:IToolbarMenu m_TOCMapMenu = new ToolbarMenu();IToolbarMenu m_TOCLayerMenu= new ToolbarMenu();第二步:在菜单框架中加入菜单项,用AddItem方法实现:m_TOCLayerMenu.Caption =Sub Menu ;/二级菜单设置名称m_TOCLayer
8、Menu.AddItem(new ControlsMapZoomInToolClass(),0,0,false,esriCommandStyles.esriCommandStyleIconAndText);m_TOCLayerMenu.AddItem(new ControlsMapZoomInToolClass(),0,1,true,esriCommandStyles.esriCommandStyleIconAndText);m_TOCLayerMenu.AddItem(new ControlsMapZoomInToolClass(),0,2,false,esriCommandStyles.e
9、sriCommandStyleIconAndText);m_TOCLayerMenu.SetHook(axMapControl1);/作用在 MapControl上面m_TOCMapMenu.AddItem(new ControlsAddDataCommandClass(),0,0,false,esriCommandStyles.esriCommandStyleIconAndText);m_TOCMapMenu.AddItem(new ControlsMapFullExtentCommand(),0,1, false,esriCommandStyles.esriCommandStyleIcon
10、AndText);m_TOCMapMenu.AddItem(new ControlsMapZoomInToolClass(),0, 2,true,esriCommandStyles.esriCommandStyleIconAndText);m_TOCMapMenu.AddItem(new ControlsMapZoomOutToolClass(),0,3,false,esriCommandStyles.esriCommandStyleIconAndText);m_TOCMapMenu.AddItem(new ControlsPagePanToolClass(),0,4 ,false,esriC
11、ommandStyles.esriCommandStyleIconAndText);m_TOCMapMenu.AddSubMenu(m_TOCLayerMenu,5, false);m_TOCMapMenu.SetHook(axMapControl1);第三步: TOCControl 的 MouseDown 事件:private void axTOCControl1_OnMouseDown( object ITOCControlEvents_OnMouseDownEvent e)sender,esriTOCControlItem item =esriTOCControlItem.esriTOC
12、ControlItemNone;IBasicMap map = null ;ILayer layer = null ;object other = null ;object index = null ;axTOCControl1.HitTest(e.x,e.y,refitem, refmap, reflayer,refother,ref index);if(e.button =2 )if(item =esriTOCControlItem.esriTOCControlItemMap)axTOCControl1.SelectItem(map,null );m_TOCMapMenu.PopupMen
13、u(e.x, e.y, axTOCControl1.hWnd);实现效果如下:public int AddItem (object item,int SubType,int index,bool beginGroup,esriCommandStyles Style);第一个参数:菜单项的内容,功能实现。第二个参数:对于一个工具定义多个 type 的时候,才会用到,每一个 int 代表一个新的实现。第三个参数:索引值,在菜单项上面显示的位置。默认为 -1 ,按书写顺序排序。第四个参数:是否开始一个新组,就是在其上面有一个 “”的效果。第五个参数:显示样式。用 SubType 实现:首先:建立工具
14、,这个时候类不仅要继承 BaseCommand 类,还要继承ICommandSubType 接口- 第 1A 个 ICommandSubType 接口:Provides access to members that define a subtyped command.MembersDescriptionGetCountThe number of commands defined with this CLSID.SetSubTypeThe subtype of the command.usingusingusingusingusingusingusingusingSystem;System.Dr
15、awing;System.Runtime.InteropServices;ESRI.ArcGIS.ADF.BaseClasses;ESRI.ArcGIS.ADF.CATIDs;ESRI.ArcGIS.Controls;ESRI.ArcGIS.Carto;ESRI.ArcGIS.SystemUI;namespace ESRI_01/Summary description for ScaleThresholdCmd./Guid(41b9d7ee-ba25-47d1-9644-f9eca4243c34)ClassInterface(ClassInterfaceType.None)ProgId(ESR
16、I_01.ScaleThresholdCmd)publicsealedclassScaleThresholdCmd : BaseCommand,ICommandSubType/ 要多继承一个接口!#region COM Registration Function(s)ComRegisterFunction()ComVisible( false )static void RegisterFunction(Type registerType)/ Required for ArcGIS ComponentCategoryRegistrarsupportArcGISCategoryRegistrati
17、on(registerType);/ TODO: Add any COM registration code here/ComUnregisterFunction()ComVisible( false )static void UnregisterFunction(Type registerType)/ Required for ArcGIS ComponentCategoryRegistrarsupportArcGISCategoryUnregistration(registerType);/ TODO: Add any COM unregistration code here/#regio
18、n ArcGIS Component Category Registrar generated code/Required method for ArcGIS Component Category registration-/Do not modify the contents of this method with the codeeditor./private staticvoidArcGISCategoryRegistration(TyperegisterType)string regKey =string .Format( HKEY_CLASSES_ROOTCLSID0register
19、Type.GUID);ControlsCommands.Register(regKey);,/Required method for ArcGIS Component Categoryunregistration -/Do not modify the contents of this method with the codeeditor./private staticvoidArcGISCategoryUnregistration(TyperegisterType)string regKey =string .Format( HKEY_CLASSES_ROOTCLSID0registerTy
20、pe.GUID);ControlsCommands.Unregister(regKey);,#endregion#endregionprivateprivateprivateIHookHelper m_hookHelper; IMapControl4 m_mapControl; long m_subType;/ 用来分类的publicScaleThresholdCmd()#region Overriden Class Methods / 重写的方法/Occurs when this command is created/ Instance of the application/public o
21、verride string Caption / 重写标题getif(m_subType =1)return 设置最大显示比例尺 ;elseif(m_subType =2)return 设置最小显示比例尺 ;elsereturn 取消比例尺显示限制 ;public override bool Enabled / 重写 Enabled 属性getbool enabled = true ; / 默认情况下都是 trueILayer layer = (ILayer)m_mapControl.CustomProperty;/ 获取所指的 layer ,在 MouseDown 事件中有获取到这个 lay
22、er 。的没有设置,则为iffalse(m_subType =!3)/ 如果选择的是3 ,且最大和最小比例尺都if(layer.MaximumScale =0 ) & (layer.MinimumScale =0)enabled= false;return enabled;publicoverridevoidOnCreate(objecthook)/ 重写创建方法if (hookm_mapControlisIMapControl4)= (IMapControl4)hook;/ 给m_mapControl赋值/Occurs when this command is clicked/public
23、override void OnClick()/ 重写单击方法/ TODO: Add ScaleThresholdCmd.OnClick implementationIMap map = m_mapControl.Map;ILayer layer = m_mapControl.CustomProperty as ILayer;if (m_subType = 1 )layer.MaximumScale= map.MapScale;/ 为1的时候,实现最大显示比例尺elseif(m_subType =2)layer.MinimumScale= map.MapScale;/ 为2的时候,实现最小显示比例尺
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1