gpt4 book ai didi

c# - 在 C# 中向面板添加控件时出现性能问题

转载 作者:行者123 更新时间:2023-11-30 15:43:42 25 4
gpt4 key购买 nike

我正在创建一个面板,然后向其添加一些标签/按钮以形成一个网格。问题是,如果我向面板中添加超过 25x25 个项目,则会严重影响性能。我可以调整表单大小,但是当我滚动面板以查看程序滞后的所有标签时,标签/按钮撕裂或闪烁,有时它会使程序无响应。我尝试将控件添加到我创建的“DoubleBufferedPanel”中。这似乎没有效果。我还能做什么?抱歉有这么大的代码 list 。我不想浪费任何人的时间。

namespace GridTest
{
public partial class Form1 : Form
{
private const int COUNT = 50;
private const int SIZE = 50;
private Button[,] buttons = new Button[COUNT, COUNT];
private GridPanel pnlGrid;

public Form1()
{
InitializeComponent();

pnlGrid = new GridPanel();
pnlGrid.AutoScroll = true;
pnlGrid.Dock = DockStyle.Fill;
pnlGrid.BackColor = Color.Black;

this.Controls.Add(pnlGrid);
}

private void Form1_Load(object sender, EventArgs e)
{
int x = 0;
int y = 0;
int offset = 1;

for (int i = 0; i < COUNT; i++)
{
for (int j = 0; j < COUNT; j++)
{
buttons[i, j] = new Button();
buttons[i, j].Size = new Size(SIZE, SIZE);
buttons[i, j].Location = new Point(x, y);
buttons[i, j].BackColor = Color.White;

pnlGrid.Controls.Add(buttons[i, j]);

x = x + SIZE + offset;
}

x = 0;
y = y + SIZE + offset;
}
}
}
}

此外,GridPanel 类:

namespace GridTest
{
public class GridPanel : Panel
{
public GridPanel()
: base()
{
this.DoubleBuffered = true;
this.ResizeRedraw = false;
}
}
}

最佳答案

如果您必须在运行时根据某些动态或变化的值添加控件,您可能需要考虑即时创建图像,并在其图片框上捕获鼠标单击事件。这会快得多,并且只需要绘制一个控件而不是数百个控件。您将失去一些按钮功能,例如点击动画和其他自动属性和事件;但您可以在生成图像时重新创建其中的大部分内容。

当二维空间中的位置很重要时,我使用这种技术为用户提供在数千个设备池中打开和关闭单个设备的能力。如果按钮的排列不重要,您最好在 ListView 或组合框中提供项目列表,或者如其他答案所建议的那样,提供带有按钮列的数据 GridView 。

编辑:

显示如何添加带有虚拟按钮的图形的示例。非常基本的实现,但希望您能理解:

首先,一些初始变量作为偏好:

int GraphicWidth = 300;
int GraphicHeight = 100;
int ButtonWidth = 60;
int ButtonHeight = 20;
Font ButtonFont = new Font("Arial", 10F);
Pen ButtonBorderColor = new Pen(Color.Black);
Brush ButtonTextColor = new SolidBrush(Color.Black);

生成图像:

Bitmap ControlImage = new Bitmap(GraphicWidth, GraphicHeight);
using (Graphics g = Graphics.FromImage(ControlImage))
{
g.Clear(Color.White);
for (int x = 0; x < GraphicWidth; x += ButtonWidth)
for (int y = 0; y < GraphicHeight; y += ButtonHeight)
{
g.DrawRectangle(ButtonBorderColor, x, y, ButtonWidth, ButtonHeight);
string ButtonLabel = ((GraphicWidth / ButtonWidth) * (y / ButtonHeight) + x / ButtonWidth).ToString();
SizeF ButtonLabelSize = g.MeasureString(ButtonLabel, ButtonFont);
g.DrawString(ButtonLabel, ButtonFont, ButtonTextColor, x + (ButtonWidth/2) - (ButtonLabelSize.Width / 2), y + (ButtonHeight/2)-(ButtonLabelSize.Height / 2));
}
}
pictureBox1.Image = ControlImage;

并响应图片框的Click事件:

// Determine which "button" was clicked
MouseEventArgs em = (MouseEventArgs)e;
Point ClickLocation = new Point(em.X, em.Y);
int ButtonNumber = (GraphicWidth / ButtonWidth) * (ClickLocation.Y / ButtonHeight) + (ClickLocation.X / ButtonWidth);
MessageBox.Show(ButtonNumber.ToString());

关于c# - 在 C# 中向面板添加控件时出现性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6602316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com