您好,登錄后才能下訂單哦!
ArcEngine開發(fā)中右鍵菜單的設(shè)計(jì)與實(shí)現(xiàn)是怎樣的,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
利用ArcGIS Engine開發(fā)自定義GIS應(yīng)用程序中,一般有兩種方式來建立右鍵菜單。
一是利用開發(fā)工具自帶的右鍵菜單控件,如Visual Studio中的ContextMenuStrip控件;
二是利用ArcGIS Engine封裝好的IToolbarMenu接口。相比較而言,前者實(shí)現(xiàn)起來較簡(jiǎn)單,但后者使程序具有更好的面向?qū)ο笮裕哺跀U(kuò)展,在大型系統(tǒng)中,使用該方法是較好的選擇。
詳細(xì)講述如何利用IToolbarMenu接口實(shí)現(xiàn)右鍵菜單功能。
設(shè)計(jì)程序主界面如下圖所示:
在FrmMain類中首先定義如下指針:
private ITOCControl2 pTocControl; private IMapControl3 pMapControl; private IToolbarMenu pToolMenuMap; private IToolbarMenu pToolMenuLayer;
在FrmMain窗體的加載事件中初始化這些指針:
// 取得 MapControl 和 PageLayoutControl 的引用 pTocControl = (ITOCControl2)axTOCControl1.Object; pMapControl = (IMapControl3)axMapControl1.Object; // 創(chuàng)建菜單 pToolMenuMap = new ToolbarMenuClass(); pToolMenuLayer = new ToolbarMenuClass();
如此便建立了新的菜單,但其中沒有實(shí)際的菜單項(xiàng),具體的命令或者工具必須作為菜單項(xiàng)添加到菜單中才能實(shí)現(xiàn)相應(yīng)的功能。
在添加菜單項(xiàng)之前,得先實(shí)現(xiàn)相應(yīng)命令或工具。下面自定義一個(gè)縮放至圖層的命令,添加一個(gè)類ZoomToLayer.cs至該項(xiàng)目,繼承自ESRI.ArcGIS.ADF.BaseClasses.BaseCommand。BaseCommand是一個(gè)抽象類,它為開發(fā)人員建立自定義命令項(xiàng)提供了一種有效的途徑。重寫基類的OnCreate和OnClick方法,實(shí)現(xiàn)縮放圖層至完整的顯示于地圖控件中。完整代碼如下。
using System; using System.Collections.Generic; using System.Text; using ESRI.ArcGIS.ADF.BaseClasses; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; namespace ArcEngine3_3 { class ZoomToLayer : BaseCommand { //定義指針 private IMapControl3 pMapControl; public ZoomToLayer() { base.m_caption = "放大至該圖層"; } //重寫B(tài)aseCommand基類的虛擬方法OnClick() public override void OnClick() { ILayer pLayer = (ILayer)pMapControl.CustomProperty; pMapControl.Extent = pLayer.AreaOfInterest; } //重寫B(tài)aseCommand基類的抽象方法OnCreate(object hook) public override void OnCreate(object hook) { pMapControl = (IMapControl3)hook; } } }
除自定義實(shí)現(xiàn)命令或者工具之外, ArcGIS Engine內(nèi)置了許多可以直接調(diào)用的常用命令和工具,如ControlsAddDataCommandClass、ControlsClearSelectionCommandClass等,也可以直接調(diào)用AE內(nèi)置的菜單,如ControlsFeatureSelectionMenu。
建立好自定義命令或工具后,就可以向菜單中添加對(duì)應(yīng)的菜單項(xiàng)了。在FrmMain窗體的加載事件中添加菜單項(xiàng)。
pToolMenuLayer.AddItem(new ZoomToLayer(), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);
設(shè)置菜單的hook
pToolMenuLayer.SetHook(pMapControl);
現(xiàn)在有了菜單,需要在TocControl中右鍵彈出,所以在其OnMouseDown/OnMouseUp事件中獲取鼠標(biāo)點(diǎn)擊信息,然后彈出對(duì)應(yīng)菜單即可。
//獲取鼠標(biāo)點(diǎn)擊信息 axTOCControl1.HitTest(e.x, e.y, ref pTocItem, ref pBasicMap, ref pLayer, ref oLegendGroup, ref oIndex); if (e.button == 2) { if (pTocItem == esriTOCControlItem.esriTOCControlItemMap) { pTocControl.SelectItem(pBasicMap, null); } else { pTocControl.SelectItem(pLayer, null); } //設(shè)置CustomProperty為layer (用于自定義的Layer命令) pMapControl.CustomProperty = pLayer; //彈出右鍵菜單 if (pTocItem == esriTOCControlItem.esriTOCControlItemMap) { pToolMenuMap.PopupMenu(e.x, e.y, pTocControl.hWnd); } else { pToolMenuLayer.PopupMenu(e.x, e.y, pTocControl.hWnd); } }
用同樣的方法可以為TocControl中的地圖、圖層、符號(hào)以及為MapControl等添加各自的右鍵菜單。最終實(shí)現(xiàn)效果如下。
關(guān)于ArcEngine開發(fā)中右鍵菜單的設(shè)計(jì)與實(shí)現(xiàn)是怎樣的問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。