在.net中更改MDI容器表单的背景颜色或背景图像

时间:2010-03-30 05:06:04

标签: c# .net mdiparent

我需要在我的应用程序中更改mdi父级的背景颜色或背景图像。我尝试更改背景颜色或指定背景图像,它将无法正常工作。我也尝试循环窗体中的控件来获取mdiclient并更改其背景颜色,也是同样的结果。

5 个答案:

答案 0 :(得分:1)

也许这会有所帮助? http://support.microsoft.com/kb/319465

答案 1 :(得分:1)

Private ClientControl As MdiClient

    Public Sub New()
        InitializeComponent()

        ClientControl = Nothing
        For Each Ctl As Control In Me.Controls
            ClientControl = TryCast(Ctl, MdiClient)
            If ClientControl IsNot Nothing Then Exit For
        Next
    End Sub

'iN FORM LOAD

ClientControl.BackColor = Color.Cyan

答案 2 :(得分:0)

如果您正在使用简单的颜色,请尝试以下代码,如果您尝试设置图像,则可以将BackgroundImage与BackgroundImageLayout一起使用

 MdiClient ctlMDI;
            foreach (Control ctl in this.Controls)
            {
                try
                {
                    ctlMDI = (MdiClient)ctl;

                    // Set the BackColor of the MdiClient control.
                    ctlMDI.BackColor = Color.DarkRed;
                }
                catch (InvalidCastException exc)
                {
                    // Catch and ignore the error if casting failed.
                }
            }

答案 3 :(得分:0)

试试这个,它有效。

foreach (Control control in this.Controls)
{

    // #2
    MdiClient client = control as MdiClient;
    if (!(client == null))
    {
        // #3
        client.BackColor = GetYourColour();
        // 4#
        break;
    }

}

答案 4 :(得分:0)

尝试一下

  Public Sub MDIBGColor()
        Dim ctl As Control
        Dim ctlMDI As MdiClient

        ' Loop through all of the form's controls looking
        ' for the control of type MdiClient.
        For Each ctl In Me.Controls
            Try

                ' Attempt to cast the control to type MdiClient.
                ctlMDI = CType(ctl, MdiClient)

                ' Set the BackColor of the MdiClient control.
                ctlMDI.BackColor = Me.BackColor

            Catch exc As InvalidCastException
                ' Catch and ignore the error if casting failed.
            End Try
        Next

    End Sub

并在表单加载事件上调用sub