AE二次开发-添加制图图例

Arcgis Engine二次开发C#常用库封装:Github源码

添加图例方法代码:

        /// <summary>
        /// 添加图例
        /// </summary>
        /// <param name="layoutControl">布局视图</param>
        /// <param name="pEnv">矩形框</param>
        public static void AddLegend(AxPageLayoutControl layoutControl, IEnvelope pEnv)
        {
            IGraphicsContainer pGraphicsContainer = layoutControl.PageLayout as IGraphicsContainer;
            if (pGraphicsContainer == null) return;
            IMap pMap = layoutControl.ActiveView.FocusMap;
            IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame;
            if (pMapFrame == null) return;
            UID pID = new UID();
            pID.Value = "esriCarto.Legend";
            IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pID, null);//根据唯一标示符,创建与之对应MapSurroundFrame
            IElement pDeletElement = layoutControl.FindElementByName("Legend");//获取PageLayout中的图例元素
            if (pDeletElement != null)
            {
                pGraphicsContainer.DeleteElement(pDeletElement);  //如果已经存在图例,删除已经存在的图例
            }
            //获取图例背景
            pMapSurroundFrame.Background = GetSymbolBackground();
            //添加图例
            IElement pElement = pMapSurroundFrame as IElement;
            if (pElement == null) return;
            pElement.Geometry = pEnv;
            IMapSurround pMapSurround = pMapSurroundFrame.MapSurround;
            ILegend2 pLegend = pMapSurround as ILegend2;
            if (pLegend == null) return;
            //设置图例属性
            SetLegendProperty(pLegend, pMap);
            pGraphicsContainer.AddElement(pElement, 0);
        }

        /// <summary>
        /// 获取图例背景
        /// </summary>
        /// <returns></returns>
        private static ISymbolBackground GetSymbolBackground()
        {
            //设置MapSurroundFrame背景
            ISymbolBackground pSymbolBackground = new SymbolBackgroundClass();
            IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
            ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
            pLineSymbol.Color = GetRgbColor(0, 0, 0);
            pFillSymbol.Color = GetRgbColor(240, 240, 240);
            pFillSymbol.Outline = pLineSymbol;
            pSymbolBackground.FillSymbol = pFillSymbol;
            return pSymbolBackground;
        }

        /// <summary>
        /// 设置图例属性
        /// </summary>
        /// <param name="pLegend">图例</param>
        /// <param name="pMap">地图</param>
        private static void SetLegendProperty(ILegend2 pLegend, IMap pMap)
        {
            pLegend.ClearItems();
            pLegend.Title = "图例";
            pLegend.AutoVisibility = true;
            for (int i = 0; i < pMap.LayerCount; i++)
            {
                ILegendItem pLegendItem = new HorizontalLegendItemClass();
                pLegendItem.Layer = pMap.Layer[i];//获取添加图例关联图层             
                pLegendItem.ShowDescriptions = false;
                pLegendItem.Columns = 1;
                pLegendItem.ShowHeading = true;
                pLegendItem.ShowLabels = true;
                pLegend.AddItem(pLegendItem);//添加图例内容
            }
        }

        /// <summary>
        /// 获取RGB颜色
        /// </summary>
        /// <param name="intR">红</param>
        /// <param name="intG">绿</param>
        /// <param name="intB">蓝</param>
        /// <returns></returns>
        private static IRgbColor GetRgbColor(int intR, int intG, int intB)
        {
            if (intR < 0 || intR > 255 || intG < 0 || intG > 255 || intB < 0 || intB > 255)
            {
                return null;
            }
            IRgbColor pRgbColor = new RgbColorClass();
            pRgbColor.Red = intR;
            pRgbColor.Green = intG;
            pRgbColor.Blue = intB;
            return pRgbColor;
        }

在布局视图上绘制矩形框,添加图例到矩形框内。
调用方法代码:

//在布局视图上绘制矩形框,添加图例到矩形框内
private void axPageLayoutControl1_OnMouseDown(object sender, IPageLayoutControlEvents_OnMouseDownEvent e)
        {
            if (e.button == 1) //左键点击
            {
                
                IGeometry pGeometry = axPageLayoutControl1.TrackRectangle();
                AddLegend(axPageLayoutControl1, (IEnvelope)pGeometry);
            }
            
        }

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值