ugp

how can i draw a rectangle by mouse pointer.
when the mouse is dragging (over the Form) need to draw the rectangle.

int X = 100;
int Y = 100;
int A = 25;
int B = 50;
Graphics g = this.CreateGraphics();
g.DrawRectangle(Pens.Black, X,Y,A,B );

how can i do this!
draw a rectangle according to the mouse pointer dragging!.


Re: Windows Forms General how can i draw a rectangle by mouse pointer.

ArranNyc

The following code will draw a rectangle when the mouse is dragged.

The main methods to look at, are the mouse event handlers, MouseDown, MouseMove and MouseUp. The rectangle is drawn while the mouse is moving and the mouse state mouseDown is true.

If you would like to draw a trail of rectangles, then remove the invalidate() statement from the MouseMove event handler.

Code Snippet

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace WindowsApplication

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private bool mouseDown = false;

Rectangle rect;

Graphics g;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(512, 438);

this.Name = "Form1";

this.Text = "Form1";

this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);

this.Load += new System.EventHandler(this.Form1_Load);

this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);

this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

{

mouseDown = true;

}

private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)

{

if (mouseDown)

{

int X = e.X;

int Y = e.Y;

int A = 25;

int B = 50;

rect = new Rectangle(X,Y,A,B);

g.DrawRectangle(Pens.Black,rect);

this.Invalidate();

}

}

private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

{

mouseDown = false;

g.DrawRectangle(Pens.Black,rect);

}

private void Form1_Load(object sender, System.EventArgs e)

{

g = this.CreateGraphics();

//Turn on double buffering

this.SetStyle(

ControlStyles.AllPaintingInWmPaint |

ControlStyles.UserPaint |

ControlStyles.DoubleBuffer,

true);

this.UpdateStyles();

}

}

}





Re: Windows Forms General how can i draw a rectangle by mouse pointer.

nobugz

Check this thread.





Re: Windows Forms General how can i draw a rectangle by mouse pointer.

ugp

thanks for the response!

i have another issue ...
I need to resize this generating rectangle...

Im trying to do this by adding to a panel; - since i was able to resize the Panel, the Rectangle also will resize automatically.

I was able to add a button/label to a Panel like this;
Code Snippet

this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.label11);


What I need is to add a Rectangle object to a Panel.

I draw a Rectangle object;
Code Snippet

Graphics graph = this.CreateGraphics();
Pen penCurrent = new Pen(Color.Red);
Rectangle Rect = new Rectangle(300, 50, 150, 75);
graph.DrawRectangle(penCurrent, Rect);


But when i do like this;
Code Snippet

this.panel1.Controls.Add(this.Rect);


it gives an ERROR!

So how can i achive this;
add a Rectangle object to a Panel...

OR
Is there any other way to do this..- resize the drawn rectangle!