|
using System.Drawing;
using System.Windows.Forms;
class Test : Form
{
Rectangle r = Rectangle.Empty;
protected override void OnMouseDown(MouseEventArgs e)
{
r.X = e.X;
r.Y = e.Y;
}
protected override void OnMouseUp(MouseEventArgs e)
{
if(e.X < r.X)
{
r.Width = r.X - e.X;
r.X = e.X;
}
else
{
r.Width = e.X - r.X;
}
if(e.Y < r.Y)
{
r.Height = r.Y - e.Y;
r.Y = e.Y;
}
else
{
r.Height = e.Y - r.Y;
}
Invalidate(new Rectangle(r.X, r.Y, r.Width+1, r.Height+1));
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawRectangle(new Pen(Color.Blue, 0), r);
}
static void Main()
{
Application.Run(new Test());
}
}
Top
using System.Drawing;
using System.Windows.Forms;
class Test : Form
{
int x0, y0, x1, y1;
Rectangle r = Rectangle.Empty;
protected override void OnMouseDown(MouseEventArgs e)
{
x0 = e.X;
y0 = e.Y;
}
protected override void OnMouseMove(MouseEventArgs e)
{
x1 = e.X;
y1 = e.Y;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (x0 < x1) { r.X = x0; r.Width = x1 - x0; }
else { r.X = x1; r.Width = x0 - x1; }
if (y0 < y1) { r.Y = y0; r.Height = y1 - y0; }
else { r.Y = y1; r.Height = y0 - y1; }
e.Graphics.DrawRectangle(new Pen(Color.Blue, 0), r);
}
static void Main()
{
Application.Run(new Test());
}
}
|