[AE] 地图整饰-添加地图框架元素(指北针、比例尺、图例、标题)


地图整饰-添加地图框架元素

添加地图框架元素

知识拓展:http://blog.csdn.net/summer_dew/article/details/79187995

思路:

  1. 获取PageLayout对象

    IPageLayout pPageLayout = axPageLayoutControl1.PageLayout;
    
  2. 获取活动视图ActiveView

    IActiveView pActiveView = axPageLayoutControl1.ActiveView;
    
  3. 获取图形容器GraphicsContainer

    IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer;
    
  4. 找到MapFrame(地图框架)对象

    IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame;
    
  5. 创建MapSurroundFrame对象(创建图例)

    UID pUid = new UIDClass();
    pUid.Value = "esriCarto.Legend";
    IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid,null);
    
  6. 添加MapSurroundFrame的空间位置

    IElement pElement = pMapSurroundFrame as IElement;
    pElement.Geometry = axPageLayoutControl1.TrackRectangle();
    
  7. 将元素添加到容器中

    pGraphicsContainer.AddElement(pElement,0);
    
  8. 刷新

    axPageLayoutControl1.Refresh();
    

代码:

int pageFlag = -1;
//PageLayout点击事件
private void axPageLayoutControl1_OnMouseDown(object sender, IPageLayoutControlEvents_OnMouseDownEvent e)
{
    if (e.button == 1)
    {
        return;
    }
    else if (e.button == 2)
    {
        switch (pageFlag)
        {
            case 1: //图名
                {
                    //找到PageLayout
                    IPageLayout pPageLayout = axPageLayoutControl1.PageLayout;
                    //找到元素容器
                    IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer;
                    //创建元素
                    ITextElement pTextElement = new TextElementClass();
                    pTextElement.Text = "世界地图";
                    ITextSymbol pTextSymbol = new TextSymbolClass();
                    pTextSymbol.Size = 30;
                    pTextSymbol.Color = GetRGB(255, 0, 0);
                    pTextElement.Symbol = pTextSymbol;
                    //设置位置                        
                    IElement pElement = pTextElement as IElement;
                    pElement.Geometry = axPageLayoutControl1.TrackRectangle();
                    //将元素添加到容器中
                    pGraphicsContainer.AddElement(pElement, 0);
                    //刷新
                    axPageLayoutControl1.Refresh();
                    break;
                }
            case 2: //比例尺
                {
                    IPageLayout pPageLayout = axPageLayoutControl1.PageLayout;
                    IActiveView pActiveView = axPageLayoutControl1.ActiveView;
                    IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer;
                    IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame;
                    UID pUid = new UIDClass();
                    pUid.Value = "esriCato.AlternatingScaleBar";
                    IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null);
                    IElement pElement = pMapSurroundFrame as IElement;
                    pElement.Geometry = axPageLayoutControl1.TrackRectangle();
                    pGraphicsContainer.AddElement(pElement, 0);
                    axPageLayoutControl1.Refresh();
                    break;
                }
            case 3: //图例
                {
                    IPageLayout pPageLayout = axPageLayoutControl1.PageLayout;
                    IActiveView pActiveView = axPageLayoutControl1.ActiveView;
                    IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer;
                    IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame;
                    UID pUid = new UIDClass();
                    pUid.Value = "esriCato.Legend";
                    IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null);
                    IElement pElement = pMapSurroundFrame as IElement;
                    pElement.Geometry = axPageLayoutControl1.TrackRectangle();
                    pGraphicsContainer.AddElement(pElement, 0);
                    axPageLayoutControl1.Refresh();
                    break;
                }
            case 4: //指北针
                {
                    IPageLayout pPageLayout = axPageLayoutControl1.PageLayout;
                    IActiveView pActiveView = axPageLayoutControl1.ActiveView;
                    IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer;
                    IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame;
                    UID pUid = new UIDClass();
                    pUid.Value = "esriCato.MarkerNorthArrow";
                    IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null);
                    IElement pElement = pMapSurroundFrame as IElement;
                    pElement.Geometry = axPageLayoutControl1.TrackRectangle();
                    pGraphicsContainer.AddElement(pElement, 0);
                    axPageLayoutControl1.Refresh();
                    break;
                }
        }
        pageFlag = -1;
        return;
    }

}

转载自:https://blog.csdn.net/summer_dew/article/details/79188134

You may also like...