原文地址:http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=79939php
/// <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);
}框架