Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改MDI表单的背景_C#_Visual Studio_Winforms_Mdi - Fatal编程技术网

C# 更改MDI表单的背景

C# 更改MDI表单的背景,c#,visual-studio,winforms,mdi,C#,Visual Studio,Winforms,Mdi,如何在C#中更改MDI窗体的背景色 我使用背景色属性更改了它,但颜色没有更改 执行此任务应该怎么做?MDI控件的实际背景颜色基于Windows当前主题中的颜色。您必须在WinForm中物理设置MdiClient控件的背景 // #1 foreach (Control control in this.Controls) { // #2 MdiClient client = control as MdiClient; if (!

如何在C#中更改MDI窗体的背景色

我使用背景色属性更改了它,但颜色没有更改


执行此任务应该怎么做?

MDI控件的实际背景颜色基于Windows当前主题中的颜色。您必须在WinForm中物理设置MdiClient控件的背景

    // #1
    foreach (Control control in this.Controls)
    {
        // #2
        MdiClient client = control as MdiClient;
        if (!(client == null))
        {
            // #3
            client.BackColor = GetYourColour();
            // 4#
            break;
        }
    }
编辑-添加评论:

  • 我们需要遍历MdiParent窗体中的控件,以查找在将窗体设置为MdiParent时添加的MdiClient控件。Foreach只是通过集合对类型进行的简单迭代

  • 我们需要在表单中找到MdiClient控件,为此,我们使用'as'关键字在循环中强制转换当前控件。使用'as'关键字意味着,如果强制转换无效,则设置的变量将为null。因此,我们检查“client”是否为null。如果是,则循环中的当前控件不是MdiClient控件。只要变量'client'不为null,那么我们掌握的控件就是MdiClient,我们可以设置它的背景颜色

  • 将背景颜色设置为您想要的任何颜色。只需将“GetYourColour()”替换为您想要的任何颜色,即Color.White、Color.Blue、Color.FromArgb(等)

  • 因为只有一个MdiClient,所以继续循环没有意义,因为这只是浪费处理时间。因此,我们调用“break”退出循环


  • 如果您还需要其他解释,请告诉我。

    将此内容写入MDI表单的加载方法中

    Controls.OfType<MdiClient>().FirstOrDefault().BackColor = Color.Purple;
    
    控制.OfType().FirstOrDefault().BackColor=Color.Purple;
    
    谢谢您的回复。在您的编码帮助下,我现在可以更改MDI表单的背景色。我还需要一个帮助。因为我是C#新手,如果可能的话,你能解释一下代码吗?谢谢!!非常感谢你!!很好的解释!!谢谢你,我更喜欢:
    foreach(这个.Controls中的控件){if(控件是MdiClient){Control.BackColor=Color.Red;break;}