2013-10-22 49 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图像正在处理中。 “使用”语句在完成运行后处理该对象 –