有没有办法用起点和终点而不是起点和区域绘制矩形?我正在使用以下代码通过鼠标在表单上绘制矩形:
System.Drawing.Graphics formGraphics;
bool isDown = false;
int initialX;
int initialY;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isDown = true;
initialX = e.X;
initialY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isDown == true)
{
this.Refresh();
Pen drwaPen = new Pen(Color.Navy,1);
int width = e.X - initialX, height = e.Y - initialY;
//if (Math.Sign (width) == -1) width = width
//Rectangle rect = new Rectangle(initialPt.X, initialPt.Y, Cursor.Position.X - initialPt.X, Cursor.Position.Y - initialPt.Y);
Rectangle rect = new Rectangle(initialX, initialY, width * Math.Sign(width), height * Math.Sign(height));
formGraphics = this.CreateGraphics();
formGraphics.DrawRectangle(drwaPen, rect);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isDown = false;
}
我可以使用此代码绘制矩形,当我将鼠标从其起点移回时,矩形应该是翻转的,但我的矩形不是这样做,而是继续在鼠标光标的相反方向上绘制。简而言之,此代码在向前方向绘制矩形时工作正常,但在向后方向上不起作用。