Windows窗体应用程序中刷新画布的问题

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

我正在尝试使用C#进行排序可视化算法,但是画布刷新出现问题。

每次重画时,我都试图刷新画布,但是看起来不太好。我敢肯定还有另一种方法,希望有人能帮助我。

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

pic

这是我的代码:

    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# .net winforms drawing
1个回答
0
投票

有问题的绘图画布MyCanvas是图片框,面板还是窗体本身,为绘画例程提供了特定的事件,尤其是在这种情况下的Paint事件。该事件有一个PaintEventArgs,它提供一个free Graphics对象来绘制图形。意思是,您不需要像在Graphics方法中那样创建额外的draw对象。现在让我们绘制这些矩形。

类级别字段:

public partial class YourForm : 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 YourForm()
    {
        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 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]);
    }

    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)
    {
        bubbleSort();
        MyPen = Pens.Green;
        MyCanvas.Invalidate();
    }
}

...,您将获得:

SOQ61443730B

© www.soinside.com 2019 - 2024. All rights reserved.