2013-10-22 50 views
2

我正在使用下面的代碼來設置MdiParent窗體的背景圖像,並且它運行良好,但是當我點擊最大化按鈕比BackgroundImage重複在右側和底部邊緣(即右側和底側圖像部分重複),我該如何避免這種情況並正確顯示圖像?正確設置MdiParent背景圖像

public Parent() 
{ 
    InitializeComponent(); 

    foreach (Control ctl in this.Controls) 
    { 
     if (ctl is MdiClient) 
     { 
      ctl.BackgroundImage = Properties.Resources.bg; 
      ctl.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; 
      break; 
     } 
    } 
} 

回答

6
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; 

this點的形成。

我自己也注意到你提到的同樣的行爲。這似乎只是一個繪畫問題。添加下面的代碼來修復它。

protected override void OnSizeChanged(EventArgs e) 
{ 
    base.OnSizeChanged(e); 
    this.Refresh(); 
} 
+0

想你的解決方案還是同樣的問題,堅持自己的 – Durga

+0

@Durga更新我的回答 –

2

MdiClient.BackgroundImageLayout是不相關的類MdiClient(由MSDN文檔頁說明)。你應該嘗試一些解決方法。其中一個解決辦法是油漆和backgroundImage自己

MdiClient client = Controls.OfType<MdiClient>().First(); 
client.Paint += (s, e) => { 
    using(Image bg = Properties.Resources.bg){ 
    e.Graphics.DrawImage(bg, client.ClientRectangle); 
    } 
}; 
//Set this to repaint when the size is changed 
typeof(Control).GetProperty("ResizeRedraw", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) 
       .SetValue(client, true, null); 
//set this to prevent flicker 
typeof(Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) 
       .SetValue(client, true, null); 
+0

的圖像需要處置。 –

+0

@HansPassant謝謝,我不知道應該如何處理圖像。你可以查看我更新的代碼嗎? –

+0

@HansPassant圖像正在處理中。 「使用」語句在完成運行後處理該對象 –