Demitrius Wheelwright

Can someone tell me how to move a rectangle shap or update its location


Re: Windows Forms General Rectangle Shape

nobugz

Use different arguments for the Graphics.DrawRectangle() method call.





Re: Windows Forms General Rectangle Shape

Demitrius Wheelwright

Can you please give me an example. I have no idea what your talking about.




Re: Windows Forms General Rectangle Shape

Wei Zhou - MSFT

Hi

I wrote a small example, hope this may help for you.

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);

}

}

Regards

Wei Zhou