Ejemplo n.º 1
0
        //更新图例
        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();
        }
Ejemplo n.º 2
0
        //创建图例
        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);
        }