当位置从左上角移动到初始位置时,我如何在C#中绘制方框
本文关键字:位置 方框 绘制 移动 左上角 | 更新日期: 2023-09-27 17:59:52
所以我试图允许用户在表单上绘制框,当我从左上到右下绘制时,一切都很顺利。一旦X或Y位置变得小于原始位置,它就停止绘制,矩形就消失了。
我对绘制方法进行了一些更改,试图调整其位置,并将其移动到应该的位置,以模拟这种行为,但它无法正常工作。有更好的方法吗?我可以在屏幕上获得多个矩形,甚至双击可以检测并删除它们。除了右下角的,我无法让他们向任何其他方向画画
// 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);
}
}
您可以显著简化代码。您可以创建tempStartPoint
,而不是在鼠标放下时创建tempRect
。这样,您就不需要在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);
}
}
}