Code Snippet
public partial class MainForm : Form
{
private Rectangle canvas = new Rectangle(new Point(5, 5), new Size(100, 100));
private bool mouseInCanvas;
private bool movingCanvas;
public MainForm()
{
InitializeComponent();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (this.canvas.Contains(e.Location))
{
if (!this.mouseInCanvas)
{
this.Cursor = Cursors.Hand;
this.mouseInCanvas = true;
}
}
else
{
if (this.mouseInCanvas)
{
this.Cursor = Cursors.Default;
this.mouseInCanvas = false;
}
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
this.movingCanvas = true;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (this.movingCanvas)
{
this.canvas.Location = e.Location;
this.movingCanvas = false;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.AliceBlue, this.canvas);
}
}