【问题标题】:Looking for a better way to change parent form's size寻找一种更好的方法来改变父窗体的大小
【发布时间】:2008-12-08 18:53:40
【问题描述】:

我有一个简单的应用程序,它显示拖到它上面的图片。我希望应用程序根据它显示的图片调整自身大小。下面的代码就是这样做的:

// Load the picture
Bitmap picture = new Bitmap(s);

// Calculate the size of the main form
this.Size = new Size(picture.Width + 
                       (this.pictureBox.Padding.All + 
                        this.pictureBox.Margin.All + 
                        this.tableLayoutPanel.Padding.All + 
                        this.tableLayoutPanel.Margin.All) * 2, 
                     picture.Height + 
                       (int)this.tableLayoutPanel.RowStyles[1].Height + 
                       (this.pictureBox.Padding.All + 
                        this.pictureBox.Margin.All + 
                        this.tableLayoutPanel.Padding.All + 
                        this.tableLayoutPanel.Margin.All) * 2);
 // Display the file.
 this.pictureBox.Image = picture;

它认为我需要一些帮助来改进这一点是相当明显的。随着表格变得越来越复杂,适当尺寸的计算也将变得越来越复杂。 有什么建议吗?

【问题讨论】:

    标签: .net winforms c#-2.0


    【解决方案1】:

    您可以查看表单属性:

    • Form.AutoSize
    • Form.AutoSizeMode

    这些,加上设置 PictureBox 的 AutoSizeMode 应该会给你想要的效果(无需编写任何代码)。

    【讨论】:

    • 不知道 AutoSize 的东西...当然这似乎是最好的方式。
    【解决方案2】:

    您可以计算旧图片和新图片之间的大小差异,然后将表单的大小调整该数量...只要表单上的所有其他内容保持相同大小即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多