winform中動態生成按鈕時速度慢,原因是winform窗體設置了背景圖。
解決方案:
在窗體中添加一個PictureBox
,設置其Dock
屬性爲Fill
,設置Image
而不是BackgroundImage
,並置於底層,然後將窗體中的控件的Parent
設置該PictureBox
。
如下:
public GeneratedButtonForm()
{
PictureBox pictureBox1 = new PictureBox();
pictureBox1.Name = "test";
pictureBox1.Image = Properties.Resources._11;
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.BackColor = Color.Transparent;
pictureBox1.SendToBack();
this.Controls.Add(pictureBox1);
while (this.Controls.Count>1)
{
if (Controls[0].Name != "test")
{
Controls[0].Parent = pictureBox1;
}
}
}