try
{
LayoutElements layoutElements = m_MapLayoutControl.MapLayout.Elements;
if (GetMapID() < 0) return;
layoutElements.SeekID(GetMapID()); //根据图例中地图的id进行查找获取地图对象
GeoMap geoMap = (GeoMap)layoutElements.GetGeometry();
string geoMapName = geoMap.MapName;
GeoLegend geoLegend = new GeoLegend(geoMapName, m_workspace);
geoLegend.Height = 175;
geoLegend.Width = 950;
geoLegend.Center = new Point2D(575, 655);
GeoStyle geoLegendStyle = new GeoStyle();
geoLegendStyle.FillForeColor = Color.FromArgb(255, 235, 175);
geoLegendStyle.FillOpaqueRate = 30;
geoLegendStyle.LineWidth = 0.5;
geoLegendStyle.LineColor = Color.FromArgb(65, 65, 65);
geoLegend.BackGroundStyle = geoLegendStyle;
geoLegend.ColumnCount = 3;
//设置图例项和图例子项的说明文本的风格
TextStyle geoLegendtextStyle = new TextStyle();
geoLegendtextStyle.BackColor = Color.Yellow;
geoLegendtextStyle.ForeColor = Color.Blue;
geoLegendtextStyle.FontName = "宋体";
geoLegendtextStyle.FontHeight = 20.0;
geoLegendtextStyle.FontWidth = 12.0;
geoLegendtextStyle.IsSizeFixed = false;
geoLegend.ItemTextStyle = geoLegendtextStyle;
geoLegend.SubItemTextStyle = geoLegendtextStyle;
//设置图例标题风格
TextStyle titleTextStyle = new TextStyle();
titleTextStyle.BackColor = Color.Yellow;
titleTextStyle.ForeColor = Color.Blue;
titleTextStyle.FontName = "宋体";
titleTextStyle.FontHeight = 40.0;
titleTextStyle.FontWidth = 25.0;
titleTextStyle.Italic = true;
titleTextStyle.Bold = true;
titleTextStyle.IsSizeFixed = false;
titleTextStyle.Weight = 500;
geoLegend.Title = "图例";
geoLegend.TitleStyle = titleTextStyle;
//将图例添加到布局图层,而非屏幕图层。
geoLegend.Load(false);
m_MapLayoutControl.MapLayout.Elements.AddNew(geoLegend);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}