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);
}
}