• C#带引导窗体的窗体设计方法:创建特殊窗体


    目录

    1.设计操作流程

    2.实例

    (1)Resources.Designer.cs

    (2)Frm_Main.Designer.cs

    (3)Frm_Main.cs

    (4)Frm_Start.Designer.cs

    (5)Frm_Start.cs

    (6)生成效果


            很多时候。我们的窗体设计需要一个引导窗体。当打开一个项目的窗体时,默认的是先打开一个欢迎或介绍项目信息的引导窗体,几秒钟后再打开项目的主窗体。这几秒时间最重要的意义是等待主窗体加载程序完毕。

    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

    1. //------------------------------------------------------------------------------
    2. //
    3. // 此代码由工具生成。
    4. // 运行时版本:4.0.30319.42000
    5. //
    6. // 对此文件的更改可能会导致不正确的行为,并且如果
    7. // 重新生成代码,这些更改将会丢失。
    8. //
    9. //------------------------------------------------------------------------------
    10. namespace _193.Properties {
    11. using System;
    12. ///
    13. /// 一个强类型的资源类,用于查找本地化的字符串等。
    14. ///
    15. // 此类是由 StronglyTypedResourceBuilder
    16. // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
    17. // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
    18. // (以 /str 作为命令选项),或重新生成 VS 项目。
    19. [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
    20. [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    21. [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    22. internal class Resources {
    23. private static global::System.Resources.ResourceManager resourceMan;
    24. private static global::System.Globalization.CultureInfo resourceCulture;
    25. [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    26. internal Resources() {
    27. }
    28. ///
    29. /// 返回此类使用的缓存的 ResourceManager 实例。
    30. ///
    31. [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    32. internal static global::System.Resources.ResourceManager ResourceManager {
    33. get {
    34. if (object.ReferenceEquals(resourceMan, null)) {
    35. global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_193.Properties.Resources", typeof(Resources).Assembly);
    36. resourceMan = temp;
    37. }
    38. return resourceMan;
    39. }
    40. }
    41. ///
    42. /// 重写当前线程的 CurrentUICulture 属性,对
    43. /// 使用此强类型资源类的所有资源查找执行重写。
    44. ///
    45. [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    46. internal static global::System.Globalization.CultureInfo Culture {
    47. get {
    48. return resourceCulture;
    49. }
    50. set {
    51. resourceCulture = value;
    52. }
    53. }
    54. ///
    55. /// 查找 System.Drawing.Bitmap 类型的本地化资源。
    56. ///
    57. internal static System.Drawing.Bitmap C_编程词典 {
    58. get {
    59. object obj = ResourceManager.GetObject("C_编程词典", resourceCulture);
    60. return ((System.Drawing.Bitmap)(obj));
    61. }
    62. }
    63. ///
    64. /// 查找 System.Drawing.Bitmap 类型的本地化资源。
    65. ///
    66. internal static System.Drawing.Bitmap start {
    67. get {
    68. object obj = ResourceManager.GetObject("start", resourceCulture);
    69. return ((System.Drawing.Bitmap)(obj));
    70. }
    71. }
    72. }
    73. }

    (2)Frm_Main.Designer.cs

    1. namespace _193
    2. {
    3. partial class Frm_Main
    4. {
    5. ///
    6. /// Required designer variable.
    7. ///
    8. private System.ComponentModel.IContainer components = null;
    9. ///
    10. /// Clean up any resources being used.
    11. ///
    12. /// true if managed resources should be disposed; otherwise, false.
    13. protected override void Dispose(bool disposing)
    14. {
    15. if (disposing && (components != null))
    16. {
    17. components.Dispose();
    18. }
    19. base.Dispose(disposing);
    20. }
    21. #region Windows Form Designer generated code
    22. ///
    23. /// Required method for Designer support - do not modify
    24. /// the contents of this method with the code editor.
    25. ///
    26. private void InitializeComponent()
    27. {
    28. SuspendLayout();
    29. //
    30. // Frm_Main
    31. //
    32. AutoScaleDimensions = new SizeF(7F, 17F);
    33. AutoScaleMode = AutoScaleMode.Font;
    34. BackgroundImage = Properties.Resources.C_编程词典;
    35. BackgroundImageLayout = ImageLayout.Stretch;
    36. ClientSize = new Size(367, 238);
    37. Name = "Frm_Main";
    38. Text = "软件主界面";
    39. Load += Frm_Main_Load;
    40. ResumeLayout(false);
    41. }
    42. #endregion
    43. }
    44. }

    (3)Frm_Main.cs

    1. namespace _193
    2. {
    3. public partial class Frm_Main : Form
    4. {
    5. public Frm_Main()
    6. {
    7. InitializeComponent();
    8. }
    9. private void Frm_Main_Load(object sender, EventArgs e)
    10. {
    11. Frm_Start frm = new();
    12. frm.ShowDialog();
    13. }
    14. }
    15. }

    (4)Frm_Start.Designer.cs

    1. namespace _193
    2. {
    3. partial class Frm_Start
    4. {
    5. ///
    6. /// Required designer variable.
    7. ///
    8. private System.ComponentModel.IContainer components = null;
    9. ///
    10. /// Clean up any resources being used.
    11. ///
    12. /// true if managed resources should be disposed; otherwise, false.
    13. protected override void Dispose(bool disposing)
    14. {
    15. if (disposing && (components != null))
    16. {
    17. components.Dispose();
    18. }
    19. base.Dispose(disposing);
    20. }
    21. #region Windows Form Designer generated code
    22. ///
    23. /// Required method for Designer support - do not modify
    24. /// the contents of this method with the code editor.
    25. ///
    26. private void InitializeComponent()
    27. {
    28. components = new System.ComponentModel.Container();
    29. timer1 = new System.Windows.Forms.Timer(components);
    30. SuspendLayout();
    31. //
    32. // timer1
    33. //
    34. timer1.Tick += Timer1_Tick;
    35. //
    36. // Frm_Start
    37. //
    38. AutoScaleDimensions = new SizeF(7F, 17F);
    39. AutoScaleMode = AutoScaleMode.Font;
    40. BackgroundImage = Properties.Resources.start;
    41. BackgroundImageLayout = ImageLayout.Stretch;
    42. ClientSize = new Size(368, 240);
    43. Name = "Frm_Start";
    44. Text = "启动界面";
    45. FormClosed += Frm_Start_FormClosed;
    46. Load += Frm_Start_Load;
    47. ResumeLayout(false);
    48. }
    49. #endregion
    50. private System.Windows.Forms.Timer timer1;
    51. }
    52. }

    (5)Frm_Start.cs

    1. namespace _193
    2. {
    3. public partial class Frm_Start : Form
    4. {
    5. public Frm_Start()
    6. {
    7. InitializeComponent();
    8. }
    9. ///
    10. /// 启动计时器,10s计时
    11. ///
    12. private void Frm_Start_Load(object sender, EventArgs e)
    13. {
    14. timer1.Start();
    15. timer1.Interval = 10000;
    16. }
    17. ///
    18. /// 关闭启动窗体
    19. ///
    20. private void Timer1_Tick(object sender, EventArgs e)
    21. {
    22. Close();
    23. }
    24. ///
    25. /// 关闭计时器
    26. ///
    27. private void Frm_Start_FormClosed(object sender, FormClosedEventArgs e)
    28. {
    29. timer1.Stop();
    30. }
    31. }
    32. }

    (6)生成效果

     

  • 相关阅读:
    【Linux专题】SFTP 用户配置 ChrootDirectory
    将二叉树的子树映射成一个数,用于判断子树相同
    Docker已存在Nginx容器对宿主机映射容器的目录进行修改,完成不同前端项目的部署
    读书笔记-《ON JAVA 中文版》-摘要2
    vue如何动态加载显示本地图片资源
    Betaflight关于STM32F405 SBUS协议兼容硬件电气特性问题
    RHCSA认证考试---17.编写Shell脚本查找文件
    HTML+CSS大作业 环境网页设计与实现(垃圾分类) web前端开发技术 web课程设计 网页规划与设计
    进程信号的保存和处理
    Android 13.0 修改wifi信号强度
  • 原文地址:https://blog.csdn.net/wenchm/article/details/138115106