我正在使用下面的代码来设置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;
}
}
}发布于 2013-10-22 22:18:08
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;this指向Form。
我自己也注意到了你提到的同样的行为。这似乎只是一个绘画的问题。添加以下代码来修复它。
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.Refresh();
}发布于 2013-10-22 22:23:57
MdiClient.BackgroundImageLayout与类MdiClient无关(如MSDN文档页面所述)。您应该尝试一些变通方法。其中一个解决方法是paint BackgroundImage yourself
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);发布于 2018-10-10 03:18:53
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 Subhttps://stackoverflow.com/questions/19520373
复制相似问题