Search code examples
c#drag-and-dropsystem.drawingwindows-forms-designer

Drag and Drop Function with Drawing Rectangle C# .net - Forms


I want to drag and drop that I have drawn on the forms. Here is my code for drawing the rectangle. An this works fine.

        Rectangle rec = new Rectangle(0, 0, 0, 0);

        public Form1()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.Aquamarine, rec);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                rec = new Rectangle(e.X, e.Y, 0, 0);
                Invalidate();
            }
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                rec.Width = e.X - rec.X;
                rec.Height = e.Y - rec.Y;
                Invalidate();
            }
        }

Now I want to drag and drop that rectangle in to a different place.

Pls help How to do that

Thank You

yohan


Solution

  • I have written a helper class for this kind of stuff:

    class ControlMover
    {
        public enum Direction
        {
            Any,
            Horizontal,
            Vertical
        }
    
        public static void Init(Control control)
        {
            Init(control, Direction.Any);
        }
    
        public static void Init(Control control, Direction direction)
        {
            Init(control, control, direction);
        }
    
        public static void Init(Control control, Control container, Direction direction)
        {
            bool Dragging = false;
            Point DragStart = Point.Empty;
            control.MouseDown += delegate(object sender, MouseEventArgs e)
            {
                Dragging = true;
                DragStart = new Point(e.X, e.Y);
                control.Capture = true;
            };
            control.MouseUp += delegate(object sender, MouseEventArgs e)
            {
                Dragging = false;
                control.Capture = false;
            };
            control.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                if (Dragging)
                {
                    if (direction != Direction.Vertical)
                        container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
                    if (direction != Direction.Horizontal)
                        container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
                }
            };
        }
    }  
    

    Then I just Initialize it in my form load event with my control:

    ControlMover.Init(myControl, myContainer, ControlMover.Direction.Any);  
    

    Well, you don't have a control to move. It's a rectangle. But hopefully, you'll get the idea.
    UPDATE: Have you checked out the related questions listed in this page? Try:
    Drag and drop rectangle in C#