|
本帖最后由 化云随风 于 2018-2-27 11:48 编辑
因为涉及到的图层比较多,图例占比太大,想要实现ArcMap中的功能,添加图例时可以自己设置图例分几列,网上没找到相关资料,涉及到多个图层组,目前输出情况截图如下
private void btnAddLegend_ItemClick(object sender, ItemClickEventArgs e)
{
IActiveView pActiveView = this.axPageLayoutControlThematic.ActiveView as IActiveView;
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 = axPageLayoutControlThematic.FindElementByName("Legend");//获取PageLayout中的图例元素
if (pDeletElement != null)
{
pGraphicsContainer.DeleteElement(pDeletElement); //如果已经存在图例,删除已经存在的图例
}
//添加图例
IElement pElement = pMapSurroundFrame as IElement;
IEnvelope pEnv = null;
if (null == pEnv)
{
pEnv = new EnvelopeClass();
pEnv.PutCoords(1, 1, 2, 2);
}
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.NewColumn = false;
pLegendItem.Layer = layer;//获取添加图例关联图层
pLegendItem.ShowDescriptions = false;
pLegendItem.Columns = 3;
pLegendItem.LayerNameSymbol = txtSymbol;
pLegendItem.ShowHeading = true;
pLegendItem.ShowLabels = true;
//IMapSurroundFrame
return pLegendItem;
}
|
-
|