首页 > 解决方案 > 在 Windows 窗体应用程序中刷新画布的问题

问题描述

我正在尝试在 c# 中进行排序可视化算法,但我在画布刷新时遇到了问题。

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

在这张图片中,您可以看到我想从画布中删除的黑色矩形

图片

这是我的代码:

    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;
    }

}

标签: c#.netwinformsdrawing

解决方案


所讨论的绘图画布MyCanvas无论是 PictureBox 还是 Panel 还是 Form 本身,都为绘图例程提供特定事件,尤其是Paint在此上下文中的事件。该事件有一个PaintEventArgs提供免费 Graphics对象来绘制您的绘图。这意味着,您不需要像在您的方法中那样创建额外的Graphics对象。draw现在让我们绘制这些矩形。

类级别字段:

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;

处理控件的Paint事件MyCanvas

    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()绘图画布的方法。

    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();
    }

此时,您将得到如下绘制的内容:

SOQ61443730A

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

    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();
    }
}

...你会得到:

SOQ61443730C


推荐阅读