AccImageBox.cs 28.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Acc.ImageBox
{
    // Cyotek ImageBox
    // Copyright (c) 2010-2014 Cyotek.
    // http://cyotek.com
    // http://cyotek.com/blog/tag/ImageBox

    // Licensed under the MIT License. See ImageBox-license.txt for the full text.

    // If you use this control in your applications, attribution, donations or contributions are welcome.

    public class AccImageBox : ImageBox
    {
        #region Instance Fields

        //private readonly DragHandleCollection _dragHandles;

        private int _dragHandleSize;

        private Size _minimumSelectionSize;

        #endregion

        #region Public Constructors

        public AccImageBox()
        {
            //_dragHandles = new DragHandleCollection();
            this.DragHandleSize = 9;
            this.MinimumSelectionSize = Size.Empty;
            //this.PositionDragHandles();
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when the DragHandleSize property value changes
        /// </summary>
        [Category("Property Changed")]
        public event EventHandler DragHandleSizeChanged;

        /// <summary>
        /// Occurs when the MinimumSelectionSize property value changes
        /// </summary>
        [Category("Property Changed")]
        public event EventHandler MinimumSelectionSizeChanged;

        [Category("Action")]
        public event EventHandler SelectionMoved;

        [Category("Action")]
        public event CancelEventHandler SelectionMoving;

        [Category("Action")]
        public event EventHandler SelectionResized;

        [Category("Action")]
        public event CancelEventHandler SelectionResizing;

        #endregion

        #region Overridden Methods

        private bool IsInSelectionRegion(Point cursorLocation, out PointF pathPoint)
        {
            pathPoint = PointF.Empty;
            if (this.SelectionRegion.PointCount > 0)
            {
                var imagePoint = this.PointToImage(cursorLocation);
                PointF[] points = this.SelectionRegion.PathPoints;
                foreach (PointF p in points)
                {
                    float size =(float) (this.DragHandleSize / this.ZoomFactor);
                    //size = (float)this.DragHandleSize;
                    RectangleF recf = new RectangleF(p.X - size / 2, p.Y - size / 2, size, size);
                    if (recf.Contains(imagePoint))
                    {
                        pathPoint = p;
                        return false;
                    }
                }
                if (this.SelectionRegion.IsVisible(imagePoint))
                {
                    return true;
                }

            }
            return false;
        }

        /// <summary>
        ///   Raises the <see cref="System.Windows.Forms.Control.MouseDown" /> event.
        /// </summary>
        /// <param name="e">
        ///   A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.
        /// </param>
        protected override void OnMouseDown(MouseEventArgs e)
        {

            bool inSelectionRegion = IsInSelectionRegion(e.Location, out PointF pathPoint);
            
            if (e.Button == MouseButtons.Left && (inSelectionRegion || !pathPoint.IsEmpty))
            {
                this.DragOrigin = e.Location;
                //this.DragOriginOffset = new Point(imagePoint.X - (int)this.SelectionRegion.X, imagePoint.Y - (int)this.SelectionRegion.Y);
            }
            else
            {
                //this.DragOriginOffset = Point.Empty;
                this.DragOrigin = Point.Empty;
            }

            base.OnMouseDown(e);
        }

        /// <summary>
        ///   Raises the <see cref="System.Windows.Forms.Control.MouseMove" /> event.
        /// </summary>
        /// <param name="e">
        ///   A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.
        /// </param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            // start either a move or a resize operation
            if (!this.IsSelecting && !this.IsMoving && !this.IsResizing && e.Button == MouseButtons.Left && !this.DragOrigin.IsEmpty && this.IsOutsideDragZone(e.Location))
            {
                bool inSelectionRegion = IsInSelectionRegion(e.Location, out PointF pathPoint);
                if (inSelectionRegion)
                {
                    // move
                    this.StartMove();
                }
                else if (!pathPoint.IsEmpty)
                {
                    // resize
                    this.StartResize(pathPoint);
                }
                
            }

            // set the cursor
            this.SetCursor(e.Location);

            // perform operations
            this.ProcessSelectionMove(e.Location);
            this.ProcessSelectionResize(e.Location);

            base.OnMouseMove(e);
        }

        /// <summary>
        ///   Raises the <see cref="System.Windows.Forms.Control.MouseUp" /> event.
        /// </summary>
        /// <param name="e">
        ///   A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.
        /// </param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (this.IsMoving)
            {
                this.CompleteMove();
            }
            else if (this.IsResizing)
            {
                this.CompleteResize();
            }

            base.OnMouseUp(e);
        }

        /// <summary>
        ///   Raises the <see cref="System.Windows.Forms.Control.Paint" /> event.
        /// </summary>
        /// <param name="e">
        ///   A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data.
        /// </param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (this.AllowPainting)
            {
                if (base.SelectionRegion.PointCount > 0)
                {
                    PointF[] points = base.SelectionRegion.PathPoints;
                    Rectangle viewport = this.GetImageViewPort();
                    int offsetX = viewport.Left + this.Padding.Left + this.AutoScrollPosition.X;
                    int offsetY = viewport.Top + this.Padding.Top + this.AutoScrollPosition.Y;
                    Matrix matrix = new Matrix();
                    matrix.Translate(offsetX, offsetY);
                    matrix.Scale((float)this.ZoomFactor, (float)this.ZoomFactor);
                    matrix.TransformPoints(points);
                    foreach (PointF point in points)
                    {
                        this.DrawDragHandle(e.Graphics, point);
                    }
                }
            }
        }

        /// <summary>
        ///   Raises the <see cref="System.Windows.Forms.Control.Resize" /> event.
        /// </summary>
        /// <param name="e">
        ///   An <see cref="T:System.EventArgs" /> that contains the event data.
        /// </param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            //this.PositionDragHandles();
        }

        /// <summary>
        ///   Raises the <see cref="System.Windows.Forms.ScrollableControl.Scroll" /> event.
        /// </summary>
        /// <param name="se">
        ///   A <see cref="T:System.Windows.Forms.ScrollEventArgs" /> that contains the event data.
        /// </param>
        protected override void OnScroll(ScrollEventArgs se)
        {
            base.OnScroll(se);

            //this.PositionDragHandles();
        }

        /// <summary>
        ///   Raises the <see cref="ImageBox.Selecting" /> event.
        /// </summary>
        /// <param name="e">
        ///   The <see cref="System.EventArgs" /> instance containing the event data.
        /// </param>
        protected override void OnSelecting(ImageBoxCancelEventArgs e)
        {
            bool inSelectionRegion = IsInSelectionRegion(e.Location, out PointF pathPoint);

            e.Cancel = this.IsMoving || this.IsResizing || this.SelectionRegion.IsVisible(this.PointToImage(e.Location)) || pathPoint != PointF.Empty;

            base.OnSelecting(e);
        }

        /// <summary>
        ///   Raises the <see cref="ImageBox.SelectionRegionChanged" /> event.
        /// </summary>
        /// <param name="e">
        ///   The <see cref="System.EventArgs" /> instance containing the event data.
        /// </param>
        protected override void OnSelectionRegionChanged(EventArgs e)
        {
            base.OnSelectionRegionChanged(e);

            //this.PositionDragHandles();
        }

        /// <summary>
        ///   Raises the <see cref="ImageBox.ZoomChanged" /> event.
        /// </summary>
        /// <param name="e">
        ///   The <see cref="System.EventArgs" /> instance containing the event data.
        /// </param>
        protected override void OnZoomChanged(EventArgs e)
        {
            base.OnZoomChanged(e);

            //this.PositionDragHandles();
        }

        /// <summary>
        /// Processes a dialog key.
        /// </summary>
        /// <returns>
        /// true if the key was processed by the control; otherwise, false.
        /// </returns>
        /// <param name="keyData">One of the <see cref="T:System.Windows.Forms.Keys"/> values that represents the key to process. </param>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            bool result;

            if (keyData == Keys.Escape && (this.IsResizing || this.IsMoving))
            {
                if (this.IsResizing)
                {
                    this.CancelResize();
                }
                else
                {
                    this.CancelMove();
                }

                result = true;
            }
            else
            {
                result = base.ProcessDialogKey(keyData);
            }

            return result;
        }

        #endregion

        #region Public Properties

        [Category("Appearance")]
        [DefaultValue(8)]
        public virtual int DragHandleSize
        {
            get { return _dragHandleSize; }
            set
            {
                if (this.DragHandleSize != value)
                {
                    _dragHandleSize = value;

                    this.OnDragHandleSizeChanged(EventArgs.Empty);
                }
            }
        }

        //[Browsable(false)]
        //public DragHandleCollection DragHandles
        //{
            //get { return _dragHandles; }
       // }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsMoving { get; protected set; }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsResizing { get; protected set; }

        [Category("Behavior")]
        [DefaultValue(typeof(Size), "0, 0")]
        public virtual Size MinimumSelectionSize
        {
            get { return _minimumSelectionSize; }
            set
            {
                if (this.MinimumSelectionSize != value)
                {
                    _minimumSelectionSize = value;

                    this.OnMinimumSelectionSizeChanged(EventArgs.Empty);
                }
            }
        }

        [Browsable(false)]
        public GraphicsPath PreviousSelectionRegion { get; protected set; }

        #endregion

        #region Protected Properties

        protected Point DragOrigin { get; set; }

        //protected Point DragOriginOffset { get; set; }

        protected PointF ResizePoint { get; set; }

        #endregion

        #region Public Members

        public void CancelResize()
        {
            this.SelectionRegion = this.PreviousSelectionRegion;
            this.CompleteResize();
        }

        public void StartMove()
        {
            CancelEventArgs e;

            if (this.IsMoving || this.IsResizing)
            {
                throw new InvalidOperationException("A move or resize action is currently being performed.");
            }

            e = new CancelEventArgs();

            this.OnSelectionMoving(e);

            if (!e.Cancel)
            {
                this.PreviousSelectionRegion = this.SelectionRegion;
                this.IsMoving = true;
            }
        }

        #endregion

        #region Protected Members

        /// <summary>
        /// 画改变大小的点
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="pointf"></param>
        protected virtual void DrawDragHandle(Graphics graphics, PointF pointf)
        {
            float size = (float)(this.DragHandleSize);
            float left = pointf.X - size / 2;
            float top = pointf.Y - size / 2;
            float width = size;
            float height = size;
            Pen outerPen = SystemPens.WindowFrame;
            Brush innerBrush = SystemBrushes.Window;

            graphics.FillRectangle(innerBrush, left + 1, top + 1, width - 2, height - 2);
            graphics.DrawLine(outerPen, left + 1, top, left + width - 2, top);
            graphics.DrawLine(outerPen, left, top + 1, left, top + height - 2);
            graphics.DrawLine(outerPen, left + 1, top + height - 1, left + width - 2, top + height - 1);
            graphics.DrawLine(outerPen, left + width - 1, top + 1, left + width - 1, top + height - 2);
        }

        /// <summary>
        /// Raises the <see cref="DragHandleSizeChanged" /> event.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected virtual void OnDragHandleSizeChanged(EventArgs e)
        {
            EventHandler handler;

            //this.PositionDragHandles();
            this.Invalidate();

            handler = this.DragHandleSizeChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="MinimumSelectionSizeChanged" /> event.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected virtual void OnMinimumSelectionSizeChanged(EventArgs e)
        {
            EventHandler handler;

            handler = this.MinimumSelectionSizeChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="SelectionMoved" /> event.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected virtual void OnSelectionMoved(EventArgs e)
        {
            EventHandler handler;

            handler = this.SelectionMoved;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="SelectionMoving" /> event.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected virtual void OnSelectionMoving(CancelEventArgs e)
        {
            CancelEventHandler handler;

            handler = this.SelectionMoving;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="SelectionResized" /> event.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected virtual void OnSelectionResized(EventArgs e)
        {
            EventHandler handler;

            handler = this.SelectionResized;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="SelectionResizing" /> event.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected virtual void OnSelectionResizing(CancelEventArgs e)
        {
            CancelEventHandler handler;

            handler = this.SelectionResizing;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion

        #region Private Members

        private void CancelMove()
        {
            this.SelectionRegion = this.PreviousSelectionRegion;
            this.CompleteMove();
        }

        private void CompleteMove()
        {
            this.ResetDrag();
            this.OnSelectionMoved(EventArgs.Empty);
        }

        private void CompleteResize()
        {
            this.ResetDrag();
            this.OnSelectionResized(EventArgs.Empty);
        }

        //private DragHandleAnchor HitTest(Point cursorPosition)
        //{
         //   return this.DragHandles.HitTest(cursorPosition);
        //}

        private bool IsOutsideDragZone(Point location)
        {
            Rectangle dragZone;
            int dragWidth;
            int dragHeight;

            dragWidth = SystemInformation.DragSize.Width;
            dragHeight = SystemInformation.DragSize.Height;
            dragZone = new Rectangle(this.DragOrigin.X - (dragWidth / 2), this.DragOrigin.Y - (dragHeight / 2), dragWidth, dragHeight);

            return !dragZone.Contains(location);
        }

        //private void PositionDragHandles()
        //{
        //    if (this.DragHandles != null && this.DragHandleSize > 0)
        //    {
        //        if (this.SelectionRegion.PointCount == 0)
        //        {
        //            foreach (DragHandle handle in this.DragHandles)
        //            {
        //                handle.Bounds = Rectangle.Empty;
        //            }
        //        }
        //        else
        //        {
        //            int left;
        //            int top;
        //            int right;
        //            int bottom;
        //            int halfWidth;
        //            int halfHeight;
        //            int halfDragHandleSize;
        //            Rectangle viewport;
        //            int offsetX;
        //            int offsetY;

        //            viewport = this.GetImageViewPort();
        //            offsetX = viewport.Left + this.Padding.Left + this.AutoScrollPosition.X;
        //            offsetY = viewport.Top + this.Padding.Top + this.AutoScrollPosition.Y;
        //            halfDragHandleSize = this.DragHandleSize / 2;
        //            //left = Convert.ToInt32((this.SelectionRegion.Left * this.ZoomFactor) + offsetX);
        //            //top = Convert.ToInt32((this.SelectionRegion.Top * this.ZoomFactor) + offsetY);
        //            //right = left + Convert.ToInt32(this.SelectionRegion.Width * this.ZoomFactor);
        //            //bottom = top + Convert.ToInt32(this.SelectionRegion.Height * this.ZoomFactor);
        //            //halfWidth = Convert.ToInt32(this.SelectionRegion.Width * this.ZoomFactor) / 2;
        //            //halfHeight = Convert.ToInt32(this.SelectionRegion.Height * this.ZoomFactor) / 2;

        //            //this.DragHandles[DragHandleAnchor.TopLeft].Bounds = new Rectangle(left - this.DragHandleSize, top - this.DragHandleSize, this.DragHandleSize, this.DragHandleSize);
        //            //this.DragHandles[DragHandleAnchor.TopCenter].Bounds = new Rectangle(left + halfWidth - halfDragHandleSize, top - this.DragHandleSize, this.DragHandleSize, this.DragHandleSize);
        //            //this.DragHandles[DragHandleAnchor.TopRight].Bounds = new Rectangle(right, top - this.DragHandleSize, this.DragHandleSize, this.DragHandleSize);
        //            //this.DragHandles[DragHandleAnchor.MiddleLeft].Bounds = new Rectangle(left - this.DragHandleSize, top + halfHeight - halfDragHandleSize, this.DragHandleSize, this.DragHandleSize);
        //            //this.DragHandles[DragHandleAnchor.MiddleRight].Bounds = new Rectangle(right, top + halfHeight - halfDragHandleSize, this.DragHandleSize, this.DragHandleSize);
        //            //this.DragHandles[DragHandleAnchor.BottomLeft].Bounds = new Rectangle(left - this.DragHandleSize, bottom, this.DragHandleSize, this.DragHandleSize);
        //            //this.DragHandles[DragHandleAnchor.BottomCenter].Bounds = new Rectangle(left + halfWidth - halfDragHandleSize, bottom, this.DragHandleSize, this.DragHandleSize);
        //            //this.DragHandles[DragHandleAnchor.BottomRight].Bounds = new Rectangle(right, bottom, this.DragHandleSize, this.DragHandleSize);
        //            //this.DragHandles[DragHandleAnchor.MiddleCenter].Bounds = new Rectangle(left + halfWidth - halfDragHandleSize, top + halfHeight - halfDragHandleSize, this.DragHandleSize, this.DragHandleSize);

        //        }
        //    }
        //}

        private void ProcessSelectionMove(Point cursorPosition)
        {
            if (this.IsMoving)
            {
                GraphicsPath path = new GraphicsPath(this.SelectionRegion.PathPoints, this.SelectionRegion.PathTypes);
                Point startPoint = this.PointToImage(this.DragOrigin, true);
                Point endPoint = this.PointToImage(cursorPosition, true);
                Matrix matrix = new Matrix();
                matrix.Translate(endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
                path.Transform(matrix);
                this.DragOrigin = cursorPosition;
                this.SelectionRegion = path;

                //x = Math.Max(0, imagePoint.X - this.DragOriginOffset.X);
                //if (x + this.SelectionRegion.Width >= this.ViewSize.Width)
                //{
                //    x = this.ViewSize.Width - (int)this.SelectionRegion.Width;
                //}

                //y = Math.Max(0, imagePoint.Y - this.DragOriginOffset.Y);
                //if (y + this.SelectionRegion.Height >= this.ViewSize.Height)
                //{
                //    y = this.ViewSize.Height - (int)this.SelectionRegion.Height;
                //}

                //this.SelectionRegion = new RectangleF(x, y, this.SelectionRegion.Width, this.SelectionRegion.Height);
            }
        }

        private void ProcessSelectionResize(Point cursorPosition)
        {
            if (this.IsResizing)
            {

                if (Control.ModifierKeys == Keys.Alt)
                {
                    //自由变形
                    Point endPoint = this.PointToImage(cursorPosition, true);
                    PointF[] pathPoints = this.SelectionRegion.PathPoints;
                    for (int i = 0; i < pathPoints.Length; i++)
                    {
                        PointF p = pathPoints[i]; 
                        var xdiff = Math.Abs(p.X - this.ResizePoint.X) < 3;
                        var ydiff = Math.Abs(p.Y - this.ResizePoint.Y) < 3;
                        if (xdiff && ydiff)
                        {
                            pathPoints[i] = endPoint;
                            this.ResizePoint = endPoint;
                            break;
                        }
                    }
                    GraphicsPath path = new GraphicsPath(pathPoints, this.SelectionRegion.PathTypes);
                    this.SelectionRegion = path;
                }
                else if (Control.ModifierKeys == Keys.Shift)
                {
                    //按比例缩放

                    GraphicsPath path = new GraphicsPath(this.SelectionRegion.PathPoints, this.SelectionRegion.PathTypes);
                    Point startPoint = this.PointToImage(this.DragOrigin, true);
                    Point endPoint = this.PointToImage(cursorPosition, true);
                    Matrix matrix = new Matrix();
                    //float scaleX = endPoint.X * 1.0f / startPoint.X;
                    //float scaleY = endPoint.Y * 1.0f / startPoint.Y;
                    //靠近中心点为缩小
                    var selectionBounds = this.SelectionRegion.GetBounds();
                    float centerX = selectionBounds.X + selectionBounds.Width / 2;
                    float centerY = selectionBounds.Y + selectionBounds.Height / 2;
                    double endToCenter = Math.Sqrt(Math.Pow(endPoint.X - centerX, 2) + Math.Pow(endPoint.Y - centerY, 2));
                    double startToCenter = Math.Sqrt(Math.Pow(startPoint.X - centerX, 2) + Math.Pow(startPoint.Y - centerY, 2));
                    float scale = scale = (float)(endToCenter / startToCenter);
                    //if (endToCenter > startToCenter)
                    //{
                    //    scale = (float)(startToCenter/endToCenter);
                    //}

                    matrix.Scale(scale, scale);
                    path.Transform(matrix);

                    var oldBounds = this.SelectionRegion.GetBounds();
                    var newBounds = path.GetBounds();
                    var oldCenterX = oldBounds.X + oldBounds.Width / 2;
                    var oldCenterY = oldBounds.Y + oldBounds.Height / 2;

                    var newCenterX = newBounds.X + newBounds.Width / 2;
                    var newCenterY = newBounds.Y + newBounds.Height / 2;

                    matrix.Reset();
                    matrix.Translate(oldCenterX - newCenterX, oldCenterY - newCenterY);
                    path.Transform(matrix);

                    this.DragOrigin = cursorPosition;
                    this.SelectionRegion = path;
                }
                else if (this.SelectionRegion.PointCount>0) {
                    //自由变形
                    Point endPoint = this.PointToImage(cursorPosition, true);
                    PointF[] pathPoints = this.SelectionRegion.PathPoints;
                    PointF startPoint= PointF.Empty;
                    for (int i = 0; i < pathPoints.Length; i++)
                    {
                        PointF p = pathPoints[i];
                        var xdiff = Math.Abs(p.X - this.ResizePoint.X) < 3;
                        var ydiff = Math.Abs(p.Y - this.ResizePoint.Y) < 3;
                        if (xdiff && ydiff)
                        {   if (i<1)
                                startPoint = pathPoints[i+2];
                            else if (i>1)
                                startPoint = pathPoints[i - 2];
                            pathPoints[i] = endPoint;
                            this.ResizePoint = endPoint;
                            break;
                        }
                    }


                    GraphicsPath path = new GraphicsPath();
                    path.AddRectangle(new RectangleF(startPoint.X, startPoint.Y, endPoint.X- startPoint.X, endPoint.Y- startPoint.Y));
                    this.SelectionRegion = path;
                }
                

                

                
            }
        }

        private void ResetDrag()
        {
            this.IsResizing = false;
            this.IsMoving = false;
            this.DragOrigin = Point.Empty;
            //this.DragOriginOffset = Point.Empty;
        }

        private void SetCursor(Point point)
        {
            Cursor cursor = Cursors.Default;
            if (IsMoving)
            {
                cursor = Cursors.SizeAll;
            }else if (IsResizing)
            {
                cursor = Cursors.NoMove2D;
            }
            else
            {
                bool inSelectionRegion = IsInSelectionRegion(point, out PointF pathPoint);
                if (inSelectionRegion)
                {
                    cursor = Cursors.SizeAll;
                }
                else if (!pathPoint.IsEmpty)
                {
                    cursor = Cursors.NoMove2D;
                }
            }
            
            this.Cursor = cursor;
        }


        private void StartResize(PointF anchor)
        {
            CancelEventArgs e;

            if (this.IsMoving || this.IsResizing)
            {
                throw new InvalidOperationException("A move or resize action is currently being performed.");
            }

            e = new CancelEventArgs();

            this.OnSelectionResizing(e);

            if (!e.Cancel)
            {
                this.ResizePoint = anchor;
                this.PreviousSelectionRegion = this.SelectionRegion;
                this.IsResizing = true;
            }
        }

        #endregion
    }
}