我使用下面的代码来设置MdiParent表单的背景图像,它运行良好,但是当我点击最大化按钮时,BackgroundImage在右侧和底部边缘重复(即右侧和底侧图像部分重复) ,我该如何避免这种情况并正确显示图像?
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;
}
}
}
答案 0 :(得分:6)
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this
指向表单。
我自己也注意到了你提到的同样的行为。这似乎只是一个绘画问题。添加以下代码来修复它。
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.Refresh();
}
答案 1 :(得分: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);
答案 2 :(得分:0)
Private Sub Frmmain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
For Each ctl As Control In Me.Controls
If TypeOf ctl Is MdiClient Then
ctl.BackgroundImage = Me.BackgroundImage
End If
Next ctl
Me.BackgroundImageLayout = ImageLayout.Zoom
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Frmmain_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
Try
Me.Refresh()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub