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

余生长醉 提交于 2019-12-08 06:40:54

问题


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


回答1:


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#



来源:https://stackoverflow.com/questions/5307938/drag-and-drop-function-with-drawing-rectangle-c-sharp-net-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!