Drag and drop rectangle in C#
- by sama
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();
}
}