在 windows 表单应用程序中刷新 canvas 时出现问题

Problem with refreshing canvas in windows form application

我正在尝试在 C# 中制作排序可视化算法,但 canvas 刷新出现问题。

我每次重绘时都尝试刷新 canvas 但它看起来不太好。我确定还有另一种方法可以做到这一点,我希望有人能帮助我。

在此图片中,您可以看到我要从 canvas

中删除的黑色矩形

这是我的代码:

    private void GenerateArrayButton_Click(object sender, EventArgs e)
    {
        MyCanvas.Refresh();
        Random random = new Random();
        int xPosition = 0 , yPosition = MyCanvas.Height/2; 
        const int k_RectangleWight = 2;

        for(int i = 0; i < k_SizeOfArray; i++)
        {
            int rectangleHeight = random.Next(MyCanvas.Height / 2);
            m_UnsortedArray[i] = new Rectangle(xPosition,yPosition, k_RectangleWight, rectangleHeight);
            xPosition += 5;
        }

        draw(m_UnsortedArray, Pens.Black);

    }

    private void draw(Rectangle[] i_ArrayToDraw, Pen i_PenColor)
    {
        var graphics = MyCanvas.CreateGraphics();
        graphics.DrawRectangles(i_PenColor, i_ArrayToDraw);
        graphics.Dispose();
    }

    private void SortingButton_Click(object sender, EventArgs e)
    {
        bubbleSort();
        draw(m_UnsortedArray, Pens.Green);
    }

    private void bubbleSort()
    {
        for(int i = 0; i < m_UnsortedArray.Length; i++)
        {
            for(int j = 0; j < m_UnsortedArray.Length - 1; j++)
            {
                if(m_UnsortedArray[j].Height > m_UnsortedArray[j + 1].Height)
                {
                    swap(ref m_UnsortedArray[j], ref m_UnsortedArray[j+1]);
                }
            }
            draw(m_UnsortedArray,Pens.Black);
        }
    }

    private void swap(ref Rectangle i_Rectangle1, ref Rectangle i_Rectangle2)
    {
        // Swap the position of the rectangle
        var location = i_Rectangle1.Location;
        i_Rectangle1.Location = i_Rectangle2.Location;
        i_Rectangle2.Location = location;

        // Swap the position of the current rectangle in the array
        var copyRect = i_Rectangle1;
        i_Rectangle1 = i_Rectangle2;
        i_Rectangle2 = copyRect;
    }

}

有问题的绘图 canvas MyCanvas 无论是 PictureBox 还是 Panel 或 Form 本身,都为绘画例程提供了特定事件,尤其是在此上下文中的 Paint 事件.该活动有一个 PaintEventArgs,它提供了一个 免费 Graphics 对象来进行绘图。意思是,您不需要像在 draw 方法中那样创建额外的 Graphics 对象。现在让我们画出那些矩形。

Class 级别字段:

using System.Threading.Tasks;
//...

public partial class Form1 : Form
{
    private const int k_RectangleWight = 2;
    private const int k_SizeOfArray = 100; //assign the right value.

    private Rectangle[] m_UnsortedArray;
    Random rand = new Random();
    private Pen MyPen;

处理 MyCanvas 控件的 Paint 事件。

    public Form1()
    {
        InitializeComponent();

        //You can add normal event handler instead if you prefer so.
        MyCanvas.Paint += (s, e) =>
        {
            if (MyPen != null)
                e.Graphics.DrawRectangles(MyPen, m_UnsortedArray);
        };
    }

GenerateArrayButton_Click事件中,创建矩形,分配绘图笔,调用绘图的Invalidate()方法canvas。

    private void GenerateArrayButton_Click(object sender, EventArgs e)
    {
        m_UnsortedArray = new Rectangle[k_SizeOfArray];
        var xPosition = 0;
        var yPosition = MyCanvas.Height / 2;
        for(var i = 0; i < k_SizeOfArray; i++)
        {
            var rectangleHeight = rand.Next(MyCanvas.Height / 2);
            m_UnsortedArray[i] = new Rectangle(
                xPosition, 
                yPosition, 
                k_RectangleWight, 
                rectangleHeight);
            xPosition += 5;
        }
        MyPen = Pens.Black;
        MyCanvas.Invalidate();
    }

这时候你会得到这样的图:

现在是第二部分。您交换矩形的方法:

    private async void bubbleSort()
    {
        for (int i = 0; i < m_UnsortedArray.Length; i++)
        {
            for (int j = 0; j < m_UnsortedArray.Length - 1; j++)
                if (m_UnsortedArray[j].Height > m_UnsortedArray[j + 1].Height)
                    swap(ref m_UnsortedArray[j], ref m_UnsortedArray[j + 1]);
            await Task.Delay(30);
            MyCanvas.Invalidate();
        }
    }

    private void swap(ref Rectangle i_Rectangle1, ref Rectangle i_Rectangle2)
    {
        var location = i_Rectangle1.Location;
        i_Rectangle1.Location = i_Rectangle2.Location;
        i_Rectangle2.Location = location;

        var copyRect = i_Rectangle1;
        i_Rectangle1 = i_Rectangle2;
        i_Rectangle2 = copyRect;
    }

SortingButton的点击事件中,你只需要:

    private void SortingButton_Click(object sender, EventArgs e)
    {
        MyPen = Pens.Green;
        bubbleSort();
    }
}

...你会得到: