C# ArcEngine开发 之地图整饰篇

ArcEngine 二次开发 专栏收录该内容
3 篇文章 0 订阅

C# ArcEngine开发 之地图整饰篇

呼,有一阵子没写博客(daun zi)了,这段时间忙于一个AE开发的项目,魔鬼产品催得紧啊,

完全没有时间,终于告一段落了, 总结一下,跟大家做一个分享。
今天来分享地图整饰。
话不多说,进入正题

地图漫游

在这里插入图片描述

首先分享对地图漫游的相关系列操作,包括放大缩小、全图展示、平移、编辑等。
相关代码如下:

		//Zoom in
        private void btnFixedZoomIn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ControlsPageZoomInFixedCommand zoominfixed = new ControlsPageZoomInFixedCommand();
            zoominfixed.OnCreate(axPageLayoutControl1.Object);
            zoominfixed.OnClick();
        }
		//Zoom out
        private void FixedZoomOut_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ControlsPageZoomOutFixedCommand zoomoutfixed = new ControlsPageZoomOutFixedCommand();
            zoomoutfixed.OnCreate(axPageLayoutControl1.Object);
            zoomoutfixed.OnClick();
        }
		//全图展示
        private void btnZoom2WholePage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ControlsPageZoomWholePageCommand wholeTool = new ControlsPageZoomWholePageCommand();
            wholeTool.OnCreate(axPageLayoutControl1.Object);
            wholeTool.OnClick();
        }
		//平移地图
        private void btnLayoutPan_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ControlsPagePanTool PanTool = new ControlsPagePanToolClass();
            PanTool.OnCreate(axPageLayoutControl1.Object);
            axPageLayoutControl1.CurrentTool = PanTool as ITool;
        }
		//鼠标切换为编辑模式
        private void btneditmouse_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ControlsSelectTool Cmd = new ControlsSelectTool();
            Cmd.OnCreate(this.axPageLayoutControl1.Object);
            this.axPageLayoutControl1.CurrentTool = Cmd as ITool;
        }

地图要素

一张完整的地图当然不能缺少地图要素(当时上学的时候也因为丢三落四没少被老师骂),所以!图例、指北针、比例尺、标题、文字、格网,代码都在下面了,老铁子们按需取货吧,走你。
在这里插入图片描述

 //图例
        private void btn_legend_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmLegend frmLegend = new FrmLegend(axPageLayoutControl1);
            frmLegend.OnQueryLenendItems += pLegend => m_Legend = pLegend;
            frmLegend.OnQueryColumnCount += pColumnCount => m_ColumnCount = pColumnCount;
            //窗体位置在屏幕中间
            frmLegend.StartPosition = FormStartPosition.CenterScreen;
            frmLegend.ShowDialog();

            axPageLayoutControl1.CurrentTool = null;
            axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
            operation = "添加图例";
        }
        //指北针
        private void btn_compass_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmNorthArrow frmNorthArrow = new FrmNorthArrow();
            frmNorthArrow.OnQueryNorthArrow += pNorthArrow => m_NorthArrrow = pNorthArrow;
            //窗体位置在屏幕中间
            frmNorthArrow.StartPosition = FormStartPosition.CenterScreen;
            frmNorthArrow.ShowDialog();

            axPageLayoutControl1.CurrentTool = null;
            axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
            operation = "添加指北针";
        }
        //比例尺
        private void btn_scale_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmScaleBar frmScaleBar = new FrmScaleBar();
            frmScaleBar.OnQueryScaleBar += pScaleBar => m_ScaleBar = pScaleBar;
            //窗体位置在屏幕中间
            frmScaleBar.StartPosition = FormStartPosition.CenterScreen;
            frmScaleBar.ShowDialog();

            axPageLayoutControl1.CurrentTool = null;
            axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
            operation = "添加比例尺";
        }
        //标题
        private void btn_title_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmTitle frmTitle = new FrmTitle();
            frmTitle.OnQueryTextSymbol += mTextSymbol => m_TextSymbol = mTextSymbol;

            //窗体位置在屏幕中间
            frmTitle.StartPosition = FormStartPosition.CenterScreen;
            //窗体在其父窗口中间
            //frmTitle.StartPosition = FormStartPosition.CenterParent;
            frmTitle.ShowDialog();

            axPageLayoutControl1.CurrentTool = null;
            axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
            operation = "添加标题";
        }

        //添加文字
        private void btn_txt_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmTxt frmTxt = new FrmTxt();
            frmTxt.OnQueryTextSymbol += mTextSymbol => m_TextSymbol = mTextSymbol;

            //窗体位置在屏幕中间
            frmTxt.StartPosition = FormStartPosition.CenterScreen;
            //窗体在其父窗口中间
            //frmTitle.StartPosition = FormStartPosition.CenterParent;
            frmTxt.ShowDialog();

            axPageLayoutControl1.CurrentTool = null;
            axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
            operation = "添加文字";
        }

        private void axPageLayoutControl1_OnMouseDown(object sender, IPageLayoutControlEvents_OnMouseDownEvent e)
        {
            switch (operation)
            {
                case "添加文字":
                    {
                        IEnvelope pEnvelope = axPageLayoutControl1.TrackRectangle();
                        if (pEnvelope.IsEmpty || pEnvelope == null || pEnvelope.Width == 0 || pEnvelope.Height == 0)
                        {
                            return;
                        }
                        //找到PageLayout
                        IActiveView pActiveViewv = axPageLayoutControl1.PageLayout as IActiveView;
                        //找到元素容器
                        IGraphicsContainer pGraphicsContainer = pActiveViewv.GraphicsContainer;
                        //创建元素
                        ITextElement pTextElement = new TextElementClass();
                        pTextElement.Text = m_TextSymbol.Text;
                        pTextElement.Symbol = m_TextSymbol;
                        //设置位置                        
                        IElement pElement = pTextElement as IElement;
                        // 添加title
                        m_Element = pElement;
                        m_Element.Geometry = pEnvelope;
                        pGraphicsContainer.AddElement(m_Element, 0);
                        pActiveViewv.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                    }
                    break;
                case "添加标题":
                    {
                        IEnvelope pEnvelope = axPageLayoutControl1.TrackRectangle();
                        if (pEnvelope.IsEmpty || pEnvelope == null || pEnvelope.Width == 0 || pEnvelope.Height == 0)
                        {
                            return;
                        }
                        //找到PageLayout
                        IActiveView pActiveViewv = axPageLayoutControl1.PageLayout as IActiveView;
                        //找到元素容器
                        IGraphicsContainer pGraphicsContainer = pActiveViewv.GraphicsContainer;
                        //创建元素
                        ITextElement pTextElement = new TextElementClass();
                        pTextElement.Text = m_TextSymbol.Text;
                        pTextElement.Symbol = m_TextSymbol;
                        //设置位置                        
                        IElement pElement = pTextElement as IElement;
                        // 添加title
                        m_Element = pElement;
                        m_Element.Geometry = pEnvelope;
                        pGraphicsContainer.AddElement(m_Element, 0);
                        pActiveViewv.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                    }
                    break;
                case "添加指北针":
                    {
                        IEnvelope pEnvelope = axPageLayoutControl1.TrackRectangle();
                        if (pEnvelope.IsEmpty || pEnvelope == null || pEnvelope.Width == 0 || pEnvelope.Height == 0)
                        {
                            return;
                        }

                        // 删除已有指北针
                        IActiveView pActiveViewv = axPageLayoutControl1.PageLayout as IActiveView;
                        IGraphicsContainer pGraphicsContainer = pActiveViewv.GraphicsContainer;


                        // 获取框架元素
                        IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveViewv.FocusMap) as IMapFrame;
                        IMapSurroundFrame pMapSurroundFrame = new MapSurroundFrame() as IMapSurroundFrame;
                        pMapSurroundFrame.MapFrame = pMapFrame;
                        pMapSurroundFrame.MapSurround = m_NorthArrrow as IMapSurround;

                        // 添加指北针
                        m_Element = pMapSurroundFrame as IElement;
                        m_Element.Geometry = pEnvelope;
                        pGraphicsContainer.AddElement(m_Element, 0);
                        pActiveViewv.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

                    }
                    break;
                case "添加比例尺":
                    {
                        axPageLayoutControl1.ActiveView.FocusMap.MapUnits = esriUnits.esriMeters;

                        IEnvelope pEnvelope = axPageLayoutControl1.TrackRectangle();
                        if (pEnvelope.IsEmpty || pEnvelope == null || pEnvelope.Width == 0 || pEnvelope.Height == 0)
                        {
                            return;
                        }

                        // 删除已有比例尺
                        IActiveView pActiveViewv = axPageLayoutControl1.PageLayout as IActiveView;
                        IGraphicsContainer pGraphicsContainer = pActiveViewv.GraphicsContainer;

                        // 获取框架元素
                        IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveViewv.FocusMap) as IMapFrame;
                        IMapSurroundFrame pMapSurroundFrame = new MapSurroundFrame() as IMapSurroundFrame;
                        pMapSurroundFrame.MapFrame = pMapFrame;

                        //设置比例尺属性
                        m_ScaleBar.UseMapSettings();
                        m_ScaleBar.Units = esriUnits.esriKilometers;
                        m_ScaleBar.Divisions = 4;
                        m_ScaleBar.Subdivisions = 3;
                        m_ScaleBar.DivisionsBeforeZero = 0;
                        m_ScaleBar.UnitLabel = "km";
                        //m_ScaleBar.LabelGap = 3.6;
                        m_ScaleBar.LabelFrequency = esriScaleBarFrequency.esriScaleBarDivisionsAndFirstMidpoint;
                        pMapSurroundFrame.MapSurround = m_ScaleBar as IMapSurround;

                        // 添加比例尺
                        m_Element = pMapSurroundFrame as IElement;
                        m_Element.Geometry = pEnvelope;
                        pGraphicsContainer.AddElement(m_Element, 0);
                        pActiveViewv.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                    }
                    break;
                case "添加图例":
                    {
                        //图例样式
                        ILegendFormat pLenendFormat = new LegendFormat();

                        ITextSymbol pTitltTextSymbol = new TextSymbolClass();
                        IFontDisp pTitltFont = new StdFont() as IFontDisp;
                        //图例图层名样式
                        ITextSymbol pLayerTextSymbol = new TextSymbolClass();
                        IFontDisp pLayerFont = new StdFont() as IFontDisp;
                        //图例标签样式
                        ITextSymbol pLabelTextSymbol = new TextSymbolClass();
                        IFontDisp pLabelFont = new StdFont() as IFontDisp;

                        IEnvelope pEnvelope = axPageLayoutControl1.TrackRectangle();
                        if (pEnvelope.IsEmpty || pEnvelope == null || pEnvelope.Width == 0 || pEnvelope.Height == 0)
                        {
                            return;
                        }

                        IActiveView pActiveViewv = axPageLayoutControl1.PageLayout as IActiveView;
                        IGraphicsContainer pGraphicsContainer = pActiveViewv.GraphicsContainer;

                        ILegend2 pLegend = new LegendClass_2();
                        UID pUid = new UIDClass();
                        pUid.Value = "esriCato.Legend";
                        if (pGraphicsContainer == null) return;


                        //图例标题字体样式
                        pLegend.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;


                        // 获取框架元素
                        IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveViewv.FocusMap) as IMapFrame;
                        IMapSurroundFrame pMapSurroundFrame = new MapSurroundFrame() as IMapSurroundFrame;
                        IMapSurround mapSurround = pMapSurroundFrame.MapSurround;
                        mapSurround = pLegend as IMapSurround;
                        mapSurround.Map = pActiveViewv.FocusMap;
                        pMapFrame = pGraphicsContainer.FindFrame(pActiveViewv.FocusMap) as IMapFrame;
                        if (pMapFrame == null) return;
                        pMapSurroundFrame.MapFrame = pMapFrame;
                        pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null);
                        pMapSurroundFrame.MapSurround = mapSurround;

                        for (int i = 0; i < m_Legend.Count; i++)
                        {
                            for (int j = 0; j < pLegend.ItemCount; j++)
                            {
                                if (m_Legend[i].ToString() == pLegend.get_Item(j).Layer.Name)
                                    pLegend.RemoveItem(j);
                            }
                        }

                        pLegend.AdjustColumns(m_ColumnCount);
                        pLegend.Refresh();

                        m_Element = pMapSurroundFrame as IElement;
                        m_Element.Geometry = pEnvelope;
                        m_Element.Activate(pActiveViewv.ScreenDisplay);
                        m_Element.Draw(pActiveViewv.ScreenDisplay, null);

                        pGraphicsContainer.AddElement(m_Element, 0);
                        pActiveViewv.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                    }
                    break;
            }
            operation = "";
            m_Element = null;
            axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;


        }

        #region 格网
        private void btnGraticule_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                FrmGrid.DeleteMapGrid(axPageLayoutControl1);
                FrmGrid.CreateGraticuleMapGrid(axPageLayoutControl1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnMeasuredGrid_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmGrid.DeleteMapGrid(axPageLayoutControl1);//删除已存在格网
            FrmGrid.CreateMeasuredGrid(axPageLayoutControl1);
        }

        private void btnIndexGrid_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmGrid.DeleteMapGrid(axPageLayoutControl1);
            FrmGrid.CreateIndexGrid(axPageLayoutControl1);
        }

        private void btnclearGrid_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmGrid.DeleteMapGrid(axPageLayoutControl1);
        }
        #endregion

打印输出

地图整饰完成之后,就应该输出出来拿去跟甲方结账了!所以打印和另存的代码也不能少啊!走起来吧!
在这里插入图片描述

		//打印输出
        private void btn_print_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmPrintPreview = new FrmPrintPreview(axPageLayoutControl1);
            //窗体位置在屏幕中间
            frmPrintPreview.StartPosition = FormStartPosition.CenterScreen;
            frmPrintPreview.ShowDialog();
        }

        private void btn_output_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ExportMap.ExportMapToImage(axPageLayoutControl1);
        }

嗨呀,大功告成,准备关机。。。。
欸我去?咋又来个电话

“欸!小王啊,干哈呢?”
我:“啊,哥,我没事,我溜达”
“别溜达了,这有个新需求啊!做完了哥给你拿华子,咱们想做一个专题图的模块,既能导入模板,也能自定义模板另存,这样就能一键生成这些地图要素,你加加班,好不好”
我:“喂。哥,我这信号不好啊,我在塔克拉玛干呢,怕加不了啊,先撂了奥!有事儿电话!”
“能加钱。。。”
啊,那能干,行了伙伴们,我去做专题图了,做完了再给你们分享嚎~

结语

本次对于AE 地图整饰的开发的分享就到此结束啦,希望能对大家有所帮助。最后,欢迎各位关注以下公众号,我们会发布各种技术交流帖子,并提供相关文档和程序的下载。
在这里插入图片描述

  • 0
    点赞
  • 3
    评论
  • 5
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页

打赏作者

Rqmadlife

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值