//开始加载 private void FrmLegend_Load(object sender, EventArgs e) { //第一界面需要加载内容 if (pMap.LayerCount > 0) { for (int i = 0; i < pMap.LayerCount; i++) { //将所有数据名称加载到第一个ListView中 this.LViewMapLayer.Items.Add(pMap.get_Layer(i).Name); //将所有显示的数据名称加载到第二个ListView中 if (pMap.get_Layer(i).Visible == true) { //第一界面中需要添加到图例的图层列表 this.LViewLegendLayer.Items.Add(pMap.get_Layer(i).Name); pSelectedidx.Add(i); } } } //第二界面需要加载内容 //加载字体名称 InstalledFontCollection pFontCollection = new InstalledFontCollection(); FontFamily[] pFontFamily = pFontCollection.Families; for (int i = 0; i < pFontFamily.Length; i++) { string pFontName = pFontFamily[i].Name; this.cboBoxTitleFontName.Items.Add(pFontName); } this.cboBoxTitleFontName.Text = "宋体"; //加载字体大小 for (int i = 3; i <= 100; i++) { this.cboBoxTitleFontSize.Items.Add(i.ToString()); } this.cboBoxTitleFontSize.Text = "10"; //颜色 btTitleColor.SelectedColor = Color.Black; //字体风格和位置 this.toolTitlePostion.Visible = true; this.toolTitleStyle.Visible = true; //第三界面需要加载内容 ILegendFormat pLegendFormat = new LegendFormatClass(); if (pLegendFormat.DefaultLinePatch != null) { FrmPatchLineAndArea frm = new FrmPatchLineAndArea(esriSymbologyStyleClass.esriStyleClassLinePatches); this.btLineShape.Image = frm.GetImageByGiveLineSymbolBeforeSelectItem(esriSymbologyStyleClass.esriStyleClassLinePatches, (ILinePatch)pLegendFormat.DefaultLinePatch, btLineShape.Width - 14, btLineShape.Height - 14); } if (pLegendFormat.DefaultAreaPatch != null) { FrmPatchLineAndArea frm = new FrmPatchLineAndArea(esriSymbologyStyleClass.esriStyleClassAreaPatches); this.btAreaShape.Image = frm.GetImageByGiveAreaSymbolBeforeSelectItem(esriSymbologyStyleClass.esriStyleClassAreaPatches, (IAreaPatch)pLegendFormat.DefaultAreaPatch, btAreaShape.Width - 14, btAreaShape.Height - 14); } //第四界面需要加载内容 this.LegendDistanceImage.Image = global::LibCerMap.Properties.Resources._1; }
// 图例 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(); }
//更新图例 private void UpdateLegend() { //边框、背景、阴影的间距 if (txtBorderGap.Text != null && pSymbolBorder != null) { pSymbolBorder.Gap = double.Parse(this.txtBorderGap.Text); } if (txtBackgroundGap.Text != null && pSymbolBackground != null) { pSymbolBackground.Gap = double.Parse(this.txtBackgroundGap.Text); } if (txtShadowX.Text != null && pSymbolShadow != null) { pSymbolShadow.VerticalSpacing = double.Parse(this.txtShadowX.Text); } if (txtShadowY.Text != null && pSymbolShadow != null) { pSymbolShadow.HorizontalSpacing = double.Parse(this.txtShadowY.Text); } //边框、背景、阴影的角度 if (txtBorderAngle.Text != null && pSymbolBorder != null) { pSymbolBorder.CornerRounding = short.Parse(txtBorderAngle.Text); } if (txtBackgroundAngle.Text != null && pSymbolBackground != null) { pSymbolBackground.CornerRounding = short.Parse(txtBackgroundAngle.Text); } if (txtShadowAngle.Text != null && pSymbolShadow != null) { pSymbolShadow.CornerRounding = short.Parse(txtShadowAngle.Text); } //边框、背景、阴影 IFrameProperties pFrameProperties = pMapSurroundFrame as IFrameProperties; if (pSymbolBorder != null) { pFrameProperties.Border = pSymbolBorder; } if (pSymbolBackground != null) { pFrameProperties.Background = pSymbolBackground; } if (pSymbolShadow != null) { pFrameProperties.Shadow = pSymbolShadow; } ILegendFormat pLegendFormat = new LegendFormatClass(); //标题设置 显示 if (this.cBoxShowTitle.Checked == true) { pLegendFormat.ShowTitle = true; } else { pLegendFormat.ShowTitle = false; } //标题样式 if (pTitleSymbol != null) { pLegendFormat.TitleSymbol = pTitleSymbol; } //图面样式以及高度和样式设计 if (pLinePatch != null) { pLegendFormat.DefaultLinePatch = pLinePatch; } if (pAreaPatch != null) { pLegendFormat.DefaultAreaPatch = pAreaPatch; } if (txtPatchWith.Text != null) { pLegendFormat.DefaultPatchWidth = double.Parse(txtPatchWith.Text); } if (txtPatchHeight.Text != null) { pLegendFormat.DefaultPatchHeight = double.Parse(txtPatchHeight.Text); } //第四界面从上到下图例各部分之间的距离 pLegendFormat.TitleGap = double.Parse(txtTitleAndLegend.Text); pLegendFormat.VerticalItemGap = double.Parse(txtLegendItems.Text); pLegendFormat.HorizontalItemGap = double.Parse(txtColumns.Text); pLegendFormat.HeadingGap = double.Parse(txtHeadings.Text); pLegendFormat.TextGap = double.Parse(txtLabelsAndDescription.Text); pLegendFormat.VerticalPatchGap = double.Parse(txtPatchesV.Text); pLegendFormat.HorizontalPatchGap = double.Parse(txtPatchAndLabels.Text); //样式添加到图例 Legend.Title = this.txtLegendTitle.Text; Legend.Format = pLegendFormat; //地图链接 Legend.AutoVisibility = cBoxAddVisibleLayer.Checked; //只显示显示的图层 Legend.AutoAdd = cBoxAddNewLayer.Checked; //自动添加显示的图层 Legend.AutoReorder = cBoxNewOrder.Checked; //按照图层顺序排列图例 //Legend.ClearItems(); //Legend.Refresh(); //图例列数 double pLegendColumn = double.Parse(this.LegendColumn.Text); //图例中要包含的图层个数为 pSelectedidx.Count double pLegendItemCount = pSelectedidx.Count; //计算图例分裂的位置 int ColumnPos = (int)(pLegendItemCount / pLegendColumn); double remainder = (pLegendItemCount % pLegendColumn) / pLegendColumn; if (remainder >= 0.5) { ColumnPos = ColumnPos + 1; } for (int i = 0; i < pSelectedidx.Count; i++) { ILegendItem pLegendItem = new HorizontalLegendItemClass(); pLegendItem.Layer = pMap.get_Layer(pSelectedidx[i]); int lengendinex = 0;//记录同名图层在legend中的位置 for (int j = 0; j < Legend.ItemCount; j++) { if (Legend.Item[j].Layer == pLegendItem.Layer) { lengendinex = j; break; } } pLegendItem.ShowDescriptions = Legend.Item[lengendinex].ShowDescriptions; pLegendItem.ShowHeading = Legend.Item[lengendinex].ShowHeading; pLegendItem.ShowLabels = Legend.Item[lengendinex].ShowLabels; pLegendItem.ShowLayerName = Legend.Item[lengendinex].ShowLayerName; pLegendItem.HeadingSymbol = Legend.Item[lengendinex].HeadingSymbol; pLegendItem.LayerNameSymbol = Legend.Item[lengendinex].LayerNameSymbol; pLegendItem.LegendClassFormat = Legend.Item[lengendinex].LegendClassFormat; //列数设置 //列数设置 if (ColumnPos <= 1) { pLegendItem.NewColumn = true; } else if (ColumnPos > 1 && i > 0) { int columnI = i % ColumnPos; if (columnI == 0) { pLegendItem.NewColumn = true; } } legendlist.Add(pLegendItem); } Legend.ClearItems(); Legend.Refresh(); for (int i = 0; i < legendlist.Count; i++) { Legend.AddItem(legendlist[i]); } ILegendLayout pLegendLayout = pLegendFormat as ILegendLayout; pLegendLayout.ScaleGraphicsOnResize = false; m_hookHelper.ActiveView.Refresh(); }
//创建图例 private void CreateLegend() { ILegendFormat pLegendFormat = new LegendFormatClass(); IMapSurroundFrame pMapSurroundFrame = new MapSurroundFrameClass(); pActiveView = m_hookHelper.ActiveView as IActiveView; pMap = pActiveView.FocusMap; //边框、背景、阴影的间距 if (txtLegendFrameGap.Text != null && pSymbolBorder != null) { pSymbolBorder.Gap = double.Parse(this.txtLegendFrameGap.Text); } if (txtLegendFrameGap.Text != null && pSymbolBackground != null) { pSymbolBackground.Gap = double.Parse(this.txtLegendFrameGap.Text); } if (txtLegendFrameGap.Text != null && pSymbolShadow != null) { pSymbolShadow.VerticalSpacing = double.Parse(this.txtLegendFrameGap.Text) + 10; pSymbolShadow.HorizontalSpacing = double.Parse(this.txtLegendFrameGap.Text) + 10; } //边框、背景、阴影的角度 if (txtLegendFrameAngle.Text != null && pSymbolBorder != null) { pSymbolBorder.CornerRounding = short.Parse(txtLegendFrameAngle.Text); } if (txtLegendFrameAngle.Text != null && pSymbolBackground != null) { pSymbolBackground.CornerRounding = short.Parse(txtLegendFrameAngle.Text); } if (txtLegendFrameAngle.Text != null && pSymbolShadow != null) { pSymbolShadow.CornerRounding = short.Parse(txtLegendFrameAngle.Text); } //边框、背景、阴影 IFrameProperties pFrameProperties = pMapSurroundFrame as IFrameProperties; if (pSymbolBorder != null) { pFrameProperties.Border = pSymbolBorder; } if (pSymbolBackground != null) { pFrameProperties.Background = pSymbolBackground; } if (pSymbolShadow != null) { pFrameProperties.Shadow = pSymbolShadow; } //显示标题 pLegendFormat.ShowTitle = true; //标题位置 pLegendFormat.TitlePosition = esriRectanglePosition.esriTopSide; //标题样式 pLegendFormat.TitleSymbol = CreateTitleSymbol(); //第四界面从上到下图例各部分之间的距离 pLegendFormat.TitleGap = double.Parse(txtTitleAndLegend.Text); pLegendFormat.VerticalItemGap = double.Parse(txtLegendItems.Text); pLegendFormat.HorizontalItemGap = double.Parse(txtColumns.Text); pLegendFormat.HeadingGap = double.Parse(txtHeadings.Text); pLegendFormat.TextGap = double.Parse(txtLabelsAndDescription.Text); pLegendFormat.VerticalPatchGap = double.Parse(txtPatchesV.Text); pLegendFormat.HorizontalPatchGap = double.Parse(txtPatchAndLabels.Text); pLegendFormat.LayerNameGap = 2; //图面的样式以及高度和宽度 if (pLinePatch != null) { pLegendFormat.DefaultLinePatch = pLinePatch; } if (pAreaPatch != null) { pLegendFormat.DefaultAreaPatch = pAreaPatch; } pLegendFormat.DefaultPatchHeight = double.Parse(this.txtPatchHeight.Text); pLegendFormat.DefaultPatchWidth = double.Parse(this.txtPatchWith.Text); //创建图例 ILegend pLegend = new LegendClass_2(); //图例名称 //pLegend.Name = this.txtLegendTitle.Text; pLegend.AutoVisibility = false; //显示显示的图层 pLegend.AutoAdd = false; //自动添加新数据到图例 pLegend.AutoReorder = true; //根据图层顺序自动排列 pLegend.Title = this.txtLegendTitle.Text; pLegend.Format = pLegendFormat; pLegend.Map = pMap; pLegend.ClearItems(); //pLegend.Refresh(); //图例列数 double pLegendColumn = double.Parse(this.LegendColumn.Text); //图例中要包含的图层个数为 pSelectedidx.Count double pLegendItemCount = pSelectedidx.Count; //计算图例分裂的位置 int ColumnPos = (int)(pLegendItemCount / pLegendColumn); double remainder = (pLegendItemCount % pLegendColumn) / pLegendColumn; if (remainder >= 0.5) { ColumnPos = ColumnPos + 1; } //设置添加到图例的图层 for (int i = 0; i < pSelectedidx.Count; i++) { ILegendItem pLegendItem = new HorizontalLegendItemClass(); ILayer pLayer = (ILayer)(pMap.get_Layer(pSelectedidx[i])); if (!pLayer.Valid) { continue; } pLegendItem.Layer = pLayer; //显示标注和描述 pLegendItem.ShowDescriptions = true; pLegendItem.ShowLabels = true; //标注和描述字体样式 ITextSymbol pLDTextSymbol = new TextSymbolClass(); IFontDisp pLDFont = new StdFontClass() as IFontDisp; IRgbColor pLDColor = new RgbColorClass(); pLDColor.Red = 0; pLDColor.Green = 0; pLDColor.Blue = 0; pLDTextSymbol.Color = pLDColor; pLDFont.Size = (decimal)8; pLDFont.Name = "宋体"; pLDTextSymbol.Font = pLDFont; ILegendClassFormat pLegendClassFormat = new LegendClassFormatClass(); pLegendClassFormat.LabelSymbol = pLDTextSymbol; pLegendClassFormat.DescriptionSymbol = pLDTextSymbol; pLegendItem.LegendClassFormat = pLegendClassFormat; //图层名称是否显示 if (pLegendItem.Layer is IFeatureLayer) { if (((IGeoFeatureLayer)pLegendItem.Layer).Renderer is ISimpleRenderer) { pLegendItem.ShowHeading = false; pLegendItem.ShowLayerName = false; } else { pLegendItem.ShowLayerName = true; pLegendItem.ShowHeading = true; //图层名称和头文件的样式 ITextSymbol pLHTextSymbol = new TextSymbolClass(); IFontDisp pLHFont = new StdFontClass() as IFontDisp; IRgbColor pLHColor = new RgbColorClass(); pLDColor.Red = 0; pLDColor.Green = 0; pLDColor.Blue = 0; pLHTextSymbol.Color = pLHColor; pLHFont.Name = "宋体"; pLHFont.Size = 10; pLHFont.Bold = true; pLHTextSymbol.Font = pLHFont; pLHTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft; pLegendItem.HeadingSymbol = pLHTextSymbol; pLegendItem.LayerNameSymbol = pLHTextSymbol; } } else if (pLegendItem.Layer is IRasterLayer) { pLegendItem.ShowLayerName = true; pLegendItem.ShowHeading = true; //图层名称和头文件的样式 ITextSymbol pLHTextSymbol = new TextSymbolClass(); IFontDisp pLHFont = new StdFontClass() as IFontDisp; IRgbColor pLHColor = new RgbColorClass(); pLDColor.Red = 0; pLDColor.Green = 0; pLDColor.Blue = 0; pLHTextSymbol.Color = pLHColor; pLHFont.Name = "宋体"; pLHFont.Size = 10; pLHFont.Bold = true; pLHTextSymbol.Font = pLHFont; pLHTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft; pLegendItem.HeadingSymbol = pLHTextSymbol; pLegendItem.LayerNameSymbol = pLHTextSymbol; } //列数设置 if (ColumnPos <= 1) { pLegendItem.NewColumn = true; } else if (ColumnPos > 1 && i > 0) { int columnI = i % ColumnPos; if (columnI == 0) { pLegendItem.NewColumn = true; } } ILegendLayout pLegendLayout = pLegendFormat as ILegendLayout; pLegendLayout.ScaleGraphicsOnResize = false; //将图层及样式设计加入图例中 pLegend.AddItem(pLegendItem); pLegend.Refresh(); } //将图例加载到PageLayout上 IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer; IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame; if (pMapFrame == null) { return; } pMapSurroundFrame.MapFrame = pMapFrame; pMapSurroundFrame.MapSurround = (IMapSurround)pLegend; IElement pElement = (IElement)pMapSurroundFrame; IPageLayout pPageLayout = (IPageLayout)pActiveView; IPage pPage = pPageLayout.Page; double pWidth = pPage.PrintableBounds.XMax / 10.0; double pHeigth = pPage.PrintableBounds.YMin / 10.0; IEnvelope pEnvelope = new EnvelopeClass(); pEnvelope.PutCoords(pWidth, pHeigth, pWidth + 6, pHeigth + 6); pElement.Geometry = (IGeometry)pEnvelope; pGraphicsContainer.AddElement((IElement)pMapSurroundFrame, 0); m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, pMapSurroundFrame, null); }