Esempio n. 1
0
        protected override void OnMouseMove(MouseEventArgs e)
        {
            // Khi di chuyển chuột (và đè chuột trái - tức đang vẽ) trên vùng DrawBox
            // Với các trường hợp vẽ hình: liên tục cập nhật lại DrawBox để tạo độ mượt
            // (xét trường hợp có đè nút Shift tạo hình vuông) ở phần vẽ hình

            base.OnMouseMove(e);

            switch (_drawStatus)
            {
            case DrawStatus.LineDrawing:
                ptMouseMove = e.Location;
                break;

            case DrawStatus.ToolDrawing:
                DrawDrag(ptMouseDown, e.Location, MyPaint.DrawType);
                ptMouseDown = e.Location;
                break;

            case DrawStatus.TextDrawing:
                areaRect = ShapePaint.CreateRectangle(ptMouseDown, e.Location);
                break;

            case DrawStatus.ShapeDrawing:
                if (ModifierKeys == Keys.Shift)
                {
                    areaRect = ShapePaint.CreateSquare(ptMouseDown, e.Location);
                }
                else
                {
                    areaRect = ShapePaint.CreateRectangle(ptMouseDown, e.Location);
                }
                break;

            case DrawStatus.EditDrawing:
                _dragHandle = EditPaint.GetDragHandle(e.Location, areaRect);
                if (_dragHandle < 9)
                {
                    UpdateCursor();
                }
                else if (areaRect.Contains(e.Location))
                {
                    Cursor = Cursors.SizeAll;
                }
                else
                {
                    Cursor = Cursors.Default;
                }
                break;

            case DrawStatus.ResizingShape:
                EditPaint.UpdateResizeRect(ref areaRect, oldRect, ptMouseDown, e.Location, _dragHandle);
                break;

            case DrawStatus.MovingShape:
                EditPaint.UpdateMoveRect(ref areaRect, oldRect, ptMouseDown, e.Location);
                break;
            }
            this.Invalidate();
        }
Esempio n. 2
0
        public void Undo()
        {
            if (_drawStatus == DrawStatus.EditDrawing)
            {
                _drawStatus = DrawStatus.Idle;
                _g          = Graphics.FromImage(Image);
                ShapePaint.DrawShape(_g, _pen, areaRect, MyPaint.DrawType);
                RedoList.Push(new Bitmap(Image));
            }

            if (UndoList.Count > 0)
            {
                RedoList.Push(UndoList.Peek());
                Image = UndoList.Pop();
                Size  = Image.Size;
            }
        }
Esempio n. 3
0
        protected override void OnPaint(PaintEventArgs e)
        {
            // Vẽ liên tục các hình tạm trên DrawBox nhằm tạo độ mượt

            base.OnPaint(e);

            switch (_drawStatus)
            {
            case DrawStatus.LineDrawing:
                e.Graphics.DrawLine(_pen, ptMouseDown, ptMouseMove);
                break;

            case DrawStatus.TextDrawing:
                ShapePaint.DrawShape(e.Graphics, _pen, areaRect, "Rectangle");
                break;

            case DrawStatus.ShapeDrawing:
                ShapePaint.DrawShape(e.Graphics, _pen, areaRect, MyPaint.DrawType);
                break;

            case DrawStatus.ResizingShape:
            case DrawStatus.MovingShape:
            case DrawStatus.EditDrawing:
                if (MyPaint.DrawType == "Text")
                {
                    if (areaRect.Height < textRect.Height)
                    {
                        areaRect.Height = textRect.Height;
                        ShapePaint.DrawShape(e.Graphics, _pen, areaRect, "Rectangle");
                    }
                }
                else
                {
                    ShapePaint.DrawShape(e.Graphics, _pen, areaRect, MyPaint.DrawType);
                }
                EditPaint.DrawDragRectangle(e.Graphics, areaRect);
                break;
            }
        }
Esempio n. 4
0
        // Khởi tạo các thuộc tính khi nhấn chuột trái vào vùng DrawBox
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            ptMouseDown = ptMouseMove = e.Location;

            switch (_drawStatus)
            {
            // Trường hơp chưa vẽ: lấy màu vẽ
            case DrawStatus.Idle:
                if (e.Button == MouseButtons.Left)
                {
                    _drawColor = MyPaint.LeftColor;
                }

                else if (e.Button == MouseButtons.Right)
                {
                    _drawColor = MyPaint.RightColor;
                }

                SetGraphics();
                break;

            // Đang chỉnh hình (Edit, Resize): căn cứ con trỏ chuột đang trỏ chuột:
            // Trỏ chuột trên 8 ô Resize: chuyển sang trạng thái Resize hình
            // Trỏ chuột trong hình: Chuyển sang trạng thái Move hình
            // Trỏ chuột ngoài hình: Kết thúc và vẽ hình

            case DrawStatus.EditDrawing:
                if (_dragHandle != 9)
                {
                    oldRect     = areaRect;
                    _drawStatus = DrawStatus.ResizingShape;
                }

                else if (areaRect.Contains(e.Location))
                {
                    oldRect     = areaRect;
                    _drawStatus = DrawStatus.MovingShape;
                }

                else
                {
                    _drawStatus = DrawStatus.Idle;
                    _g          = Graphics.FromImage(Image);

                    if (MyPaint.DrawType == "Text")
                    {
                        if (TextToDraw != null)
                        {
                            UndoList.Push(new Bitmap(Image));
                            RedoList.Clear();
                            AddText(TextToDraw, areaRect);
                            RedoList.Push(new Bitmap(Image));
                        }
                        textBoxPopUp.Dispose();
                    }
                    else
                    {
                        ShapePaint.DrawShape(_g, _pen, areaRect, MyPaint.DrawType);
                        RedoList.Push(new Bitmap(Image));
                    }
                    areaRect = Rectangle.Empty;
                    this.Invalidate();
                }
                break;
            }
        }
Esempio n. 5
0
        protected override void OnMouseUp(MouseEventArgs e)
        {
            // Nhấc chuột lên: Hoàn thành vẽ với vẽ đường thẳng, các trường hợp còn lại chuyển sang Resize

            base.OnMouseUp(e);

            switch (_drawStatus)
            {
            case DrawStatus.LineDrawing:
                _g = Graphics.FromImage(Image);
                if (ptMouseDown != ptMouseMove)
                {
                    UndoList.Push(new Bitmap(Image));
                    RedoList.Clear();
                    _g.DrawLine(_pen, ptMouseDown, ptMouseMove);
                    RedoList.Push(new Bitmap(Image));
                }
                _drawStatus = DrawStatus.Idle;
                break;

            case DrawStatus.ToolDrawing:
                _drawStatus = DrawStatus.Idle;
                RedoList.Push(new Bitmap(Image));
                break;

            case DrawStatus.TextDrawing:
                if (areaRect != Rectangle.Empty)
                {
                    _drawStatus = DrawStatus.EditDrawing;
                    DrawTextBox(null);
                }
                else
                {
                    _drawStatus = DrawStatus.Idle;
                }
                break;

            case DrawStatus.ShapeDrawing:
                _g = CreateGraphics();
                if (areaRect.Width > 1 && areaRect.Height > 1 && ptMouseDown != e.Location)
                {
                    UndoList.Push(new Bitmap(Image));
                    RedoList.Clear();
                    ShapePaint.DrawShape(_g, _pen, areaRect, MyPaint.DrawType);
                    EditPaint.DrawDragRectangle(_g, areaRect);
                    _drawStatus = DrawStatus.EditDrawing;
                }
                else
                {
                    _drawStatus = DrawStatus.Idle;
                }
                break;

            case DrawStatus.ResizingShape:
            case DrawStatus.MovingShape:
                if (MyPaint.DrawType == "Text")
                {
                    textBoxPopUp.Dispose();
                    DrawTextBox(TextToDraw);
                }
                _drawStatus = DrawStatus.EditDrawing;
                break;
            }
        }