Windows Develop Bookmark and Share   
 index > Windows Forms General > Rectangle Shape
 

Rectangle Shape

Can someone tell me how to move a rectangle shap or update its location?
Demitrius Wheelwright  Sunday, July 08, 2007 6:48 AM

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

Wei Zhou - MSFT  Tuesday, July 10, 2007 5:51 AM
Use different arguments for the Graphics.DrawRectangle() method call.
nobugz  Sunday, July 08, 2007 5:00 PM
Can you please give me an example. I have no idea what your talking about.
Demitrius Wheelwright  Monday, July 09, 2007 5:52 AM

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

Wei Zhou - MSFT  Tuesday, July 10, 2007 5:51 AM

You can use google to search for other answers

Custom Search

More Threads

• ToolStripSplitButton->ToolStripMenuItem checkmark position
• is threre a message box which does not block the execution of the program
• Invoking "selected" option in webBrowserControl comboBox
• issue with the List view scroll bar
• signature capturing from third party tool
• Multiple Forms / Calling a Single Form from the Code
• UserControl doesn't update
• How to unlock keyboard or mouse input stream on SendInput Function?
• Paint Game Loop
• DataGridView CellValueChanged event problem