ArcGIS Engine新增地圖元素的實現
阿新 • • 發佈:2019-11-04
在ArcGIS中,我們使用的製圖控制元件除了MapControl之外,還有PageLayoutControl,用於頁面佈局和製圖,生成一幅成品地圖。
PageLayoutControl 封裝了PageLayout物件,提供佈局檢視中控制元素的屬性和方法,其中包括圖形的位置屬性、標尺和對齊網格的設定,以及確定頁面顯示在螢幕上的方法。
我們將實現在佈局檢視下的新增圖例、指北針、比例尺和文字的操作。
新增地圖元素:
/// <summary> /// 新增地圖元素 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 新增地圖元素ToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { //排除資料檢視下不能插入 if (tabControl1.SelectedIndex == 0) { return; } //使用UID識別操作命令 UID uid = new UIDClass(); if (e.ClickedItem.Text != "") { //e是滑鼠操作所返回的物件, 攜帶了相關的操作資訊 switch (e.ClickedItem.Text) { case "圖例": //定義好UID的樣式為Carto.legend uid.Value = "ESRICarto.legend"; //呼叫自定義方法AddElementInpageLayer, 下同 AddElementInPageLayer(uid); break; case "指北針": //定義好UID的樣式為Carto.MarkerNorthArrow uid.Value = "ESRICarto.MarkerNorthArrow"; AddElementInPageLayer(uid); break; case "比例尺": //定義好UID的樣式為ESRICarto.ScaleLine ?? AddScalebar(axPageLayoutControl1.PageLayout, axPageLayoutControl1.ActiveView.FocusMap); break; case "文字": TextInput txtInput = new TextInput(); txtInput.ShowDialog(); //呼叫自定義方法加入圖名 AddTextElement(axPageLayoutControl1, txtInput.Fontsize, txtInput.ThimaticMapName); break; default: break; } } }
1、圖例或指北針
/// <summary> /// 新增圖例或指北針——根據UID元素新增相應的元素 /// </summary> /// <param name="uid"></param> private void AddElementInPageLayer(UID uid) { //提供對控制圖形容器的成員的訪問。 IGraphicsContainer graphicsContainer = axPageLayoutControl1.PageLayout as IGraphicsContainer; //提供對成員的訪問, 控制map元素的物件, IMapFrame是地圖瀏覽欄物件的預設介面 //通過FindFrame方法, 查詢axPageLayoutControl1中螢幕包含指定物件的框架 IMapFrame mapFrame = graphicsContainer.FindFrame(axPageLayoutControl1.ActiveView.FocusMap) as IMapFrame; //提供對成員的訪問, 控制地圖環繞元素對映的介面, 是附屬物框架的物件的預設介面 //通過CreateSurroundFrame方法建立基於當前地圖框下的一個新地圖環繞元素(如圖例、指北針) IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uid, null); //IElement是所有圖形元素和框架元素類都要實現的介面 //將mapSurroundFrame強轉成IElement型別 IElement element = mapSurroundFrame as IElement; //例項化一個包絡線 IEnvelope envelope = new EnvelopeClass(); //設定座標 envelope.PutCoords(1, 1, 2, 2); //設定元素中的幾何形狀 element.Geometry = envelope; try { //提供對控制圖例的成員的訪問。 ILegend legend = (ILegend)mapSurroundFrame.MapSurround; legend.Title = "圖例"; } catch { } graphicsContainer.AddElement(element, 0); //設定元素將在axPageLayoutControl螢幕上顯示圖形 element.Activate(axPageLayoutControl1.ActiveView.ScreenDisplay); //部分重新整理 axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }
2、比例尺
/// <summary> /// 新增比例尺 /// </summary> /// <param name="pageLayout"></param> /// <param name="map"></param> private void AddScalebar(IPageLayout pageLayout, IMap map) { if (pageLayout == null || map == null) { return;//當pageLayerout和map為空時返回 } //例項化一個包絡線 IEnvelope envelope = new EnvelopeClass(); //設定座標 envelope.PutCoords(1, 1, 3, 2); //例項化一個uid IUID uid = new UIDClass(); //將uid設定為ESRICarto.scalebar uid.Value = "ESRICarto.scalebar"; //提供對控制圖形容器的成員的訪問 IGraphicsContainer graphicsContainer = pageLayout as IGraphicsContainer; //查詢map中指定物件的框架 IMapFrame mapFrame = graphicsContainer.FindFrame(map) as IMapFrame; //建立基於當前地圖框下的一個新地圖環繞元素 IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uid as UID, null); //元素屬性 IElementProperties pElePro; //例項化一個比例尺物件 IScaleBar markerScaleBar = new AlternatingScaleBarClass(); //可以有多種比例尺型別 markerScaleBar.Division = 2; markerScaleBar.Divisions = 2; markerScaleBar.LabelPosition = esriVertPosEnum.esriAbove; markerScaleBar.Map = map; markerScaleBar.Subdivisions = 2; markerScaleBar.UnitLabel = ""; markerScaleBar.UnitLabelGap = 4; markerScaleBar.UnitLabelPosition = esriScaleBarPos.esriScaleBarAbove; //位於比例尺上方 markerScaleBar.Units = esriUnits.esriKilometers; //千米 mapSurroundFrame.MapSurround = markerScaleBar; //將mapSurroundFrame強轉為IElementProperties pElePro = mapSurroundFrame as IElementProperties; //設定元素Name屬性 pElePro.Name = "my scale"; //新增元素至axPageLayoutControl1 axPageLayoutControl1.AddElement(mapSurroundFrame as IElement, envelope, Type.Missing, Type.Missing, 0); //部分重新整理 axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Type.Missing, null); }
3、文字
/// <summary> /// 新增文字 /// </summary> /// <param name="axPageLayoutControl1">目標PageLayoutControl的Name屬性</param> /// <param name="fontsize">字型尺寸</param> /// <param name="thimaticMapName">圖名</param> private void AddTextElement(AxPageLayoutControl axPageLayoutControl1, decimal fontsize, string thimaticMapName) { //建立PageLayout物件 IPageLayout pPageLayout = axPageLayoutControl1.PageLayout; //將PageLayout強轉成IActiveView IActiveView pAV = (IActiveView)pPageLayout; //將PageLayout強轉成IGraphicsContainer IGraphicsContainer graphicsContainer = (IGraphicsContainer)pPageLayout; //例項化文字元素 ITextElement pTextElement = new TextElementClass(); //例項化字型元素 IFontDisp pFont = new StdFontClass() as IFontDisp; pFont.Bold = true; pFont.Name = "宋體"; pFont.Size = fontsize; //例項化IRgbColor IRgbColor pColor = new RgbColorClass(); pColor.Red = 0; pColor.Green = 0; pColor.Blue = 0; //例項化文字符號 ITextSymbol pTextSymbol = new TextSymbolClass(); pTextSymbol.Color = (IColor)pColor; pTextSymbol.Font = pFont; //賦值元素文字和符號 pTextElement.Text = thimaticMapName; pTextElement.Symbol = pTextSymbol; //例項化一個點 IPoint pPoint = new PointClass(); pPoint.X = 1; pPoint.Y = 1; //例項化一個元素 IElement pElement = (IElement)pTextElement; pElement.Geometry = (IGeometry)pPoint; graphicsContainer.AddElement(pElement, 0); //真正實現部分重新整理 pAV.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }
核心AddElementInPageLayer(UID uid)函式總結:
謝謝觀看!本人初學GIS二次開發,如果有不對的地方,請多多包涵!
&n