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二次开发,如果有不对的地方,请多多包涵!

猜你喜欢

转载自www.cnblogs.com/xyy2019/p/11792927.html