2008-12-08 97 views
2

我有一个简单的应用程序,显示图片拖动到它。我希望应用程序根据它显示的图片来调整自己的大小。代码如下:寻找更好的方式来改变父窗体的大小

// 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; 

它认为这是相当明显的,我希望有一些帮助改善这一点。随着表格变得更加复杂,计算适当的大小也会变得更加复杂。 建议?

回答

3

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

4

你可以看看窗体属性:

  • Form.AutoSize
  • Form.AutoSizeMode

这些,再加上设置图片框的AutoSizeMode应该给你的影响,你”重新寻找(不必编写任何代码)。

+0

不知道AutoSize的东西...当然,这似乎是最好的方式去这里。 – 2008-12-09 20:47:10

相关问题