2008-12-08 109 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

相關問題