1. 程式人生 > >ArcEngine註記Annotation&元素Element 新增和旋轉實現

ArcEngine註記Annotation&元素Element 新增和旋轉實現

廢話不說,直接上程式碼

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);
        }
    }