解释此代码的工作原理(C#)
谁能给我解释一下以下代码:解释此代码的工作原理(C#),c#,winforms,C#,Winforms,谁能给我解释一下以下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; namespace web.frmcolor { public class FormEx : Form { /// <summary> /// Set the d
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace web.frmcolor
{
public class FormEx : Form
{
/// <summary>
/// Set the default color for the designer
/// </summary>
static FormEx()
{
_globalBackgroundColor = default(Color?);
}
private static void InvalidateForms()
{
try
{
for (int i1 = 0; i1 < Application.OpenForms.Count; i1++)
{
try
{
FormEx frm = (Application.OpenForms[i1] as FormEx);
if (frm != null)
{
frm.Invalidate(true);
frm.Refresh();
}
}
catch
{
//Should never happen
}
}
}
catch
{
//this will catch if the form count changes
}
}
private static Color? _globalBackgroundColor;
/// <summary>
/// Sets the background color for all forms
/// </summary>
public static Color? GlobalBackgroundColor
{
get { return FormEx._globalBackgroundColor; }
set
{
if (FormEx._globalBackgroundColor != value)
{
FormEx._globalBackgroundColor = value;
InvalidateForms();
}
}
}
public override Color BackColor
{
get
{
return (_globalBackgroundColor == null ? base.BackColor : (Color)_globalBackgroundColor);
}
set
{
base.BackColor = value;
}
}
/// <summary>
/// Create a new colored form
/// </summary>
public FormEx()
: base()
{
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// FormEx
//
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "FormEx";
this.Load += new System.EventHandler(this.FormEx_Load);
this.ResumeLayout(false);
}
private void FormEx_Load(object sender, EventArgs e)
{
}
}
}
为什么在颜色后面有一个
?
,这表示什么?基本上,为您提供了一种非常快速的方法,可以将应用程序中所有窗口的背景更改为通用颜色
重要的部分是私有静态。。。而公众呢
要更改所有打开表单的背景,请执行以下操作:
FormEx.GlobalBackgroundColor=…这里有一些颜色
它将遍历属于应用程序的每个窗口,并更改其背景颜色(基本上无效将强制它重新绘制自身)。基本上,为您提供了一种非常快速的方法,可以将应用程序中所有窗口的背景更改为通用颜色 重要的部分是私有静态。。。而公众呢 要更改所有打开表单的背景,请执行以下操作: FormEx.GlobalBackgroundColor=…这里有一些颜色
它将通过属于应用程序的每个窗口并更改其背景颜色(基本上无效将强制它重新绘制自身)。?意味着颜色应该为空。因为颜色是一个枚举,所以它通常不可为null,它是一个值类型(查看值类型和引用类型的解释)。添加?这意味着就在这段代码中,变量可以设置为
null
。可以在这里找到对该问题的解释。
此外,
default(Color?
语句将变量初始化为Color?
的默认值,该值可能是白色的,或者是因为?,null
?意味着颜色应该为空。因为颜色是一个枚举,所以它通常不可为null,它是一个值类型(查看值类型和引用类型的解释)。添加?这意味着就在这段代码中,变量可以设置为null
。可以在这里找到对该问题的解释。
此外,
default(Color?
语句将变量初始化为默认值Color?
,该值可能为白色,或者是因为?,null
您是否尝试执行该代码?是的,我执行了更改整个应用程序背景的代码。请尝试{FormEx frm=(application.OpenForms[i1]作为FormEx);if(frm!=null){frm.Invalidate(true);frm.Refresh();}}}catch{//永远不会发生}太棒了!!!我讨厌try{}catch{//永远不会发生}呵呵,那么哪个部分不清楚呢?这个部分不清楚_globalBackgroundColor=default(Color?);为什么会有这样的问题?在颜色之后表示什么?您是否尝试执行代码?是的,我执行了更改整个应用程序背景的代码。请尝试{FormEx frm=(application.OpenForms[i1]作为FormEx);if(frm!=null){frm.Invalidate(true);frm.Refresh();}}}catch{//永远不会发生}太棒了!!!我讨厌try{}catch{//永远不会发生}呵呵,那么哪个部分不清楚呢?这个部分不清楚_globalBackgroundColor=default(Color?);为什么会有这样的问题?颜色之后是什么意思?谢谢你的回复。但是为什么我们要使用问号:_globalBackgroundColor=default(Color?);在上面的编码中,问号表示什么?表示它是一个可为空的类型啊。。。呵呵,这是一个可空的。。所以,这意味着特定变量可能为null或包含color类型的值。谢谢您的回复。但是为什么我们要使用问号:_globalBackgroundColor=default(Color?);在上面的编码中,问号表示什么?表示它是一个可为空的类型啊。。。呵呵,这是一个可空的。。所以这意味着特定变量可能为null或包含color类型的值。感谢您的及时回复。那么,for循环需要使用什么呢?for循环遍历它在当前应用程序上下文中可以找到的所有打开的表单,然后将已经打开的表单的背景颜色设置为新选择的颜色。FormEx是从标准.NET Framework派生的类Windows.Forms.Form class.frm.Invalidate和frm.Refresh被调用以强制重新绘制,这意味着表单将再次呈现(因此将显示新的背景色)。感谢您的及时回复。那么,for循环需要使用什么呢?for循环遍历它在当前应用程序上下文中可以找到的所有打开的表单,然后将已经打开的表单的背景颜色设置为新选择的颜色。FormEx是从标准.NET Framework派生的类。调用Windows.Forms.Form class.frm.Invalidate和frm.Refresh可强制重新绘制,这意味着表单将再次呈现(因此将显示新的背景色)
_globalBackgroundColor = default(Color?);