2

我正在使用下面的代码来设置 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;
        }
    }
}
4

3 回答 3

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

this指向表格。

我自己也注意到了你提到的同样的行为。这似乎只是一个绘画问题。添加以下代码来修复它。

protected override void OnSizeChanged(EventArgs e)
{
    base.OnSizeChanged(e);
    this.Refresh();
}
于 2013-10-22T14:18:07.763 回答
3

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);
于 2013-10-22T14:23:57.390 回答
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
于 2018-10-09T19:18:53.187 回答