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

当位置从左上方移动到初始位置时,如何在C#中绘制方框?,c#,.net,winforms,graphics,drawing,C#,.net,Winforms,Graphics,Drawing,因此,我尝试允许用户在表单上绘制框,当我从左上角到右下角绘制框时,一切都很顺利。一旦X或Y位置小于原始位置,则停止绘制,矩形消失 我对paint方法做了一些更改,试图调整它的位置,并将它移动到应该的位置,以模拟这种行为,但它无法正常工作。有没有更好的办法?我可以在屏幕上看到多个矩形,甚至双击来检测和删除它们。除了右下角,我没法让他们朝其他方向画画 // All rectangles saved to the form private List<Rectangle> r

因此,我尝试允许用户在表单上绘制框,当我从左上角到右下角绘制框时,一切都很顺利。一旦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);
        }
    }
//保存到表单中的所有矩形
私有列表矩形;
//当前矩形(如果正在绘制)
私有矩形tempRect;
公共表格1()
{
初始化组件();
rects=新列表();
}
私有void Form1\u MouseDown(对象发送方,MouseEventArgs e)
{
tempRect=新矩形(e.X,e.Y,0,0);
这个。使无效();
}
私有void Form1\u MouseMove(对象发送方,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=10&&tempRect.Height>=10)
rects.Add(tempRect);
}
私有void Form1_Paint(对象发送器、PaintEventArgs e)
{
使用(钢笔=新钢笔(颜色:黑色,2))
{
//将所有现有矩形重新绘制到窗体上
foreach(矩形中的矩形)
e、 绘图矩形(钢笔、矩形);
//必须在约束范围内,以防止添加微小的不可见矩形
如果(tempRect.Width>=10&&tempRect.Height>=10)
e、 图形。DrawRectangle(画笔、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);
        }
    }
}
公共部分类表单1:表单
{
私有只读列表rects=新列表();
专用点临时开始点;
私有矩形tempRect;
公共表格1()
{
初始化组件();
}
私有void Form1\u MouseDown(对象发送方,MouseEventArgs e)
{
tempStartPoint=e.位置;
使无效();
}
私有void Form1\u MouseMove(对象发送方,MouseEventArgs e)
{
if(e.按钮!=鼠标按钮左)
返回;
点端点=e.位置;
坦普雷特=
新矩形(
Math.Min(tempStartPoint.X,tempEndPoint.X),
Math.Min(tempStartPoint.Y,tempEndPoint.Y),
Abs(tempStartPoint.X-tempEndPoint.X),
Abs(tempStartPoint.Y-tempEndPoint.Y));
使无效();
}
私有void Form1_MouseUp(对象发送方,MouseEventArgs e)
{
//必须在约束范围内,以防止添加微小的不可见矩形
如果(tempRect.Width>=10&&tempRect.Height>=10)
rects.Add(tempRect);
}
私有void Form1_Paint(对象发送器、PaintEventArgs e)
{
使用(钢笔=新钢笔(颜色:黑色,2))
{
//将所有现有矩形重新绘制到窗体上
foreach(矩形中的矩形)
e、 绘图矩形(钢笔、矩形);
//必须在约束范围内,以防止添加微小的不可见矩形
如果(tempRect.Width>=10&&tempRect.Height>=10)
e、 图形。DrawRectangle(画笔、tempRect);
}
}
}

阅读起来也容易多了,不知道我的bug在哪里,但这样分解它会使它更容易维护,而且所有的工作都像一个符咒:)