027TOCControl上实现右键.docx
- 文档编号:8856226
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:16
- 大小:74.32KB
027TOCControl上实现右键.docx
《027TOCControl上实现右键.docx》由会员分享,可在线阅读,更多相关《027TOCControl上实现右键.docx(16页珍藏版)》请在冰豆网上搜索。
027TOCControl上实现右键
【027】TOCControl上实现右键
---------------------------------------------------------------------------------------------------------
●·●目录:
A1………… 第一种方法:
contextMenuStrip
A2………… 第二种方法:
IToolbarMenu
1A………… ICommandSubType接口
2A………… IToolbarMenu接口
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A1个 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 第一种方法:
contextMenuStrip:
第一步:
新建另外一个窗体
∙首先要定义一个全局变量ILayer。
∙窗体要带参数,以便将ILayer传递过来。
∙获取属性列表。
ILayerpLayer;//定义一个虚拟的ILayer
publicForm2(ILayerlayer)
{
InitializeComponent();
pLayer=layer;//将赋值的layer再赋值给pLayer,这样pLayer就是实体了
}
privatevoidForm2_Load(objectsender,EventArgse)
{
IFeatureLayerpFeatureLayer=pLayerasIFeatureLayer;
IFeatureClasspFeatureClass=pFeatureLayer.FeatureClass;
IFeatureCursorpFeatureCursor=pFeatureClass.Search(null,false);
IFeaturepFeature=pFeatureCursor.NextFeature();
IFieldspFields=pFeatureClass.Fields;
DataTablepTable=newDataTable();
for(inti=0;i { DataColumnpColumn=newDataColumn(pFields.get_Field(i).Name); pTable.Columns.Add(pColumn); } while(pFeature! =null) { DataRowpRow=pTable.NewRow(); for(inti=0;i { pRow[i]=pFeature.get_Value(i); } pTable.Rows.Add(pRow); pFeature=pFeatureCursor.NextFeature(); } dataGridView1.DataSource=pTable; } 第二步: 建立右键菜单项 ∙添加一个ContextMenuStrip控件,然后增加几个菜单项。 ∙特别注意加入一个菜单项为“显示属性”。 ∙判断在什么情况下显示右键菜单。 ILayerm_Layer; //沟通图层 privatevoidaxTOCControl1_OnMouseDown(objectsender,ITOCControlEvents_OnMouseDownEvente) { if(e.button==2) { esriTOCControlItemItem=esriTOCControlItem.esriTOCControlItemNone; IBasicMappBasicMap=null; ILayerpLayer=null; objectother=null; objectindex=null; axTOCControl1.HitTest(e.x,e.y,refItem,refpBasicMap,refpLayer,refother,refindex);//实现赋值 m_Layer=pLayer; if(Item==esriTOCControlItem.esriTOCControlItemLayer)//点击的是图层的话,就显示右键菜单 { contextMenuStrip2.Show(axTOCControl1,newSystem.Drawing.Point(e.x,e.y)); //显示右键菜单,并定义其相对控件的位置,正好在鼠标出显示 } } } privatevoid显示属性ToolStripMenuItem_Click(objectsender,EventArgse) { Form2frm=newForm2(m_Layer);//定义窗体,注意窗体的参数 frm.Text="Attributesof"+m_Layer.Name;//显示标题 frm.ShowDialog();//以对话框形式显示窗体 } 效果如下所示: --------------------------------------------------------------------------------------------------------- ╔════════╗ ╠════╣ 第A2个 ╠══════════════════════════════════════════════════╣ ╚════════╝ ●·● 第二种方法: IToolbarMenu: 第一步: 新建菜单框架: IToolbarMenum_TOCMapMenu=newToolbarMenu(); IToolbarMenum_TOCLayerMenu=newToolbarMenu(); 第二步: 在菜单框架中加入菜单项,用AddItem方法实现: m_TOCLayerMenu.Caption="SubMenu";//二级菜单设置名称 m_TOCLayerMenu.AddItem(newControlsMapZoomInToolClass(),0,0,false,esriCommandStyles.esriCommandStyleIconAndText); m_TOCLayerMenu.AddItem(newControlsMapZoomInToolClass(),0,1,true,esriCommandStyles.esriCommandStyleIconAndText); m_TOCLayerMenu.AddItem(newControlsMapZoomInToolClass(),0,2,false,esriCommandStyles.esriCommandStyleIconAndText); m_TOCLayerMenu.SetHook(axMapControl1);//作用在MapControl上面 m_TOCMapMenu.AddItem(newControlsAddDataCommandClass(),0,0,false,esriCommandStyles.esriCommandStyleIconAndText); m_TOCMapMenu.AddItem(newControlsMapFullExtentCommand(),0,1,false,esriCommandStyles.esriCommandStyleIconAndText); m_TOCMapMenu.AddItem(newControlsMapZoomInToolClass(),0,2,true,esriCommandStyles.esriCommandStyleIconAndText); m_TOCMapMenu.AddItem(newControlsMapZoomOutToolClass(),0,3,false,esriCommandStyles.esriCommandStyleIconAndText); m_TOCMapMenu.AddItem(newControlsPagePanToolClass(),0,4,false,esriCommandStyles.esriCommandStyleIconAndText); m_TOCMapMenu.AddSubMenu(m_TOCLayerMenu,5,false); m_TOCMapMenu.SetHook(axMapControl1); 第三步: TOCControl的MouseDown事件: privatevoidaxTOCControl1_OnMouseDown(objectsender,ITOCControlEvents_OnMouseDownEvente) { esriTOCControlItemitem=esriTOCControlItem.esriTOCControlItemNone; IBasicMapmap=null; ILayerlayer=null; objectother=null; objectindex=null; axTOCControl1.HitTest(e.x,e.y,refitem,refmap,reflayer,refother,refindex); if(e.button==2) { if(item==esriTOCControlItem.esriTOCControlItemMap) { axTOCControl1.SelectItem(map,null); m_TOCMapMenu.PopupMenu(e.x,e.y,axTOCControl1.hWnd); } } } 实现效果如下: publicintAddItem( objectitem, intSubType, intindex, boolbeginGroup, esriCommandStylesStyle ); 第一个参数: 菜单项的内容,功能实现。 第二个参数: 对于一个工具定义多个type的时候,才会用到,每一个int代表一个新的实现。 第三个参数: 索引值,在菜单项上面显示的位置。 默认为-1,按书写顺序排序。 第四个参数: 是否开始一个新组,就是在其上面有一个“——”的效果。 第五个参数: 显示样式。 用SubType实现: 首先: 建立工具,这个时候类不仅要继承BaseCommand类,还要继承ICommandSubType接口 --------------------------------------------------------------------------------------------------------- ╔════════╗ ╠════╣ 第1A个 ╠══════════════════════════════════════════════════╣ ╚════════╝ ●·● ICommandSubType接口: Providesaccesstomembersthatdefineasubtypedcommand. Members Description GetCount ThenumberofcommandsdefinedwiththisCLSID. SetSubType Thesubtypeofthecommand. usingSystem; usingSystem.Drawing; usingSystem.Runtime.InteropServices; usingESRI.ArcGIS.ADF.BaseClasses; usingESRI.ArcGIS.ADF.CATIDs; usingESRI.ArcGIS.Controls; usingESRI.ArcGIS.Carto; usingESRI.ArcGIS.SystemUI; namespaceESRI_01 { /// ///SummarydescriptionforScaleThresholdCmd. /// [Guid("41b9d7ee-ba25-47d1-9644-f9eca4243c34")] [ClassInterface(ClassInterfaceType.None)] [ProgId("ESRI_01.ScaleThresholdCmd")] publicsealedclassScaleThresholdCmd: BaseCommand,ICommandSubType //要多继承一个接口! { #regionCOMRegistrationFunction(s) [ComRegisterFunction()] [ComVisible(false)] staticvoidRegisterFunction(TyperegisterType) { //RequiredforArcGISComponentCategoryRegistrarsupport ArcGISCategoryRegistration(registerType); // //TODO: AddanyCOMregistrationcodehere // } [ComUnregisterFunction()] [ComVisible(false)] staticvoidUnregisterFunction(TyperegisterType) { //RequiredforArcGISComponentCategoryRegistrarsupport ArcGISCategoryUnregistration(registerType); // //TODO: AddanyCOMunregistrationcodehere // } #regionArcGISComponentCategoryRegistrargeneratedcode /// ///RequiredmethodforArcGISComponentCategoryregistration- ///Donotmodifythecontentsofthismethodwiththecodeeditor. /// privatestaticvoidArcGISCategoryRegistration(TyperegisterType) { stringregKey=string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}",registerType.GUID); ControlsCommands.Register(regKey); } /// ///RequiredmethodforArcGISComponentCategoryunregistration- ///Donotmodifythecontentsofthismethodwiththecodeeditor. /// privatestaticvoidArcGISCategoryUnregistration(TyperegisterType) { stringregKey=string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}",registerType.GUID); ControlsCommands.Unregister(regKey); } #endregion #endregion privateIHookHelperm_hookHelper; privateIMapControl4m_mapControl; privatelongm_subType; //用来分类的 publicScaleThresholdCmd() { } #regionOverridenClassMethods //重写的方法 /// ///Occurswhenthiscommandiscreated /// /// /// publicoverridestringCaption //重写标题 { get { if(m_subType==1) { return"设置最大显示比例尺"; } elseif(m_subType==2) { return"设置最小显示比例尺"; } else { return"取消比例尺显示限制"; } } } publicoverrideboolEnabled //重写Enabled属性 { get { boolenabled=true; //默认情况下都是true的 ILayerlayer=(ILayer)m_mapControl.CustomProperty; //获取所指的layer,在MouseDown事件中有获取到这个layer。 if(m_subType==3) //如果选择的是3,且最大和最小比例尺都没有设置,则为false! { if((layer.MaximumScale==0)&&(layer.MinimumScale==0)) { enabled=false; } } returnenabled; } } publicoverridevoidOnCreate(objecthook) //重写创建方法 { if(hookisIMapControl4) m_mapControl=(IMapControl4)hook; //给m_mapControl赋值 } /// ///Occurswhenthiscommandisclicked /// publicoverridevoidOnClick() //重写单击方法 { //TODO: AddScaleThresholdCmd.OnClickimplementation IMapmap=m_mapControl.Map; ILayerlayer=m_mapControl.CustomPropertyasILayer; if(m_subType==1) { layer.MaximumScale=map.MapScale; //为1的时候,实现最大显示比例尺 } elseif(m_subType==2) { layer.MinimumScale=map.MapScale; //为2的时候,实现最小显示比例尺 } elseif(m_subType==3) { layer.MaximumScale=0; //为3的时候,去除这些 layer.MinimumScale=0; } } #endregion #regionICommandSubType成员 //继承接口的话,就一定要实现接口了! publicintGetCount() //重写ICommandSubType的GetCount方法 { return3; } publicvoidSetSubType(intSubType) //重写ICommandSubType的SetSubType方法 { m_subType=SubType; } #endregion } } 最后通过下面的1、2、3来实现即可! m_TOCLayerMenu.AddItem(newScaleThresholdCmd(),1,2,true,esriCommandStyles.esriCommandStyleTextOnly); m_TOCLayerMenu.AddItem(newScaleThresholdCmd(),2,3,false,esriCommandStyles.esriCommandStyleTextOnly); m_TOCLayerMenu.AddItem(newScaleThresholdCmd(),3,4,false,esriCommandStyles.esriCommandStyleTextOnly); m_TOCLayerMenu.SetHook(axMapControl1); --------------------------------------------------------------------------------------------------------- ╔════════╗ ╠════╣ 第2A个 ╠══════════════════════════════════════════════════╣ ╚════════╝ ●·● IToolbarMenu
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 027 TOCControl 实现 右键
![提示](https://static.bdocx.com/images/bang_tan.gif)