/// <summary> /// 设置图例属性 /// </summary> /// <param name="pLegend">图例</param> /// <param name="pMap">地图</param> private static void SetLegendProperty(ILegend2 pLegend, IMap pMap) { pLegend.ClearItems(); pLegend.Title = "Legend"; pLegend.AutoVisibility = true; ILegendFormat lFormat = pLegend.Format; lFormat.DefaultPatchHeight = 20; lFormat.DefaultPatchWidth = 30; lFormat.TitleSymbol = GetTextSymbol(); for (int i = 0; i < pMap.LayerCount; i++) { ILegendItem pLegendItem = new HorizontalLegendItemClass(); pLegendItem.Layer = pMap.Layer[i]; //获取添加图例关联图层 pLegendItem.Columns = 2; ILegendClassFormat iClassFormat = pLegendItem.LegendClassFormat; iClassFormat.LabelSymbol = GetTextSymbol(); //iClassFormat.DescriptionSymbol = GetTextSymbol(); pLegendItem.HeadingSymbol = GetTextSymbol(); pLegendItem.ShowDescriptions = false; pLegendItem.ShowHeading = true; pLegendItem.ShowLabels = true; pLegendItem.ShowLayerName = false; pLegend.AddItem(pLegendItem); //添加图例内容 } }
/// <summary> /// 添加默认图例 /// </summary> /// <param name="layoutControl">布局视图</param> /// <param name="pEnv">矩形框</param> public static void AddDefaultLegend(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 { Value = "esriCarto.Legend" }; IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pID, null); //根据唯一标示符,创建与之对应MapSurroundFrame //如果已经存在图例,删除已经存在的图例 MappingHelper.DeleteElementByName(layoutControl, "Legend"); //添加图例 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); //添加元素 MappingHelper.AddElementWithName(pGraphicsContainer, pElement, "Legend"); }
// 图例 private void AddLegend(AxPageLayoutControl axPageLayout) { //删除已经存在的图例 IElement pElement = axPageLayout.FindElementByName("Legends"); if (pElement != null) { axPageLayout.ActiveView.GraphicsContainer.DeleteElement(pElement); } IActiveView pActiveView = axPageLayout.PageLayout as IActiveView; IGraphicsContainer container = axPageLayout.PageLayout as IGraphicsContainer; // 获得MapFrame IMapFrame mapFrame = container.FindFrame(pActiveView.FocusMap) as IMapFrame; //根据MapSurround的uid,创建相应的MapSurroundFrame和MapSurround UID uid = new UIDClass(); uid.Value = "esriCarto.Legend"; IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uid, null); //设置图例的Title ILegend2 legend = mapSurroundFrame.MapSurround as ILegend2; legend.Title = "地图图例"; ILegendFormat format = new LegendFormatClass(); ITextSymbol symbol = new TextSymbolClass(); symbol.Size = 4; format.TitleSymbol = symbol; legend.Format = format; //QI,确定mapSurroundFrame的位置 IElement element = mapSurroundFrame as IElement; IEnvelope envelope = new EnvelopeClass(); double x = 3; double y = 5; envelope.PutCoords(x, y, x + 5, y + 5); element.Geometry = envelope; //使用IGraphicsContainer接口添加显示 container.AddElement(element, 0); pActiveView.Refresh(); }
/// <summary> /// 添加图例 /// </summary> /// <param name="layoutControl">布局视图</param> /// <param name="pEnv">矩形框</param> /// <param name="pLegend">图例</param> public static void AddLegend(AxPageLayoutControl layoutControl, IEnvelope pEnv, ILegend2 pLegend) { if (pLegend == null) { return; } 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; } IMapSurroundFrame pMapSurroundFrame = new MapSurroundFrameClass(); pMapSurroundFrame.MapFrame = pMapFrame; //如果已经存在图例,删除已经存在的图例 MappingHelper.DeleteElementByName(layoutControl, "Legend"); pMapSurroundFrame.MapSurround = pLegend as IMapSurround; //添加图例 IElement pElement = pMapSurroundFrame as IElement; pElement.Geometry = pEnv; //添加元素 MappingHelper.AddElementWithName(pGraphicsContainer, pElement, "Legend"); }
//点击地图框事件 private void axPageLayoutControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IPageLayoutControlEvents_OnMouseDownEvent e) { if (e.button == 1) { IPageLayout pPageLayout = axPageLayoutControl1.PageLayout;; IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer; IActiveView pActiveView = pPageLayout as IActiveView; IMapFrame pMapFrame; IMapSurroundFrame pMapSurroundFrame; IElement pElement; switch (tag) { #region 添加标题 case 1: ITextElement pTextElement = new TextElementClass(); pTextElement.Text = title; pTextElement.Symbol = pTextSymbol; pElement = pTextElement as IElement; try { pElement.Geometry = axPageLayoutControl1.TrackRectangle(); } catch { MessageBox.Show("请拉框选择范围!", "提示"); return; } pGraphicsContainer.AddElement(pElement, 0); axPageLayoutControl1.Refresh(); tag = 0; break; #endregion #region 添加图例 case 2: pLegend2 = new LegendClass_2(); pElement = axPageLayoutControl1.FindElementByName("legend"); if (pElement != null) { pGraphicsContainer.DeleteElement(pElement); } pElementTypeName = "lenend"; pUid = new UIDClass(); pUid.Value = "esriCato.Legend"; if (pGraphicsContainer == null) { return; } pMapSurround = pLegend2 as IMapSurround; pMapSurround.Map = pActiveView.FocusMap; pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame; if (pMapFrame == null) { return; } pMapSurroundFrame = new MapSurroundFrameClass(); pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null); pMapSurroundFrame.MapSurround = pMapSurround; pMapSurroundFrame.MapSurround.Name = pElementTypeName; pMapSurroundFrame.MapFrame = pMapFrame; pElement = pMapSurroundFrame as IElement; try { pElement.Geometry = axPageLayoutControl1.TrackRectangle(); } catch { MessageBox.Show("请拉框选择范围!", "提示"); return; } pElement.Activate(pActiveView.ScreenDisplay); pElement.Draw(pActiveView.ScreenDisplay, null); pGraphicsContainer.AddElement(pElement, 0); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); //图例标题字体样式 pLegend2.Title = "图例"; pTitltFont.Name = "黑体"; pTitltFont.Bold = true; pTitltFont.Size = 16; pTitltTextSymbol.Color = ColorToIColor(Color.Black); pTitltTextSymbol.Font = pTitltFont; pLenendFormat.TitleSymbol = pTitltTextSymbol; //图例图层名字体样式 pLayerFont.Name = "仿宋_GB2312"; pLayerFont.Size = 13; pLayerTextSymbol.Font = pLayerFont; //图例标签字体样式 pLabelFont.Name = "仿宋_GB2312"; pLabelFont.Size = 13; pLabelTextSymbol.Font = pLayerFont; for (int i = 0; i < pLenendItemsString.Count; i++) { for (int j = 0; j < pLegend2.ItemCount; j++) { if (pLenendItemsString[i].ToString() == pLegend2.get_Item(j).Layer.Name) { pLegend2.RemoveItem(j); } } } pLegend2.AdjustColumns(pColumnCount); pLegend2.Refresh(); tag = 0; break; #endregion #region 添加指北针 case 3: if (pGraphicsContainer == null) { return; } pElement = axPageLayoutControl1.FindElementByName("northarrow"); if (pElement != null) { pGraphicsContainer.DeleteElement(pElement); } pElementTypeName = "northarrow"; pUid = new UIDClass(); pUid.Value = "esriCato.MarkerNorthArrow"; pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame; pMapSurroundFrame = new MapSurroundFrameClass(); pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null); pMapSurroundFrame.MapSurround = pNorthArrowStyleGalleryItem.Item as IMapSurround; pElement = pMapSurroundFrame as IElement; try { pElement.Geometry = axPageLayoutControl1.TrackRectangle(); } catch { MessageBox.Show("请拉框选择范围!", "提示"); return; } pGraphicsContainer.AddElement(pElement, 0); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); tag = 0; break; #endregion #region 添加比例尺 case 4: if (pGraphicsContainer == null) { return; } pElement = axPageLayoutControl1.FindElementByName("scalebar"); if (pElement != null) { pGraphicsContainer.DeleteElement(pElement); } IScaleBar pScaleBar; pScaleBar = pScaleStyleGalleryItem.Item as IScaleBar; pScaleBar.Units = esriUnits.esriKilometers; pScaleBar.UnitLabel = " KM"; pElementTypeName = "scalebar"; pUid = new UIDClass(); pUid.Value = "esriCato.AlternatingScaleBar"; pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame; pMapSurroundFrame = new MapSurroundFrameClass(); pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null); pMapSurroundFrame.MapSurround = pScaleStyleGalleryItem.Item as IMapSurround; pElement = pMapSurroundFrame as IElement; try { pElement.Geometry = axPageLayoutControl1.TrackRectangle(); } catch { MessageBox.Show("请拉框选择范围!", "提示"); } pGraphicsContainer.AddElement(pElement, 0); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); tag = 0; break; #endregion } } }