i need some programming help.in my widows form application i need to put my mouse pointer somewhere and then drag the mouse over the form in any direction.what my requirement is it should create a virtual square.and then i should be able to measure the dimensions of that square. i don't know my problem is clear enough.
simply i want to make square shaped selection area with the mouse dragging.it should be visualized so that i could see that area is selected anyway.and then i need to measure the dimensions of that area.(lengths of x and y directions)but that selection should not be static.after clicking the mouse it should be cleared.
i did google searching but could not get some better explanations.please can some body help me with some code snippet.
thanks
kind regards
sudhara
Suthish NairPosted Dec 22, 2010, 4:50 AM
Resolved?
FroglegPosted Dec 21, 2010, 12:43 AM
bool mouseDown = false;
Point pt1, pt2;
Rectangle rct;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
pt1.X = e.X;
pt1.Y = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown == true)
{
Graphics g = this.CreateGraphics();
this.Refresh();//can create region instead of refreshing entire form
pt2.X = e.X;
pt2.Y = e.Y;
if (pt2.X > pt1.X && pt2.Y > pt1.Y)//top left to bottom right
{
pt2.Y = pt1.Y + (pt2.X - pt1.X);//needed for square
rct = rct = Rectangle.FromLTRB(pt1.X, pt1.Y, pt2.X, pt2.Y);
}
else if (pt2.X > pt1.X && pt2.Y < pt1.Y)//bottom left to top right
{
pt2.Y = pt1.Y - (pt2.X - pt1.X);//needed for square
rct = rct = Rectangle.FromLTRB(pt1.X, pt2.Y, pt2.X, pt1.Y);
}
else if (pt2.X < pt1.X && pt2.Y > pt1.Y)//top right to bottom left
{
pt2.Y = pt1.Y - (pt2.X - pt1.X);//needed for square
rct = rct = Rectangle.FromLTRB(pt2.X, pt1.Y, pt1.X, pt2.Y);
}
else if (pt2.X < pt1.X && pt2.Y < pt1.Y)//bottom right to top left
{
pt2.Y = pt1.Y + (pt2.X - pt1.X);//needed for square
rct = rct = Rectangle.FromLTRB(pt2.X, pt2.Y, pt1.X, pt1.Y);
}
label1.Text = Convert.ToString(rct.Width);
label2.Text = Convert.ToString(rct.Height);
g.DrawRectangle(Pens.Black, rct);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
theLizardPosted Dec 19, 2010, 7:43 PM
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int xBegin, yBegin,w, h;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
xBegin = e.X;
yBegin = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
w = e.X - xBegin;
h = e.Y - yBegin;
Text = w.ToString() + " - " + h.ToString();
}
}
}
this is JUST the basics
xBegin and yBegin are the start points on the screen.
w is the width (start point of x - end point of x (e.X))
H is the Height (start point of y - end point of y (e.Y))
all you need to do now is to work out how to draw a rectangle..
Sam HobbsPosted Dec 19, 2010, 6:42 PM
http://social.msdn.microsoft.com/Search/en-US?query=mouse%20drag%20draw%20rectangle&ac=3
Suthish NairPosted Dec 19, 2010, 5:52 AM
Wait for more expert answers.
Also refer Our recommended articles section, right side panel of your browser window.
Having some good aricles, might helps you.