我想在 mouse_down 事件工作正常的窗体上绘制一个矩形,但是我如何对 mouse_up 进行编程。我应该如何确定起始坐标。
http://oi49.tinypic.com/2wcnof8.jpg
Rectangle rect = new Rectangle();
Color rastercolor = Color.Black;
private void drawGrid(Color rastercolor)
{
Graphics gfx = this.CreateGraphics();
Pen pen = new Pen(rastercolor);
for (int i = 0; i <= 2000; i = i + 20)
{
gfx.DrawLine(pen, 0, i, 2000, i);
}
for (int x = 0; x < 2000; x = x + 20)
{
gfx.DrawLine(pen, x, 0, x, 2000);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
rect = new Rectangle(e.X, e.Y, 0, 0);
if (e.Button == MouseButtons.Middle)
{
ColorDialog coldial = new ColorDialog();
coldial.ShowDialog();
Color rastercolor = coldial.Color;
tekenGrid(rastercolor);
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
}
}
protected override void OnPaint(PaintEventArgs e)
{
tekenGrid(rastercolor);
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
private void Frm1_MouseUp(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Red, 2);
g.DrawRectangle(pen, rect);
}
private void Frm1_Load(object sender, EventArgs e)
{
}
private void Frm1_MouseDown(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue, 2);
g.DrawRectangle(pen, rect);
}
}
}
任何帮助将不胜感激,