ArcEngine注记Annotation&元素Element 新增和旋转实现

本文详细解析了一段用于创建地图标注工具的代码实现,包括如何使用HookHelper进行初始化,通过NewTextFeedback和RotateTextFeedback进行文字标注的创建与旋转反馈,以及如何在地图上添加并显示最终的文本元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不说,直接上代码

public class CreateAnnotationTool : BaseTool
    {
        private IHookHelper _hookHelper;
        public IActiveView ActiveView { get; set; }


        private IPoint curPoint;
        private ITextSymbol textSymbol;
        private INewTextFeedback newTextFeedback;
        private IRotateTextFeedback rotateTextFeedback;
        private bool isStartRotate = false;

        public override void OnCreate(object hook)
        {
            try
            {
                _hookHelper = new HookHelperClass();
                _hookHelper.Hook = hook;
                base.m_enabled = true;
            }
            catch
            {
                _hookHelper = null;
            }
        }

        public override void OnClick()
        {
            textSymbol = new TextSymbolClass();
            textSymbol.Text = "RRRRRRRRRRRRRR";
            (textSymbol as ISymbol).ROP2 = esriRasterOpCode.esriROPNotXOrPen;

            curPoint = new PointClass();
            curPoint.PutCoords(0, 0);

            base.OnClick();
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            IPoint point = ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            if (Button == 1)
            {
                if (!isStartRotate)
                {
                    if(newTextFeedback != null)
                    {
                        newTextFeedback.Stop();
                        newTextFeedback = null;
                    }
                    //停止后有残影,需要手动刷新
                    ActiveView.PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null);

                    rotateTextFeedback = new RotateTextFeedbackClass();
                    rotateTextFeedback.Display = ActiveView.ScreenDisplay;
                    rotateTextFeedback.Symbol = ((IClone)textSymbol).Clone() as ISymbol;
                    rotateTextFeedback.Start(point, 50000000);//第二个参数为注记的参考比例

                    isStartRotate = true;
                }
                else
                {
                    if (rotateTextFeedback != null)
                    {
                        double angle = rotateTextFeedback.Stop();
                        CreateElement(rotateTextFeedback.Anchor, angle);

                        rotateTextFeedback = null;
                        isStartRotate = false;
                    }
                }
            }
        }


        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            IPoint point = ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            if (!isStartRotate)
            {
                if (newTextFeedback != null)
                    newTextFeedback.MoveTo(point);
                else
                {
                    newTextFeedback = new NewTextFeedbackClass();
                    newTextFeedback.Display = ActiveView.ScreenDisplay;
                    newTextFeedback.Symbol = ((IClone)textSymbol).Clone() as ISymbol;
                    newTextFeedback.Start(point, 50000000);
                }
            }
            else
            {
                if (rotateTextFeedback != null)
                    rotateTextFeedback.MoveTo(point);
            }
        }

        private void CreateElement(IGeometry pGeometry, double angle)
        {
            ITextSymbol mrk = ((IClone)textSymbol).Clone() as ITextSymbol;
            mrk.Angle = angle;

            ITextElement pMarkerElement = new TextElementClass();
            pMarkerElement.Symbol = mrk;
            pMarkerElement.Text = mrk.Text;

            IElement pElement = pMarkerElement as IElement;
            pElement.Geometry = pGeometry;

            ActiveView.GraphicsContainer.AddElement(pElement, 0);

            IEnvelope pEnvelope = new EnvelopeClass();
            pElement.QueryBounds(ActiveView.ScreenDisplay, pEnvelope);
            ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pEnvelope);
        }


        public override void Refresh(int hDC)
        {
            if (newTextFeedback != null)
            {
                newTextFeedback.Refresh(hDC);
            }
            if (rotateTextFeedback != null)
            {
                rotateTextFeedback.Refresh(hDC);
            }
            base.Refresh(hDC);
        }
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值