动态创建的PictureBox渲染问题

时间:2019-11-10 13:47:30

标签: c# .net winforms picturebox

最终目标是一个可以玩的记忆游戏。目前,我陷入了渲染问题。我有以下课程:

字段,它是抽象的UserControl

public abstract class Field : UserControl
{
    protected PictureBox _pictureBox;

    public Field()
    {
        _pictureBox = new PictureBox();
        _pictureBox.Image = Properties.Resources.empty;
        _pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
        _pictureBox.BorderStyle = BorderStyle.FixedSingle;
        this.Controls.Add(_pictureBox);
    }

    // ...
    // some abstract methods, not currently important
}

MemoryField (源自Field:

public class MemoryField : Field
{
    public MemoryField(Form parent, int xPos, int yPos, int xSize, int ySize)
    {
        _pictureBox.ClientSize = new Size(xSize, ySize);
        _pictureBox.Location = new Point(xPos, yPos);
        _pictureBox.Parent = parent;
    }

    // ...
}

最后是 MainForm ,这是我的应用程序的入口点:

public partial class MainForm : Form
{
    private readonly int fieldWidth = 100; // 150 no rendering problems at all
    private readonly int fieldHeight = 100;

    public MainForm() { InitializeComponent(); }

    private void MainForm_Load(object sender, EventArgs e)
    {
        for (int y = 0; y < 6; y++) // 6 rows
        {
            for (int x = 0; x < 10; x++) // 10 columns
            {
                Field field = new MemoryField(this, 
                    x * (fieldWidth + 3), // xPos, 3 is for a small space between fields
                    labelTimer.Location.Y + labelTimer.Height + y * (fieldHeight + 3), // yPos
                    fieldWidth, 
                    fieldHeight);

                this.Controls.Add(field);
            }
        }
    }
}

这是我的问题所在: 在这些for循环中,我试图生成一个{x1} s的6x10网格(每个像素包含{x1}} 100x100 px的大小。我几乎成功完成了此操作,因为第二个字段未正确显示: Rendering problem with field size of 100x100 px

只有我发现有效的方法(完全解决了问题)才使字段变大(即150px)。另一方面,将其缩小(即50px)会使问题更大:

Rendering problem with field size of 50x50 px


也许是有用的信息和我尝试过的事情:

  • 我的MainForm是FieldPictureBox
  • 我的MainForm(最初)除AutoSize = true;AutoSizeMode = GrowAndShrink;之外不包含任何组件
  • 我尝试更改{strong>无效的menuStrip属性。
  • 我尝试仅使用label控件(而不使用PictureBox.Image作为PictureBox包装器)来创建网格,从而使尽力起作用。
  • 我尝试将Field放在“有问题的区域”中,该区域确实根据我的确切放置位置来解决该问题。 (因为字段的位置取决于PictureBox的位置和高度)
  • 我尝试重新启动Visual Studio 2017,没有

当然,我可以将大小更改为150px并继续前进,但是我真的很想知道这个问题的根源是什么。谢谢!

1 个答案:

答案 0 :(得分:1)

解决该问题最简单的方法是您已经尝试过-直接使用PictureBox而不是Field。现在,考虑到仅使用Field来包装PictureBox,您可以继承PictureBox而不是仅仅包装它。 如您所知,将您的班级更改为这些班级将解决此问题:

public abstract class Field : PictureBox {

    public Field() {
        Image = Image.FromFile(@"Bomb01.jpg");
        SizeMode = PictureBoxSizeMode.StretchImage;
        BorderStyle = BorderStyle.FixedSingle;
        Size = new Size(100, 100);
    }

    // ...
    // some abstract methods, not currently important
}

public class MemoryField : Field {
    public MemoryField(Form parent, int xPos, int yPos, int xSize, int ySize) {
        ClientSize = new Size(xSize, ySize);
        Location = new Point(xPos, yPos);
    }

    // ...
}

不起作用的真正原因与每个Field及其子组件的大小定位有关。您不应该相对于其父{{1}设置每个Location的{​​{1}},而应相对于其父{{1}来更改_pictureBox的{​​{1}} }}。

您还应该将MemoryField的大小设置为其子Location的大小,否则将无法正确调整其大小以适应其内容。

MemoryField

并将您的创建内循环更改为

Form
相关问题