⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shitu.cs

📁 基于arcgisengine和c#写的添加地图图例的源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS.Analyst3D;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geodatabase;

namespace AddLegend
{
    public partial class shitu : Form
    {
        private IElement preLegendElement ;
        IGraphicsContainer pNorthArrowGraphicsContainer;
        IMap pMap;
        public shitu(AxMapControl pMap1)
        {
            InitializeComponent();
            pNorthArrowGraphicsContainer = axPageLayoutControl1.PageLayout as IGraphicsContainer;
            if (pMap1 == null)
            {
                return;
            }
            pMap = pMap1.Map;
            CopyAndOverwriteMap(pMap1.Map);

        }
        private void CopyAndOverwriteMap(IMap paraMap)
        {
            IObjectCopy objectCopy = new ObjectCopyClass();
            object toCopyMap = paraMap;//被复制的Arcmap
            object copiedMap = objectCopy.Copy(toCopyMap);
            object toOverwriteMap = axPageLayoutControl1.ActiveView.FocusMap;
            //object toOverwriteMap = axPageLayoutControl1.ActiveView.FocusMap;
            objectCopy.Overwrite(copiedMap, ref toOverwriteMap);
        }

        private void 添加图例ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddLegend();

        }

        
        //获得图层名

        private string GetLayerName()
        {
            IActiveView pActiveView = axPageLayoutControl1.ActiveView;
            string[] layerName;

            
        }

        private void AddLegend()
        {
            //Get the GraphicsContainer
            
            IGraphicsContainer pLegendGraphicsContainer = axPageLayoutControl1.GraphicsContainer;
            if (preLegendElement != null)
            {
                pLegendGraphicsContainer.DeleteElement(preLegendElement);
            }
            //Get the MapFrame
            //IMapFrame mapFrame = (IMapFrame)pLegendGraphicsContainer.FindFrame(axPageLayoutControl1.ActiveView.FocusMap);
            IMapFrame mapFrame = (IMapFrame)pLegendGraphicsContainer.FindFrame(axPageLayoutControl1.ActiveView.FocusMap);
            if (mapFrame == null) return;

            //Create a legend
            UID uID = new UIDClass();
            uID.Value = "esriCarto.Legend";


            //Create a MapSurroundFrame from the MapFrame
            IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uID, null);

            if (mapSurroundFrame == null) return;
            if (mapSurroundFrame.MapSurround == null) return;
            //Set the name 
            mapSurroundFrame.MapSurround.Name = "Legend";

            //Envelope for the legend
            IEnvelope envelopeLegend = new EnvelopeClass();
            envelopeLegend.PutCoords(1, 1, 10, 10);

            //Set the geometry of the MapSurroundFrame 
            IElement pLegendElement = (IElement)mapSurroundFrame;
            pLegendElement.Geometry = envelopeLegend;

            //Add the legend to the PageLayout
            //axPageLayoutControl1.AddElement(pLegendElement, Type.Missing, Type.Missing, "Legend", 0);
            axPageLayoutControl1.AddElement(pLegendElement, Type.Missing, Type.Missing, "Legend", 0);

            preLegendElement = (IElement)mapSurroundFrame;
            //更改比例尺标题名称
            IMapSurroundFrame modifySurroundFrame = (IMapSurroundFrame)preLegendElement;
            ILegend pLegend = (ILegend)modifySurroundFrame.MapSurround;
            pLegend.Name = "Legend";
            pLegend.Title = "图例";

            //Refresh the PageLayoutControl
            //axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }

        private void 删除图例ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (preLegendElement != null)
            {
                pNorthArrowGraphicsContainer.DeleteElement(preLegendElement);
                preLegendElement = null;
                axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            }
            else
            {
                MessageBox.Show("该视图上不存在图例,无法完成删除1");
                return;
            }
        }

        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -