AE中TOC右鍵功能
阿新 • • 發佈:2022-03-21
圖層右鍵功能
圖層右鍵功能中用到了contextMenuStrip控制元件,前文有提到,詳細可參照上文,若有任何問題歡迎留言交流!
1、開啟屬性表
主窗體程式碼:
private void 開啟屬性表ToolStripMenuItem_Click(object sender, EventArgs e) { try { AttributeForm attributeForm = new AttributeForm(); attributeForm.CurFeatureLayer= pTocFeatureLayer; attributeForm.InitUI(); attributeForm.ShowDialog(); //顯示右鍵選單,並定義其相對控制元件的位置,正好在鼠標出顯示 } catch { MessageBox.Show("屬性表為空"); } }
屬性表窗體程式碼:
public partial class AttributeForm : Form {public AttributeForm() { InitializeComponent(); } private IFeatureLayer curFeatureLayer; public IFeatureLayer CurFeatureLayer { get { return curFeatureLayer; } set { curFeatureLayer = value; } } publicvoid InitUI() { if (curFeatureLayer == null) return; IFeature pFeature = null; DataTable pFeatDT = new DataTable(); DataRow pDataRow = null; DataColumn pDatacol = null; IField pField = null; //遍歷圖層的每一個欄位 for (int i = 0; i < curFeatureLayer.FeatureClass.Fields.FieldCount; i++) { pDatacol = new DataColumn(); pField = curFeatureLayer.FeatureClass.Fields.get_Field(i); pDatacol.ColumnName = pField.AliasName; pDatacol.DataType = Type.GetType("System.Object"); pFeatDT.Columns.Add(pDatacol); } IFeatureCursor pFeatureCursor = curFeatureLayer.Search(null, true); pFeature = pFeatureCursor.NextFeature(); while (pFeature != null) { pDataRow = pFeatDT.NewRow(); for (int k = 0; k < pFeatDT.Columns.Count; k++) { pDataRow[k] = pFeature.get_Value(k); } pFeatDT.Rows.Add(pDataRow); pFeature = pFeatureCursor.NextFeature(); } System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor); dataGridView1.DataSource = pFeatDT; } }
2、縮放至圖層
private void 縮放至圖層ToolStripMenuItem_Click(object sender, EventArgs e) { if (pTocFeatureLayer == null) return; (axMapControl1.Map as IActiveView).Extent = pTocFeatureLayer.AreaOfInterest; (axMapControl1.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); }
3、移除圖層
private void 移除ToolStripMenuItem_Click(object sender, EventArgs e) { if (pTocFeatureLayer == null) return; DialogResult result = MessageBox.Show("是否移除[" + pTocFeatureLayer.Name + "]圖層", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (result == DialogResult.OK) { axMapControl1.Map.DeleteLayer(pTocFeatureLayer); axMapControl1.ActiveView.Refresh(); } } private void 移除所有圖層ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("是否移除所有圖層", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (result == DialogResult.OK) { axMapControl1.Map.ClearLayers(); axTOCControl1.Update();//更新圖層欄圖表資訊 axMapControl1.Refresh(); //axMapControl2.Refresh(); } }
4、標註
基本思路:建立顏色→建立字型→建立符號→刪除已有元素→遍歷要素新增標註→重新整理地圖
private void 標註ToolStripMenuItem_Click(object sender, EventArgs e) { try { // 建立顏色(字型顏色) IRgbColor pRgbColor = new RgbColor(); pRgbColor.Red = 49; pRgbColor.Green = 234; pRgbColor.Blue = 26; // 建立字型 IFontDisp pFontDisp = new StdFont() as IFontDisp; pFontDisp.Bold = true; pFontDisp.Name = "楷體"; pFontDisp.Size = 12;//字型大小 // 建立符號 ITextSymbol pTextSymbol = new TextSymbol(); pTextSymbol.Angle = 0; pTextSymbol.Color = pRgbColor; pTextSymbol.Font = pFontDisp; // 刪除已有文字元素 IActiveView pActiveView = axMapControl1.ActiveView; IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer; pGraphicsContainer.DeleteAllElements(); // 獲取要素遊標 IFeatureLayer pFeatureLayer = pTocFeatureLayer; IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true); IFeature pFeature = pFeatureCursor.NextFeature(); // 遍歷要素遊標 int fieldIndex = pFeatureClass.Fields.FindField("F1");//標註欄位 while (pFeature != null) { // 獲取重心 IArea pArea = pFeature.ShapeCopy as IArea; IPoint pPoint = pArea.Centroid; // 建立文字元素 ITextElement pTextElement = new TextElement() as ITextElement; pTextElement.Symbol = pTextSymbol; pTextElement.Text = pFeature.get_Value(fieldIndex).ToString(); // 新增文字元素 IElement pElement = pTextElement as IElement; pElement.Geometry = pPoint; pGraphicsContainer.AddElement(pElement, 0); pFeature = pFeatureCursor.NextFeature(); } // 重新整理地圖 System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
也是想出一個系列吧,在自己剛開始學習AE開發的時候苦於查詢書籍程式碼不可用或者各部落格描述不夠詳盡到我一個小白可以讀懂,然後踩了各種各樣的坑(雖然踩過的坑忘記了很多),其間很多程式碼的產生都是始於Copy,然後結合自己的查詢資料和思考所得,還是希望自己能幫助到各位正在學習或使用的各位同仁吧。
掃碼關注公眾號