gpt4 book ai didi

c# - 当位置从左上角移动到初始位置时,如何在 C# 中绘制框?

转载 作者:行者123 更新时间:2023-11-30 22:37:37 25 4
gpt4 key购买 nike

所以我试图让用户在表单上绘制框,当我从左上角到右下角绘制时一切顺利。一旦 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);
}
}

最佳答案

您可以显着简化您的代码。您可以创建 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);
}
}
}

关于c# - 当位置从左上角移动到初始位置时,如何在 C# 中绘制框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6419694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com