首页 > 解决方案 > C# 表单 - 动态创建大量按钮/面板/图片框很慢。如何加快速度?

问题描述

由于我不是使用 C# 表单的专家,我希望我能指出正确的方向,因为我运行的特定代码部分运行缓慢,我不知道如何加快速度。

我现在的方式是让用户选择图像和图块大小。

根据图像的尺寸和选定的图块大小,我一直在主图像上方绘制图像。这些图像是平铺大小,带有一个填充主图像尺寸的数字。当我单击它们时,我希望它们像按钮一样。到目前为止,这些图像已为此目的制作成面板。

如果我不必在每次用户更改图像或图块大小时重新计算我需要多少个面板,那也不会那么糟糕。但实际上,它会持续大约半秒,尤其是在较小的瓷砖尺寸下。

换句话说,既然我必须制作很多图像/控件并动态显示它们,你会建议我做什么来提高效率?

到目前为止我所拥有的。

    pictureBox.Controls.Clear();

    //Create our large list of buttons
    for (int y = 0; y < tileCountHeight; y++)
    {
        for (int x = 0; x < tileCountWidth; x++)
        {
            Panel newPicture = new Panel();
            int locX = x * tileSize;
            int locY = y * tileSize;
            newPicture.Location = new Point(locX, locY);

            //Determine the starting image
            int dataIndex = x + (y * tileCountWidth);

            if (passabilityList.Count >= dataIndex)
            {
                passabilityList.Add(Passability.Passable);
            }

            switch (passabilityList[dataIndex])
                {
                    case Passability.Blocked:
                        newPicture.BackgroundImage = SquareImage;
                        break;

                    case Passability.HideBehind:
                        newPicture.BackgroundImage = TriangleImage;
                        break;

                    case Passability.Passable:
                        newPicture.BackgroundImage = CircleImage;
                        break;
                }

                newPicture.Width = tileSize;
                newPicture.Height = tileSize;
                newPicture.Visible = true;
                newPicture.BackgroundImageLayout = ImageLayout.Center;
                newPicture.Name = "picOptionX" + x + "Y" + y;

                pictureBox.Controls.Add(newPicture);                            
                }
            } 

标签: c#.netwinforms

解决方案


推荐阅读