所以我试图允许用户在表单上绘制框,当我从左上角到右下角绘制时,一切顺利。一旦X或Y位置变得小于原始位置,它就会停止绘制并且矩形消失。
我对paint方法进行了一些更改,试图调整它的位置并将其移动到应该模拟此行为的位置,但它只是无法正常工作。有没有更好的方法来解决这个问题?我可以在屏幕上获得多个矩形,甚至双击以检测并删除它们。除了右下角
之外,我无法让他们在任何其他方向画画 // All rectangles saved to the form
private List<Rectangle> rects;
// Current rectangle if one is being drawn
private Rectangle tempRect;
public Form1()
{
InitializeComponent();
rects = new List<Rectangle>();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
tempRect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int tempX = tempRect.X;
int tempY = tempRect.Y;
int tempWidth = tempRect.Width;
int tempHeight = tempRect.Height;
if (e.X < tempRect.Location.X)
{
tempX = e.X;
tempWidth = tempRect.Width + (tempRect.Location.X - e.X);
}
else
tempWidth = e.X - tempRect.Location.X;
if (e.Y < tempRect.Location.Y)
{
tempY = e.Y;
tempHeight = tempRect.Height + (tempRect.Location.Y - e.Y);
}
else
tempHeight = e.Y - tempRect.Location.Y;
Point rectLocation = new Point(tempX, tempY);
Size rectSize = new Size(tempWidth, tempHeight);
tempRect = new Rectangle(rectLocation, rectSize);
this.Invalidate();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
// Must be within constraint, prevents tiny invisible rectangles from being added
if (tempRect.Width >= 10 && tempRect.Height >= 10)
rects.Add(tempRect);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Black, 2))
{
// Redraws all existing rectangles onto the form
foreach (Rectangle rect in rects)
e.Graphics.DrawRectangle(pen, rect);
// Must be within constraint, prevents tiny invisible rectangles from being added
if (tempRect.Width >= 10 && tempRect.Height >= 10)
e.Graphics.DrawRectangle(pen, tempRect);
}
}
答案 0 :(得分:3)
您可以显着简化代码。您可以创建tempRect
,而不是在鼠标停用时创建tempStartPoint
。这样,在MouseMove
事件处理程序中就不需要那么多操作了,所有代码都归结为一个语句:
tempRect =
new Rectangle(
Math.Min(tempStartPoint.X, tempEndPoint.X),
Math.Min(tempStartPoint.Y, tempEndPoint.Y),
Math.Abs(tempStartPoint.X - tempEndPoint.X),
Math.Abs(tempStartPoint.Y - tempEndPoint.Y));
完整代码:
public partial class Form1 : Form
{
private readonly List<Rectangle> rects = new List<Rectangle>();
private Point tempStartPoint;
private Rectangle tempRect;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
tempStartPoint = e.Location;
Invalidate();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
Point tempEndPoint = e.Location;
tempRect =
new Rectangle(
Math.Min(tempStartPoint.X, tempEndPoint.X),
Math.Min(tempStartPoint.Y, tempEndPoint.Y),
Math.Abs(tempStartPoint.X - tempEndPoint.X),
Math.Abs(tempStartPoint.Y - tempEndPoint.Y));
Invalidate();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
// Must be within constraint, prevents tiny invisible rectangles from being added
if (tempRect.Width >= 10 && tempRect.Height >= 10)
rects.Add(tempRect);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Black, 2))
{
// Redraws all existing rectangles onto the form
foreach (Rectangle rect in rects)
e.Graphics.DrawRectangle(pen, rect);
// Must be within constraint, prevents tiny invisible rectangles from being added
if (tempRect.Width >= 10 && tempRect.Height >= 10)
e.Graphics.DrawRectangle(pen, tempRect);
}
}
}