| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 | using PaintDotNet.Annotation.Enum;using PaintDotNet.Base.SettingModel;using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;using System.Globalization;using System.Runtime.Serialization;using System.Windows.Forms;namespace PaintDotNet.Annotation.Label{    /// <summary>    /// 标注->文本    /// </summary>    public class DrawTextString : DrawObject    {        private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationLine.cur"));        /// <summary>        /// 标注样式信息model        /// </summary>        public LabelStyleModel.Text labelTextModel;        /// <summary>        /// 辅助为第一次创建文本矩形对象赋值        /// </summary>        public bool isFirstClick = true;        public DrawTextString(ISurfaceBox surfaceBox) : base()        {            this.objectType = DrawClass.Label;            this.drawToolType = DrawToolType.DrawTextString;            Initialize();        }        public DrawTextString(ISurfaceBox surfaceBox, int x1, int y1, string textStr) : base()        {            this.objectType = DrawClass.Label;            this.drawToolType = DrawToolType.DrawTextString;            startPoint.X = x1;            startPoint.Y = y1;            textboxMessage = textStr;            labelTextModel = surfaceBox.GetLabelStyleModel().text;            Initialize();        }        public DrawTextString(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()        {            this.objectType = DrawClass.Label;            this.drawToolType = DrawToolType.DrawTextString;            this.ISurfaceBox = surfaceBox;            labelTextModel = (LabelStyleModel.Text)parentStyleModel;            startPoint = points[0];            rectangle.X = points[0].X;            rectangle.Y = points[0].Y;            rectangle.Width = Math.Abs(points[1].X - points[0].X);            rectangle.Height = Math.Abs(points[1].Y - points[0].Y);            textboxMessage = content.ToString();            isFirstClick = false;        }        /// <summary>        /// Clone this instance        /// </summary>        public override DrawObject Clone()        {            DrawTextString textString = new DrawTextString(ISurfaceBox);            textString.objectType = DrawClass.Label;            textString.drawToolType = DrawToolType.DrawTextString;            textString.ISurfaceBox = this.ISurfaceBox;            textString.startPoint = this.startPoint;            textString.textboxMessage = this.textboxMessage;            textString.rectangle = this.rectangle;            textString.isFirstClick = false;            textString.labelTextModel = this.labelTextModel;            FillDrawObjectFields(textString);            return textString;        }        public override DrawObject Clone(ISurfaceBox surfaceBox)        {            DrawTextString textString = new DrawTextString(surfaceBox);            textString.objectType = DrawClass.Label;            textString.drawToolType = DrawToolType.DrawTextString;            textString.ISurfaceBox = surfaceBox;            textString.startPoint = this.startPoint;            textString.textboxMessage = this.textboxMessage;            textString.rectangle = this.rectangle;            textString.isFirstClick = false;            textString.labelTextModel = this.labelTextModel;            FillDrawObjectFields(textString);            return textString;        }        public override void Draw(Graphics g)        {            g.SmoothingMode = SmoothingMode.AntiAlias;            Font textfont = new Font(this.labelTextModel.font, this.labelTextModel.fontSize);            Brush textbrush = new SolidBrush(Color.FromArgb(this.labelTextModel.textColor));            Color color = Color.FromArgb(this.labelTextModel.backgroundBoxColor);            Pen pen = new Pen(color, this.labelTextModel.lineWidth);            Color fillColor = Color.FromArgb(this.labelTextModel.backgroundColor);            SolidBrush fillBrush = new SolidBrush(fillColor);            if (isFirstClick)            {                Size fontSize = g.MeasureString(textboxMessage, textfont).ToSize();                rectangle.X = startPoint.X;                rectangle.Y = startPoint.Y;                rectangle.Width = fontSize.Width;                rectangle.Height = fontSize.Height;                isFirstClick = false;            }                        g.FillRectangle(fillBrush, rectangle);            g.DrawRectangle(pen, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);            g.DrawString(textboxMessage, textfont, textbrush, rectangle);            fillBrush.Dispose();            pen.Dispose();        }        public override int HandleCount        {            get            {                return 8;            }        }        protected void SetRectangle(float x, float y, float width, float height)        {            this.rectangle.X = x;            this.rectangle.Y = y;            this.rectangle.Width = width;            this.rectangle.Height = height;        }        /// <summary>        /// Get handle pointscroll by 1-based number        /// </summary>        /// <param name="handleNumber"></param>        /// <returns></returns>        public override PointF GetHandle(int handleNumber)        {            float x, y, xCenter, yCenter;            xCenter = this.rectangle.X + this.rectangle.Width / 2;            yCenter = this.rectangle.Y + this.rectangle.Height / 2;            x = this.rectangle.X;            y = this.rectangle.Y;            switch (handleNumber)            {                case 1:                    x = this.rectangle.X;                    y = this.rectangle.Y;                    break;                case 2:                    x = xCenter;                    y = this.rectangle.Y;                    break;                case 3:                    x = this.rectangle.Right;                    y = this.rectangle.Y;                    break;                case 4:                    x = this.rectangle.Right;                    y = yCenter;                    break;                case 5:                    x = this.rectangle.Right;                    y = this.rectangle.Bottom;                    break;                case 6:                    x = xCenter;                    y = this.rectangle.Bottom;                    break;                case 7:                    x = this.rectangle.X;                    y = this.rectangle.Bottom;                    break;                case 8:                    x = this.rectangle.X;                    y = yCenter;                    break;            }            return new PointF(x, y);        }        /// <summary>        /// Hit test.        /// Return value: -1 - no hit        ///               0 - hit anywhere        ///               > 1 - handle number        /// </summary>        /// <param name="pointscroll"></param>        /// <returns></returns>        public override int HitTest(Point point)        {            if (Selected)            {                for (int i = 1; i <= HandleCount; i++)                {                    if (GetHandleRectangle(i).Contains(point))                        return i;                }            }            if (PointInObject(point))                return 0;            return -1;        }        protected override bool PointInObject(Point point)        {            return rectangle.Contains(point);        }        public override bool IntersectsWith(Rectangle rectangle)        {            return Rectangle.IntersectsWith(rectangle);        }        public override Cursor GetHandleCursor(int handleNumber)        {            switch (handleNumber)            {                case 1:                    return Cursors.SizeNWSE;                case 2:                    return Cursors.SizeNS;                case 3:                    return Cursors.SizeNESW;                case 4:                    return Cursors.SizeWE;                case 5:                    return Cursors.SizeNWSE;                case 6:                    return Cursors.SizeNS;                case 7:                    return Cursors.SizeNESW;                case 8:                    return Cursors.SizeWE;                default:                    return Cursors.Default;            }        }        public override void MoveHandleTo(Point point, int handleNumber)        {            float left = rectangle.Left;            float top = rectangle.Top;            float right = rectangle.Right;            float bottom = rectangle.Bottom;            switch (handleNumber)            {                case 1:                    left = point.X;                    top = point.Y;                    break;                case 2:                    top = point.Y;                    break;                case 3:                    right = point.X;                    top = point.Y;                    break;                case 4:                    right = point.X;                    break;                case 5:                    right = point.X;                    bottom = point.Y;                    break;                case 6:                    bottom = point.Y;                    break;                case 7:                    left = point.X;                    bottom = point.Y;                    break;                case 8:                    left = point.X;                    break;            }            SetRectangle(left, top, right - left, bottom - top);        }        public override void Move(int deltaX, int deltaY)        {            int x = ISurfaceBox.UnscaleScalar(deltaX);            int y = ISurfaceBox.UnscaleScalar(deltaY);            startPoint.X += x;            startPoint.Y += y;            rectangle.X += x;            rectangle.Y += y;        }                public override RectangleF GetBoundingBox()        {            return rectangle;        }        public override List<PointF> GetPoints()        {            List<PointF> points = new List<PointF>();            points.Add(new PointF(rectangle.X, rectangle.Y));            points.Add(new PointF(rectangle.Right, rectangle.Bottom));            return points;        }        public override ParentStyleModel GetStyle()        {            return labelTextModel;        }        public override string GetContent()        {            return textboxMessage;        }    }}
 |