c# - 在 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;
}
}
解决方案
所讨论的绘图画布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();
}
此时,您将得到如下绘制的内容:
现在是第二部分。您交换矩形的方法:
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();
}
}
...你会得到:
推荐阅读
- java - PostUpdate 挂钩不起作用,同时进行批量更新
- javascript - 以角度使用引导模板中的脚本
- c# - 如何将列表转换为本机类型以在存储过程中使用?
- c# - 在c#中获取所有属性及其值作为字符串
- excel - 如何使用 powershell 将 xlsx 文件转换为 csv 文件?
- r - 使用 ggplot2 作为函数作为底图添加栅格
- ios - 具有多个组件的 UIPickerView 使应用程序崩溃
- flutter - 允许在录制视频期间播放音乐,例如 snapchat flutter
- c# - C# code being optimized in Debug mode with Optimize code unchecked
- javascript - React Flow showing Cannot resolve module error for create-react-app