Drag and drop rectangle in C#

Posted by sama on Stack Overflow See other posts from Stack Overflow or by sama
Published on 2010-03-14T12:17:31Z Indexed on 2010/03/14 12:25 UTC
Read the original article Hit count: 564

Filed under:
|

I want to know how to draw rectangle in C# and make it dragged and dropped in the page here my code to draw it but I cannot drag or drop it.

public partial class Form1 : Form
{
    public bool drag = false;
    int cur_x, cur_y;
    Rectangle rec = new Rectangle(10, 10, 100, 100);
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs r)
    {
        base.OnPaint(r);
        Graphics g = r.Graphics;
        //g.DrawRectangle(Pens.Black, rec);
        g.FillRectangle(Brushes.Aquamarine, rec);

    }
    private void recmousedown(object sender, MouseEventArgs m)
    {
        if (m.Button != MouseButtons.Left)
            return;
        rec = new Rectangle(m.X, m.Y,100,100);

        drag = true;
        cur_x = m.X;
        cur_y = m.Y;
    }

    private void recmousemove(object sender, MouseEventArgs m)
    {
        if (m.Button != MouseButtons.Left)
            return;

       rec.X = m.X;
       rec.Y = m.Y;
       Invalidate();
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about system.drawing