上一節已經介紹過圖層樹如何和地圖和製圖關聯,圖層樹右鍵菜單主要是基於TocControl控件進行對菜單節點進行控制,TocControl主要做用是顯示當前加載的圖層有哪些、採用什麼樣的符號等,目的是使用戶對當前加載的數據和結構有一個整體的把握。node
圖層樹與命令關聯綁定是將具體的代碼進行封裝轉換爲Command或者Tool,爲了簡化代碼,便於調用,調試,使用簡單。this
1、 界面設計spa
在界面添加控件ConTextMenuStrip,新建兩個ToolStripMenuItem,以添加數據和刪除圖層爲例;設計
2、 功能實現3d
圖層樹的右鍵菜單的彈出,首先須要右鍵圖層樹,觸發tocControlMain_MouseClick 點擊事件;調試
1 /// <summary> 2 /// 右鍵菜單事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void tocControlMain_MouseClick(object sender, MouseEventArgs e) 7 { 8 if (e.Button == MouseButtons.Right)//右鍵 9 { 10 IMap pMap = null; 11 m_Layer = null; 12 PIETOCNodeType nodeType = PIETOCNodeType.Null; 13 object unk = null; 14 object data = null; 15 16 this.tocControlMain.HitTest(e.X, e.Y, ref nodeType, ref pMap, ref m_Layer, ref unk, ref data); 17 //判斷點擊的節點是哪一個類型,彈出指定的右鍵菜單 18 if (nodeType == PIETOCNodeType.Map) 19 { 20 //控制菜單項的顯示隱藏 21 this.ToolStripMenuItem_LoadData.Visible = true;//顯示加載數據菜單選項 22 this.ToolStripMenuItem_DeleteLayer.Visible = false;//不顯示刪除圖層菜單選項 23 } 24 else 25 { 26 //控制菜單項的顯示隱藏 27 this.ToolStripMenuItem_LoadData.Visible = false;//不顯示加載數據 28 this.ToolStripMenuItem_DeleteLayer.Visible = true;//顯示刪除圖層菜單選項 29 } 30 this.contextMenuStrip1.Show(this.tocControlMain, new Point(e.X, e.Y)); //右鍵菜單顯示 31 } 32 } 33 34 /// <summary> 35 /// 右鍵菜單加載數據事件 36 /// </summary> 37 /// <param name="sender"></param> 38 /// <param name="e"></param> 39 private void ToolStripMenuItem_LoadData_Click(object sender, EventArgs e) 40 { 41 // 得到要打開Shape數據的路徑 42 OpenFileDialog openFileDialog = new OpenFileDialog(); 43 openFileDialog.Title = "選擇打開數據"; 44 openFileDialog.Filter = "Shape Files|*.shp;*.000|Raster Files|*.tif;*.tiff;*.dat;*.bmp;*.img;*.jpg|HDF Files|*.hdf;*.h5|NC Files|*.nc"; 45 if (openFileDialog.ShowDialog() != DialogResult.OK) return; 46 47 // 打開圖層 48 String filePath = openFileDialog.FileName; 49 ILayer layer = PIE.Carto.LayerFactory.CreateDefaultLayer(filePath); 50 if (layer == null) return; 51 52 // 添加圖層到地圖並刷新 53 mapControlMain.FocusMap.AddLayer(layer); 54 mapControlMain.ActiveView.PartialRefresh(PIE.Carto.ViewDrawPhaseType.ViewAll); 55 } 56 57 /// <summary> 58 /// 右鍵菜單刪除圖層事件 59 /// </summary> 60 /// <param name="sender"></param> 61 /// <param name="e"></param> 62 private void ToolStripMenuItem_DeleteLayer_Click(object sender, EventArgs e) 63 { 64 if (m_Layer == null) return; 65 mapControlMain.FocusMap.DeleteLayer(m_Layer); 66 mapControlMain.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll); 67 }
拓展:圖層樹的命令綁定code
圖層樹右鍵菜單的選項的具體實現功能方式:一種是直接將代碼寫在事件裏面,(如上面的添加數據和刪除圖層事件),有時候代碼量會很大;另外一種是直接封裝代碼成命令Command或者是Tool,在菜單選項中直接調用命令便可,很明顯後者比較輕便且快捷視頻
以修改刪除圖層事件爲例:blog
1 /// <summary> 2 /// 右鍵菜單刪除圖層事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void ToolStripMenuItem_DeleteLayer_Click(object sender, EventArgs e) 7 { 8 //方法一 9 //if (m_Layer == null) return; 10 //mapControlMain.FocusMap.DeleteLayer(m_Layer); 11 //mapControlMain.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll); 12 13 //方法二 14 ICommand cmd = new Command.DeleLayerCommand(); 15 cmd.OnCreate(mapControlMain); 16 cmd.OnClick(); 17 }
項目路徑教程 |
百度雲盤地址下/PIE示例程序/11.圖層樹功能/ PIEDemo_SetBuddy |
數據路徑 |
百度雲盤地址下/PIE示例數據/柵格數據/ World/World.tif |
視頻路徑 |
百度雲盤地址下/PIE視頻教程/11.圖層樹功能/02.圖圖層樹右鍵操做.avi |