AE二次开发使用TextElement绘制标注

            IFeatureCursor pFeatCursor;
            IFeatureClass pfeatClass;
            IFeatureLayer pfeatlayer;
            IFeature pfeature;
            IEnvelope pEnv;
            IRgbColor pColor;
            stdole.Font pFont;
            IPoint pt;
            IElement pEle;
            ITextElement pTextEle;
            IGraphicsContainer pGraphisContainer;
            ITextSymbol pTextSymbol;
            IActiveView pActiveView;
            pfeatlayer = (IFeatureLayer)axMapControl1.get_Layer(0);
            pfeatClass = pfeatlayer.FeatureClass;
            pFeatCursor = pfeatClass.Search(null, false);
            pfeature = pFeatCursor.NextFeature();
            int fieldIdx;
            int i;
            //得到cname字段的Index
            fieldIdx = pfeature.Fields.FindField("CNTRY_NAME");
            //创建颜色
            pColor = new RgbColorClass();
            pColor.Blue = 255;
            //创建字体
            pFont = new stdole.StdFontClass();
            pFont.Name = "楷体";
            //创建标注样式
            pTextSymbol = new TextSymbolClass();
            pTextSymbol.Color = pColor;
            pTextSymbol.Font = pFont as stdole.IFontDisp;
            pTextSymbol.Size = 10;
            pGraphisContainer = axMapControl1.Map as IGraphicsContainer;
            pActiveView = axMapControl1.Map as IActiveView;
            //使用循环标注每个要素
            while (pfeature != null)
            {
                pEnv = pfeature.Extent;
                pt = new PointClass();
                //使用地理对象的包络线中心作为标注位置
                pt.PutCoords(pEnv.XMin + pEnv.Width * 0.5, pEnv.YMin + pEnv.Height * 0.5);
                //创建文本标注
                pTextEle = new TextElementClass();
                pTextEle.Symbol = pTextSymbol;
                pTextEle.Text = pfeature.get_Value(fieldIdx).ToString();
                //设置标注位置
                pEle = pTextEle as IElement;
                pEle.Geometry = pt;
                //绘制标注
                pGraphisContainer.AddElement(pEle, 0);
                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pEnv);
                pt = null;
                pTextEle = null;
                pfeature = pFeatCursor.NextFeature();
            }
string CurrentPath = Directory.GetCurrentDirectory();
         Directory.SetCurrentDirectory(CurrentPath + "\\..\\..\\..\\..\\..\\data\\WORLD");
         
         string FilePath = Directory.GetCurrentDirectory();
         axMapControl1.AddShapeFile(FilePath, "country.shp");
         axMapControl1.AddShapeFile(FilePath, "rivers.shp");
         axMapControl1.AddShapeFile(FilePath, "cities.shp");
         IMap pMap;
         int i;
         pMap = axMapControl1.Map;
         for (i = 0; i <= pMap.LayerCount - 1; i++)
         {
             axMapControl2.Map.AddLayer(pMap.get_Layer(i));
         }
        }

        private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            if (e.button == 1)
            { axMapControl1.Extent = axMapControl1.TrackRectangle(); }
            else if (e.button == 2)
            { axMapControl1.Pan(); }
        }

        private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            IEnvelope pEnv;
            pEnv =(IEnvelope)e.newEnvelope;
            IGraphicsContainer pGraphicsContainer;
            IActiveView pActiveView;
            pGraphicsContainer = axMapControl2.Map as IGraphicsContainer;
            pActiveView = pGraphicsContainer as IActiveView;
            //在绘制新的矩形框前,清除Map对象中的所有图形元素
            pGraphicsContainer.DeleteAllElements();
            //定义一个矩形元素对象
            IRectangleElement pRectangelEle;
            pRectangelEle = new RectangleElementClass();
            IElement pEle;
            pEle = pRectangelEle as IElement;
            pEle.Geometry = pEnv;
            IRgbColor pColor;
            pColor = new RgbColorClass();
            pColor.Red = 255;
            pColor.Transparency = 255;
            //产生一个线符号对象
            ILineSymbol pOutline;
            pOutline = new SimpleLineSymbolClass();
            //设置线符号的属性
            pOutline.Color = pColor;
            pOutline.Width = 2;
            //设置颜色属性
            pColor = new RgbColorClass();
            pColor.Transparency = 0;
            //设置填充符号的属性
            IFillSymbol pFillSymbol;
            pFillSymbol = new SimpleFillSymbolClass();
            pFillSymbol.Color = pColor;
            pFillSymbol.Outline = pOutline;
            IFillShapeElement pFillshapeEle;
            pFillshapeEle = pEle as IFillShapeElement;
            pFillshapeEle.Symbol = pFillSymbol;
            pEle = pFillshapeEle as IElement;
            pGraphicsContainer.AddElement(pEle,0);
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }

        private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            IPoint point = new PointClass();
            point.PutCoords(e.mapX, e.mapY);
            axMapControl1.CenterAt(point);
        }
    }

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
下面是实现两点距离测量的步骤: 1. 在 ArcGIS Engine 项目中添加 MuneStirp 控件,该控件用于显示测量结果。 2. 在菜单栏中添加“测量距离”按钮,单击该按钮后,进入测量模式。 3. 在地图上单击两个点,计算它们之间的距离,并将结果显示在 MuneStirp 控件中。 下面是具体实现方法: 1. 添加 MuneStirp 控件 在 Visual Studio 中打开 ArcGIS Engine 项目,右键单击工具箱中的“常规”选项卡,选择“选择项”,在“COM 组件”选项卡中选中“MapControl Tools 1.0 Type Library”和“MapControl 1.0 Type Library”,单击“确定”按钮,将 MuneStirp 控件添加到工具箱中。 在窗体设计器中,将 MuneStirp 控件拖放到窗体上,调整其大小和位置。 2. 添加测量按钮 在窗体设计器中,右键单击菜单栏,选择“添加新项”,创建一个名为“测量距离”的按钮。 在按钮的 Click 事件中,添加以下代码: ```csharp private void btnMeasureDistance_Click(object sender, EventArgs e) { // 进入测量模式 axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; axMapControl1.MouseDownEvent += new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent); } ``` 3. 实现测量距离功能 在 axMapControl1_MouseDownEvent 事件中,获取鼠标单击的坐标,并计算两点之间的距离。将距离显示在 MuneStirp 控件中。 ```csharp private void axMapControl1_MouseDownEvent(object sender, IMapControlEvents2_OnMouseDownEvent e) { if (e.button == 1) // 左键单击 { if (axMapControl1.CurrentTool == null) // 如果不在测量模式中 { // 进入测量模式 axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; double x1 = e.mapX; double y1 = e.mapY; IPoint point1 = new PointClass(); point1.PutCoords(x1, y1); IGraphicsContainer graphicsContainer = axMapControl1.ActiveView as IGraphicsContainer; IElement element1 = GetPointElement(point1); graphicsContainer.AddElement(element1, 0); axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null); axMapControl1.MouseDownEvent -= new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent); axMapControl1.MouseDownEvent += new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent2); } } } private void axMapControl1_MouseDownEvent2(object sender, IMapControlEvents2_OnMouseDownEvent e) { if (e.button == 1) // 左键单击 { double x2 = e.mapX; double y2 = e.mapY; IPoint point2 = new PointClass(); point2.PutCoords(x2, y2); IGraphicsContainer graphicsContainer = axMapControl1.ActiveView as IGraphicsContainer; IElement element2 = GetPointElement(point2); graphicsContainer.AddElement(element2, 0); axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null); // 计算距离 double distance = GetDistance(point1, point2); IFormattedTextSymbol symbol = new TextSymbolClass(); symbol.Background = GetColor(255, 255, 255); symbol.Border = true; symbol.Color = GetColor(0, 0, 0); symbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter; symbol.VerticalAlignment = esriTextVerticalAlignment.esriTVABottom; symbol.Angle = GetAngle(point1, point2); ITextElement textElement = new TextElementClass(); textElement.Symbol = symbol; textElement.Text = string.Format("{0:0.00} 米", distance); textElement.ScaleText = true; IElement element3 = textElement as IElement; element3.Geometry = GetMidPoint(point1, point2); graphicsContainer.AddElement(element3, 0); axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null); // 显示结果 muneStrip.Items.Clear(); muneStrip.Items.Add(string.Format("距离:{0:0.00} 米", distance)); muneStrip.Visible = true; // 退出测量模式 axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; axMapControl1.MouseDownEvent -= new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent2); axMapControl1.MouseDownEvent += new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent); } } private IElement GetPointElement(IPoint point) { ISimpleMarkerSymbol symbol = new SimpleMarkerSymbolClass(); symbol.Style = esriSimpleMarkerStyle.esriSMSCircle; symbol.Color = GetColor(255, 0, 0); symbol.Size = 6; ISimpleMarkerSymbol symbol2 = new SimpleMarkerSymbolClass(); symbol2.Style = esriSimpleMarkerStyle.esriSMSCircle; symbol2.Color = GetColor(255, 255, 255); symbol2.Size = 4; IMultiLayerMarkerSymbol multiLayerMarkerSymbol = new MultiLayerMarkerSymbolClass(); multiLayerMarkerSymbol.AddLayer(symbol); multiLayerMarkerSymbol.AddLayer(symbol2); IMarkerElement markerElement = new MarkerElementClass(); markerElement.Symbol = multiLayerMarkerSymbol as IMarkerSymbol; IElement element = markerElement as IElement; element.Geometry = point; return element; } private double GetDistance(IPoint point1, IPoint point2) { IProximityOperator proximityOperator = point1 as IProximityOperator; return proximityOperator.ReturnDistance(point2); } private double GetAngle(IPoint point1, IPoint point2) { double dx = point2.X - point1.X; double dy = point2.Y - point1.Y; double angle = Math.Atan2(dy, dx) * 180 / Math.PI; return angle; } private IPoint GetMidPoint(IPoint point1, IPoint point2) { IPoint midPoint = new PointClass(); midPoint.X = (point1.X + point2.X) / 2; midPoint.Y = (point1.Y + point2.Y) / 2; return midPoint; } private IColor GetColor(int red, int green, int blue) { IRgbColor color = new RgbColorClass(); color.Red = red; color.Green = green; color.Blue = blue; return color; } ``` 以上代码中,GetPointElement 方法用于创建点元素;GetDistance 方法用于计算两点之间的距离;GetAngle 方法用于计算两点之间的夹角;GetMidPoint 方法用于计算两点之间的中点;GetColor 方法用于创建颜色对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值