ArcEngine制作专题地图(上,标题、图例)(20190920)

1、需求

为地图添加地图的基本要素(图例、比例尺、指北针)及相关注记,并将地图导出为图片。

2、解决方案

(1)创建地图的基本要素主要用到IMapFrame接口中的CreateSurroundFrame方法;
(2)控制地图的基本要素主要用到IMapSurround接口;
(3)还有一个IMapSurroundFrame接口,这个是做什么的我也不知道怎么表达,代码中慢慢意会吧;
(4)通过一个标志变量来判断操作的类型,根据操作的类型来添加地图要素(文字、图例等)。

3、界面设计

(1)主界面,主要由PageLayout控件、TOC控件、ToolBar控件组成,这里只通过ToolBar控件添加一些基本的缩放、平移等功能。
主界面
(2)添加标题,在文本框中输入地图名字,选择文字的颜色和字体后点击应用,在地图上拉框选择放置位置。
添加标题界面
(3)添加图例界面,将需要添加至图例的图层由左侧添加至右侧,选择列数,点击应用后在地图中拉框选择放置位置。
添加图例界面

4、代码实现

地图图框点击事件,通过一个int类型的变量tag来判断要添加地图元素的类型。
(1)定义的全局变量:

 #region 定义通用变量
        int tag = 0;//记录操作的标签
        IMapSurround pMapSurround = null;
        UID pUid = null;
        string pElementTypeName = "";//记录创建元素的名称,图例?比例尺?...
        IMap pMap;
        #endregion

        #region 定义添加标题变量
        public string title = "";
        public ITextSymbol pTextSymbol = new TextSymbolClass();
        #endregion

        #region 定义添加图例变量
        public List<string> pLenendItemsString = new List<string>();
        ILegend2 pLegend2;
        public int pColumnCount = 1;
        //图例标题样式
        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;
        ILegendFormat pLenendFormat = new LegendFormat();
        #endregion

(2)点击地图图框事件

//点击地图框事件
        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
                }
            }
        }

5、代码下载

github: ArcEngine实现专题图制作

  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值