在窗口窗体应用程序中创建新形状时,现有形状不可见

问题描述 投票:0回答:1

我创建了一个窗口窗体,在这个项目中我有以下代码:

public partial class Form1 : Form
{

    Rectangle _rectangle;
    List<Rectangle> _rectangles = new();
    Point LocationXY;
    Point LocationXYEnd;

    bool MouseIsDown = false;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        MouseIsDown = true;
        LocationXY = e.Location;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (MouseIsDown)
        {
            LocationXYEnd = e.Location;
            Refresh();
        }
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        if (MouseIsDown)
        {

            LocationXYEnd = e.Location;
            MouseIsDown = false;

            _rectangles.Add(GetRectangle());
            
            foreach (var rect in _rectangles)
            {
                System.Drawing.Graphics formGraphics;
                formGraphics = this.CreateGraphics();
                formGraphics.DrawRectangle(new Pen(Color.Black), rect);
                formGraphics.Dispose();
            }

        }
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        if (_rectangles != null)
        {
            e.Graphics.DrawRectangle(Pens.Black, GetRectangle());
        }
    }

    private Rectangle GetRectangle()
    {
        _rectangle = new Rectangle();
        _rectangle.X = Math.Min(LocationXY.X, LocationXYEnd.X);
        _rectangle.Y = Math.Min(LocationXY.Y, LocationXYEnd.Y);
        _rectangle.Width = Math.Abs(LocationXY.X - LocationXYEnd.X);
        _rectangle.Height = Math.Abs(LocationXY.Y - LocationXYEnd.Y);
        return _rectangle;
    }


}

每当我绘制新形状时,现有形状都不可见。当我释放鼠标时,新形状将添加到列表中,并且所有其他形状再次可见。

我尝试解决这个问题,但没有成功。有谁可以帮忙吗?

c# winforms drawing shapes
1个回答
0
投票

您的代码看起来“大部分是正确的”,但我注意到您通过仅绘制新矩形来处理

Paint
事件。事实上,您已经收到了一张带有 e.Graphics
空白画布
,所以请继续重新绘制整个矩形集合。

public partial class MainForm : Form
{
    List<Rectangle> _rectangles = new();
    Rectangle _rectInProgress;
    public MainForm()
    {
        InitializeComponent();
        DoubleBuffered = true;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (_rectangles.Any())
        {
            e.Graphics.DrawRectangles(Pens.Blue, _rectangles.ToArray());
        }
        if (MouseButtons == MouseButtons.Left)
        {
            e.Graphics.DrawRectangle(Pens.Red, _rectInProgress);
        }
    }
    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        _rectInProgress = new Rectangle(e.Location, new Size());
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        if(MouseButtons == MouseButtons.Left)
        {
            _rectInProgress.Width = e.Location.X - _rectInProgress.X;
            _rectInProgress.Height = e.Location.Y - _rectInProgress.Y;
            Invalidate();
        }
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        _rectangles.Add(_rectInProgress);
        Invalidate();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.