添加图例

原文地址:http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=79939

/// <summary>
        /// 添加图例
        /// </summary>
        /// <param name="pageLayout"></param>
        public static void AddLegendToPagelayout(IPageLayout pageLayout)
        {
            if (pageLayout == null)
            {
                return;
            }
            IActiveView activeView = pageLayout as IActiveView;
            //定义图例UID对象
            UID uid = new UIDClass();
            uid.Value = "esriCore.Legend";
            //设置图例存放的坐标位置  
            //定义单位
            //pageLayout.Page.Units = esriUnits.esriPoints;
            //得到草图容器对象
            IGraphicsContainer container = pageLayout as IGraphicsContainer;
            //得到当前地图的框架
            IMapFrame frameElement = container.FindFrame(activeView.FocusMap) as IMapFrame;
            //IElement mapElement = frameElement as IElement;
            //IEnvelope mapEnv = mapElement.Geometry.Envelope;           
            //创建图例
            IMapSurroundFrame pMapSurroundFrame = frameElement.CreateSurroundFrame(uid, null);
            if (pMapSurroundFrame==null)
            {
                return;
            }
            IMapSurround pMapSurround = pMapSurroundFrame.MapSurround;
            ILegend2 legend = pMapSurround as ILegend2;
            legend.Title = "图例";
            #region 图例格式
            //图标格式
            ILegendFormat pLegendFormat = new LegendFormatClass();
            object areaPathch = GetStyleByNameAndCategory("Horizontal", "Default", "Area Patches");
            if (areaPathch != null)
            {
                //面图例时
                IAreaPatch pAreaPatch = areaPathch as IAreaPatch;
                pLegendFormat.DefaultAreaPatch = pAreaPatch;
            }
            object linePatch = GetStyleByNameAndCategory("Horizontal", "Default", "Line Patches");
            if (linePatch != null)
            {
                //线图例时
                ILinePatch pLinePatch = linePatch as ILinePatch;
                pLegendFormat.DefaultLinePatch = pLinePatch;
            }
            pLegendFormat.DefaultPatchHeight = 14;
            pLegendFormat.DefaultPatchWidth = 28;
            pLegendFormat.HeadingGap=5;
            pLegendFormat.TitleGap = 5;
            pLegendFormat.TextGap = 5;
            pLegendFormat.VerticalPatchGap = 5;
            pLegendFormat.VerticalItemGap = 5;
            pLegendFormat.HorizontalPatchGap = 5;
            pLegendFormat.HorizontalItemGap = 5; 
            #endregion          
            ITextSymbol symbol = new TextSymbolClass();
            //symbol.Text = "图例";
            System.Drawing.Font ft = new System.Drawing.Font("宋体", 1);
            IFontDisp iFontDispFromFont = (IFontDisp)OLE.GetIFontDispFromFont(ft);
            symbol.Font = iFontDispFromFont;
            symbol.Size = 14;
            IRgbColor color = (IRgbColor)ESRI.ArcGIS.ADF.Converter.ToRGBColor(Color.Black);
            symbol.Color = color;
            //文字水平方向的对齐方式
            symbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter;
            pLegendFormat.TitleSymbol = symbol;
            legend.Format = pLegendFormat;
            #region 先注释的图例项
            //添加图例项
            legend.ClearItems();
            IMap map = activeView.FocusMap;            
            for (int i = 0; i < map.LayerCount; i++)
            {
                if (i <= 4)
                {//去除多余的图层
                    IFeatureLayer layer = (IFeatureLayer)map.get_Layer(i) as IFeatureLayer;  
                    //图例项风格
                    ILegendItem item = new HorizontalLegendItemClass();
                    item.Columns =(short)1;  
                    
                    item.Layer = layer;
                    item.ShowLabels = true;
                    //item.ShowLayerName = true;
                    item.ShowDescriptions = true;                    
                    legend.AddItem(item);
                    legend.Refresh();
                    //hight += item.Height;
                }
            } 
            #endregion
            //显示的图例名称           
            //legend.Title = "图例";            
            legend.FlowRight = true;
            legend.AdjustColumns(1);
            legend.AutoAdd = true;
            legend.AutoReorder = true;
            legend.AutoVisibility = true;
            #region 外边框属性设置
            //IFrameProperties properties = frame as IFrameProperties;
            ////外边框
            //ISymbolBorder border = new SymbolBorderClass();
            //border.Gap = 0.0;
            //border.CornerRounding = 0;
            //IBorder border2 = border;
            //properties.Border = border2;
            //IFrameDecoration decoration = new SymbolBackgroundClass();
            //IRgbColor color1 = (IRgbColor)ESRI.ArcGIS.ADF.Converter.ToRGBColor(Color.LightPink);
            //color1.Transparency = 50;
            //decoration.Color = color1;
            //decoration.CornerRounding = 0;
            //decoration.HorizontalSpacing = 0.0;
            //decoration.VerticalSpacing = 0.0;
            //properties.Background = ((IBackground)decoration); 
            #endregion
            IElement element = pMapSurroundFrame as IElement;
            IEnvelope envelope = new EnvelopeClass();
            //通过当前地图框架得到相对位置            
            IPoint pPoint = new PointClass();
            double mapX = 4.4951;//先写死,通过配置文件设置
            double mapY = 5.151;//先写死,通过配置文件设置
            pPoint.PutCoords(mapX, mapY);
            envelope.LowerLeft = pPoint;
            envelope.Height = 2;// hight;//先写死,通过配置文件设置
            envelope.Width = 2;// width;//先写死,通过配置文件设置
            element.Geometry = envelope as IGeometry;

            //element.Geometry = envelope;
            //element.Activate(activeView.ScreenDisplay);
            container.AddElement(element, 0);
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }

原文地址:https://www.cnblogs.com/daidaigua/p/3259495.html