添加地图图例 Arcengine+C#
private void MakeLegend(IActiveView activeView,IPageLayout pageLayout)
{
//定义图例UID对象
UID uid = new UIDClass();
uid.Value="esriCore.Legend";
//设置图例存放的坐标位置
//定义单位
pageLayout.Page.Units = esriUnits.esriCentimeters;
//得到草图容器对象
IGraphicsContainer container = pageLayout as IGraphicsContainer;
//得到当前地图的框架
IMapFrame frameElement = container.FindFrame(activeView.FocusMap) as IMapFrame;
IElement mapElement = frameElement as IElement;
IEnvelope mapEnv = mapElement.Geometry.Envelope;
IEnvelope envelope = new EnvelopeClass();
//通过当前地图框架得到相对位置
envelope.PutCoords(mapEnv.XMin, mapEnv.YMin, mapEnv.XMin + 6.5, mapEnv.YMin + 0.8);
IMapSurroundFrame frame = frameElement.CreateSurroundFrame(uid, null);
ILegend legend = frame.MapSurround as ILegend;
ILegendFormat format = new LegendFormatClass();
format.TitlePosition = esriRectanglePosition.esriTopSide;
format.LayerNameGap=0.0;
format.TextGap=0.0;
format.TitleGap=0.0;
format.HeadingGap=0.0;
format.HorizontalItemGap=0.0;
format.VerticalItemGap=0.0;
format.ShowTitle=true;
ITextSymbol symbol = new TextSymbolClass();
symbol.Text="图例";
System.Drawing.Font ft = new System.Drawing.Font("宋体", 5);
IFontDisp iFontDispFromFont = (IFontDisp)OLE.GetIFontDispFromFont(ft);
symbol.Font=iFontDispFromFont;
symbol.Size = 11.5;
IRgbColor color = (IRgbColor)ESRI.ArcGIS.ADF.Converter.ToRGBColor(Color.Black);
symbol.Color = color;
//文字水平方向的对齐方式
symbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter;
format.TitleSymbol=symbol;
legend.Format=format;
legend.Title="图例";
legend.FlowRight = true;
legend.ClearItems();
IMap map = activeView.FocusMap;
for (int i = 0; i < map.LayerCount; i++)
{
IFeatureLayer layer = (IFeatureLayer)map.get_Layer(i) as IFeatureLayer;
ILegendItem item = new HorizontalLegendItemClass();
item.Columns=(short)map.LayerCount;
item.NewColumn=true;
ITextSymbol symbolItem = new TextSymbolClass();
symbolItem.Size = 11.5;
ILegendClassFormat legClsFormat = new LegendClassFormatClass();
legClsFormat.LabelSymbol = symbolItem;
legClsFormat.PatchHeight = 40;
legClsFormat.PatchWidth = 50;
item.LegendClassFormat = legClsFormat;
item.Layer=layer;
item.KeepTogether=false;
legend.AddItem(item);
}
frame.MapSurround.Name="myLegend";
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.LightSeaGreen);
color1.Transparency = 50;
decoration.Color = color1;
decoration.CornerRounding=0;
decoration.HorizontalSpacing=0.0;
decoration.VerticalSpacing=0.0;
properties.Background=((IBackground)decoration);
IElement element = frame as IElement;
element.Geometry=envelope;
element.Activate(activeView.ScreenDisplay);
container.AddElement(element, 0);
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}