更改MDI表单的背景 [英] Change Background of an MDI Form

查看:121
本文介绍了更改MDI表单的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中更改MDI表单的背景颜色?

How can I change the BACKGROUND color of the MDI FORM in C#?

我使用背景颜色属性对其进行了更改,但颜色未更改.

I changed it using the background color property but the color is not changed.

我应该怎么做才能执行此任务?

What should I do to perform this task?

推荐答案

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

The actual BackGround colour of the MDI control is based on the colour in the Windows current Theme. You have to physically set the MdiClient control's background inside the WinForm.

    // #1
    foreach (Control control in this.Controls)
    {
        // #2
        MdiClient client = control as MdiClient;
        if (!(client == null))
        {
            // #3
            client.BackColor = GetYourColour();
            // 4#
            break;
        }
    }

编辑-添加评论:

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

  1. We need to loop through the controls in the MdiParent form to find the MdiClient control that gets added when you set the Form to be an MdiParent. Foreach is just a simple iteration of a type through a collection.

我们需要在表单中找到MdiClient控件,因此,我们使用'as'关键字将当前控件投射到循环中.使用'as'关键字意味着,如果强制类型转换无效,则所设置的变量将为null.因此,我们检查"client"是否为空.如果是这样,则循环中的当前控件不是MdiClient控件.一旦变量'client'不为null,那么我们拥有的控件就是MdiClient,我们可以设置其背景色.

We need to find the MdiClient control within the form, so to do this we cast the current control within the loop using the 'as' keyword. Using the 'as' keyword means that if the cast is invalid then the variable being set will be null. Therefore we check to see if 'client' is null. If it is, the current control in the loop is not the MdiClient control. As soon as the variable 'client' is not null, then the control we've got hold of is the MdiClient and we can set its background colour.

将背景色设置为所需的任何颜色.只需将"GetYourColour()"替换为所需的任何颜色,即Color.White,Color.Blue,Colour.FromArgb(etc)...

Set the backcolour to anything you want. Just replace "GetYourColour()" with whatever colour you want, i.e. Color.White, Color.Blue, Colour.FromArgb(etc)...

由于只有1个MdiClient,因此继续循环是没有意义的,因为这只是在浪费处理时间.因此,我们称中断"退出循环.

As there is only ever 1 MdiClient, there's no point continuing the loop as it's just a waste of processing time. Therefore we call 'break' to exit the loop.

如果您需要其他说明,请告诉我.

Let me know if you want anything else explaining.

这篇关于更改MDI表单的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆