PDA

View Full Version : سوال: رسم مستطیل با ماوس



quantomquery
چهارشنبه 02 اردیبهشت 1388, 20:27 عصر
به ساده ترین روش چگونه می توانیم در pictureBox در سی شارپ با ماوس یک مستطیل بکشیم؟(با کمترین تعداد خط کد نویسی)

Sajjad.Aghapour
چهارشنبه 02 اردیبهشت 1388, 20:48 عصر
Graphics g;
//form load or any another event
g = picBox.CreateGraphics();

// picBox mouse_move event
if (e.Button == MouseButtons.Left)
{
g.Clear(picBox.BackColor);
g.DrawRectangle(Pens.Black, 0, 0, e.X, e.Y);
}

prankster
چهارشنبه 02 اردیبهشت 1388, 21:02 عصر
pictureBox1.MouseDown += (sender, e) =>
{
pictureBox1.Tag = new Point(e.X, e.Y);
};

pictureBox1.MouseMove += (sender, e) =>
{
if (e.Button == MouseButtons.Left)
{
Graphics graphics = pictureBox1.CreateGraphics();
Point mouseDownLocation = (Point)pictureBox1.Tag;

graphics.Clear(pictureBox1.BackColor);
graphics.DrawLine(Pens.Black, mouseDownLocation.X, mouseDownLocation.Y, e.X, mouseDownLocation.Y);
graphics.DrawLine(Pens.Black, e.X, mouseDownLocation.Y, e.X, e.Y);
graphics.DrawLine(Pens.Black, e.X, e.Y, mouseDownLocation.X, e.Y);
graphics.DrawLine(Pens.Black, mouseDownLocation.X, e.Y, mouseDownLocation.X, mouseDownLocation.Y);
}
};