ArcGIS Engine添加地圖元素的實現


在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二次開發,如果有不對的地方,請多多包涵!

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM