Example #1
0
        // Mouse is moved.
        // None button is pressed, ot left button is pressed.
        public override void OnMouseMove(Canvas drawArea, MouseEventArgs e)
        {
            Point point = drawArea.BackTrackMouse(new Point(e.X, e.Y));

            wasMove = true;
            if (e.Button == MouseButtons.None)
            {
                Cursor cursor = Cursors.Default;
                for (int i = drawArea.Document.Count - 1; i >= 0; i--)
                {
                    int n = drawArea.Document[i].HitTest(point);
                    if (n > 0)
                    {
                        cursor = drawArea.Document[i].GetHandleCursor(n);
                        break;
                    }
                }

                drawArea.Cursor = cursor;
                return;
            }

            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            if (point.X < 0)
            {
                point.X = 0;
            }
            if (point.X > drawArea.Width)
            {
                point.X = drawArea.Width;
            }

            if (point.Y < 0)
            {
                point.Y = 0;
            }
            if (point.Y > drawArea.Height)
            {
                point.Y = drawArea.Height;
            }

            if (lastPoint.X < 0)
            {
                lastPoint.X = 0;
            }
            if (lastPoint.X > drawArea.Width)
            {
                lastPoint.X = drawArea.Width;
            }

            if (lastPoint.Y < 0)
            {
                point.Y = 0;
            }
            if (lastPoint.Y > drawArea.Height)
            {
                lastPoint.Y = drawArea.Height;
            }


            // Left button is pressed
            // Find difference between previous and current position
            int dx = point.X - lastPoint.X;
            int dy = point.Y - lastPoint.Y;


            //加快移动速度
            int zoom = drawArea.MessageControl != null ?drawArea.MessageControl.Zoom : 6;

            if (Math.Abs(dx) < zoom && Math.Abs(dy) < zoom)
            {
                return;
            }

            int xOffset = dx % zoom;
            int yOffset = dy % zoom;

            if (xOffset != 0 || yOffset % zoom != 0)
            {
                dx -= xOffset;
                dy -= yOffset;
            }

            if (!ClampCanvas(drawArea, point))
            {
                return;
            }
            lastPoint = point;

            xOffset = point.X % zoom;
            yOffset = point.Y % zoom;
            if (xOffset != 0 || yOffset != 0)
            {
                if (xOffset <= zoom / 2)
                {
                    point.X -= xOffset;
                }
                else
                {
                    point.X = point.X + zoom - xOffset;
                }

                if (yOffset <= zoom / 2)
                {
                    point.Y -= yOffset;
                }
                else
                {
                    point.Y = point.Y + zoom - yOffset;
                }
            }


            // resize
            if (selectMode == SelectionMode.Size)
            {
                if (resizedObject != null)
                {
                    //if (resizedObject.Type == ShapeType.Text)
                    //{
                    //    ((ShapeLabel)resizedObject).FrameSizeFixed = true;
                    //}

                    if (resizedObject.Type != ShapeType.Line)
                    {
                        switch (resizedObjectHandle)
                        {
                        case 1:
                            if (point.X >= resizedObject.VirtualBounds.Right)
                            {
                                point.X = resizedObject.VirtualBounds.Right - zoom;
                            }
                            if (point.Y >= resizedObject.VirtualBounds.Bottom)
                            {
                                point.Y = resizedObject.VirtualBounds.Bottom - zoom;
                            }
                            break;

                        case 2:
                            if (point.Y >= resizedObject.VirtualBounds.Bottom)
                            {
                                point.Y = resizedObject.VirtualBounds.Bottom - zoom;
                            }
                            break;

                        case 3:
                            if (point.X <= resizedObject.VirtualBounds.Left)
                            {
                                point.X = resizedObject.VirtualBounds.Left + zoom;
                            }
                            if (point.Y >= resizedObject.VirtualBounds.Bottom)
                            {
                                point.Y = resizedObject.VirtualBounds.Bottom - zoom;
                            }
                            break;

                        case 4:
                            if (point.X < resizedObject.VirtualBounds.Left)
                            {
                                point.X = resizedObject.VirtualBounds.Left + zoom;
                            }
                            break;

                        case 5:
                            if (point.X <= resizedObject.VirtualBounds.Left)
                            {
                                point.X = resizedObject.VirtualBounds.Left + zoom;
                            }
                            if (point.Y <= resizedObject.VirtualBounds.Top)
                            {
                                point.Y = resizedObject.VirtualBounds.Top + zoom;
                            }
                            break;

                        case 6:
                            if (point.Y <= resizedObject.VirtualBounds.Top)
                            {
                                point.Y = resizedObject.VirtualBounds.Top + zoom;
                            }
                            break;

                        case 7:
                            if (point.X >= resizedObject.VirtualBounds.Right)
                            {
                                point.X = resizedObject.VirtualBounds.Right - zoom;
                            }
                            if (point.Y < resizedObject.VirtualBounds.Top)
                            {
                                point.Y = resizedObject.VirtualBounds.Top + zoom;
                            }
                            break;

                        case 8:
                            if (point.X >= resizedObject.VirtualBounds.Right)
                            {
                                point.X = resizedObject.VirtualBounds.Right - zoom;
                            }
                            break;

                        default:
                            break;
                        }
                        lastPoint = point;
                    }

                    resizedObject.MoveHandleTo(point, resizedObjectHandle);

                    drawArea.Document.ChangedService.MarkChanged();

                    if (resizedObject is ShapeLine)
                    {
                        lastPoint = resizedObject.GetHandle(resizedObjectHandle);
                    }

                    drawArea.Refresh();
                }
            }

            // move
            if (selectMode == SelectionMode.Move)
            {
                int movecount = 0;
                foreach (ShapeBase sb in drawArea.Document.SelectedShapes)
                {
                    if (sb.IsBackground)
                    {
                        continue;
                    }
                    movecount++;
                    sb.Move(dx, dy);
                }
                if (movecount > 0)
                {
                    drawArea.Document.ChangedService.MarkChanged();
                }

                drawArea.Cursor = Cursors.SizeAll;
                drawArea.Refresh();
            }

            if (selectMode == SelectionMode.NetSelection)
            {
                drawArea.NetRectangle = ShapeControlPaintNew.GetNormalizedRectangle(startPoint, lastPoint);
                drawArea.Refresh();
                return;
            }
        }