[ArcEngine地图制图系列]—添加图例

来源:互联网 发布:mac 网页原型设计工具 编辑:程序博客网 时间:2024/05/11 22:40
      private void AddLegend(IActiveView pActiveView, IEnvelope pEnv)
        {
            IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
            UID pID = new UID();
            pID.Value = "esriCarto.Legend";

            IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame;
            IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pID, null);//根据唯一标示符,创建与之对应MapSurroundFrame

            IElement pDeletElement = axPageControl.FindElementByName("Legend");//获取PageLayout中的图例元素
            if (pDeletElement != null)
            {
                pGraphicsContainer.DeleteElement(pDeletElement);  //如果已经存在图例,删除已经存在的图例
            }
            //设置MapSurroundFrame背景
            ISymbolBackground pSymbolBackground = new SymbolBackgroundClass();
            ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
            pLineSymbol.Color = ColorUtilty.GetRgbColor(0, 0, 0);
            IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
            pFillSymbol.Color = ColorUtilty.GetRgbColor(240, 240, 240);
            pFillSymbol.Outline = pLineSymbol;
            pSymbolBackground.FillSymbol = pFillSymbol;
            pMapSurroundFrame.Background = pSymbolBackground;
            //添加图例
            IElement pElement = pMapSurroundFrame as IElement;
            pElement.Geometry = pEnv as IGeometry;
            IMapSurround pMapSurround = pMapSurroundFrame.MapSurround;
            ILegend pLegend = pMapSurround as ILegend;
            pLegend.ClearItems();
            pLegend.Title = "图例";

            for (int i = 0; i < pActiveView.FocusMap.LayerCount; i++)
            {
                ILayer pLayer = pActiveView.FocusMap.get_Layer(i);
                if (pLayer.Visible == true)
                {
                    if (pLayer is IGroupLayer || pLayer is ICompositeLayer)
                    {
                        ICompositeLayer pCompositeLayer = (ICompositeLayer)pLayer;
                        for (int j = pCompositeLayer.Count - 1; j >= 0; j--)
                        {
                            ILayer pSubLayer = pCompositeLayer.get_Layer(j);
                            ILegendItem pItem = SetItemStyle(pSubLayer);
                            pLegend.AddItem(pItem);//添加图例内容
                        }
                    }
                    else
                    {
                        ILegendItem pItem = SetItemStyle(pLayer);
                        pLegend.AddItem(pItem);//添加图例内容
                    }
                }
            }
            
            pGraphicsContainer.AddElement(pElement, 0);
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }

        private ILegendItem SetItemStyle(ILayer layer)
        {
            StdFont myFont = new stdole.StdFontClass();
            myFont.Name = "宋体";
            myFont.Size = 10;

            ITextSymbol txtSymbol = new TextSymbolClass();
            txtSymbol.Font = (IFontDisp)myFont;

            ILegendItem pLegendItem = new HorizontalLegendItemClass();
            pLegendItem.Layer = layer;//获取添加图例关联图层             
            pLegendItem.ShowDescriptions = false;
            pLegendItem.Columns = 1;
            pLegendItem.LayerNameSymbol = txtSymbol;
            pLegendItem.ShowHeading = true;
            pLegendItem.ShowLabels = true;

            return pLegendItem;
        }

0 0
原创粉丝点击