引言
很多时候。我们的窗体设计需要一个引导窗体。当打开一个项目的窗体时,默认的是先打开一个欢迎或介绍项目信息的引导窗体,几秒钟后再打开项目的主窗体。这几秒时间最重要的意义是等待主窗体加载程序完毕。
1.设计操作流程
实现带引导窗体的窗体设计操作流程:在项目里先设计两个窗体,并把Form1改名为Frm_Main,把Form2改名为Frm_Start。切记在更改窗体名称的时候按下ENTER确认此更改影响到了整个工程。
接着,把项目所需要的图片资源设计到图片资源管理器,并把图片设计为相应窗体的背景,stretch。这一步的操作,详见本文作者写的其他文章。
进一步地,在Frm_Main窗体创建Load事件,在事件里定义Frm_Start的对象,并打开Frm_Start窗体。
进一步地,在Frm_Start窗体创建Load事件、设计Timer组件,并设计一个Tick事件,再创建一个FormClosed事件;在其Load事件中启动定时器,设置定时器动作时间为10s,在定时器的Tick事件中设计为关闭窗体引导窗体;在其FormClosed事件设计为关闭定时器;
好了,至此符合项目需要的操作流程设计完毕,下面上代码。
2.实例
(1)Resources.Designer.cs
//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.42000 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ namespace _193.Properties { using System; /// <summary> /// 一个强类型的资源类,用于查找本地化的字符串等。 /// </summary> // 此类是由 StronglyTypedResourceBuilder // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen // (以 /str 作为命令选项),或重新生成 VS 项目。 [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { private static global::System.Resources.ResourceManager resourceMan; private static global::System.Globalization.CultureInfo resourceCulture; [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal Resources() { } /// <summary> /// 返回此类使用的缓存的 ResourceManager 实例。 /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_193.Properties.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; } } /// <summary> /// 重写当前线程的 CurrentUICulture 属性,对 /// 使用此强类型资源类的所有资源查找执行重写。 /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Globalization.CultureInfo Culture { get { return resourceCulture; } set { resourceCulture = value; } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap C_编程词典 { get { object obj = ResourceManager.GetObject("C_编程词典", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap start { get { object obj = ResourceManager.GetObject("start", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } } }
(2)Frm_Main.Designer.cs
namespace _193 { partial class Frm_Main { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { SuspendLayout(); // // Frm_Main // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; BackgroundImage = Properties.Resources.C_编程词典; BackgroundImageLayout = ImageLayout.Stretch; ClientSize = new Size(367, 238); Name = "Frm_Main"; Text = "软件主界面"; Load += Frm_Main_Load; ResumeLayout(false); } #endregion } }
(3)Frm_Main.cs
namespace _193 { public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent(); } private void Frm_Main_Load(object sender, EventArgs e) { Frm_Start frm = new(); frm.ShowDialog(); } } }
(4)Frm_Start.Designer.cs
namespace _193 { partial class Frm_Start { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { components = new System.ComponentModel.Container(); timer1 = new System.Windows.Forms.Timer(components); SuspendLayout(); // // timer1 // timer1.Tick += Timer1_Tick; // // Frm_Start // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; BackgroundImage = Properties.Resources.start; BackgroundImageLayout = ImageLayout.Stretch; ClientSize = new Size(368, 240); Name = "Frm_Start"; Text = "启动界面"; FormClosed += Frm_Start_FormClosed; Load += Frm_Start_Load; ResumeLayout(false); } #endregion private System.Windows.Forms.Timer timer1; } }
(5)Frm_Start.cs
namespace _193 { public partial class Frm_Start : Form { public Frm_Start() { InitializeComponent(); } /// <summary> /// 启动计时器,10s计时 /// </summary> private void Frm_Start_Load(object sender, EventArgs e) { timer1.Start(); timer1.Interval = 10000; } /// <summary> /// 关闭启动窗体 /// </summary> private void Timer1_Tick(object sender, EventArgs e) { Close(); } /// <summary> /// 关闭计时器 /// </summary> private void Frm_Start_FormClosed(object sender, FormClosedEventArgs e) { timer1.Stop(); } } }