Windows窗体初始屏幕

编程入门 行业动态 更新时间:2024-10-12 16:30:00
本文介绍了Windows窗体初始屏幕-加载主窗体时显示窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图首先显示启动屏幕,并在启动后显示 MainForm 。但是我在初始屏幕中显示的进度条没有到达进度条的末尾。并且程序会继续运行并且无法运行。

I am trying to make the splash screen appears first and after the splash, the MainForm appears. But the progress bar which I have in splash screen don't get to the end of the bar. And the program continues running and not works.

如何在加载主表单时显示启动屏幕?

How can I show the splash screen during loading the main form?

我的代码就像这样:

public partial class SplashForm : Form { public SplashForm() { InitializeComponent(); } private void SplashForm_Load(object sender, EventArgs e) { timer1.Enabled = true; timer1.Start(); timer1.Interval = 1000; progressBar1.Maximum = 10; timer1.Tick += new EventHandler(timer1_Tick); } public void timer1_Tick(object sender, EventArgs e) { if (progressBar1.Value != 10) { progressBar1.Value++; } else { timer1.Stop(); Application.Exit(); } } }

以下是 MainForm 的代码:

public partial class MainForm : Form { public MainForm() { InitializeComponent(); Application.Run(new SplashForm()); } }

推荐答案

有有多种创建启动屏幕的方法:

There are different ways of creating splash screens:

  • 您可以依靠 WindowsFormsApplicationBase

您可以通过在其他UI线程上显示表单并隐藏来自己显示实现功能

You can show implement the feature yourself by showing a form on a different UI thread and hiding it after the main from loaded successfully.

在这篇文章中,我将展示这两种解决方案的示例。

In this post I'll show an example of both solutions.

注意:那些在数据加载过程中显示加载窗口或加载 gif动画的人可以看看这篇文章:显示在其他线程中加载数据期间的加载动画

选项1-使用WindowsFormsApplicationBase闪屏功能

  • 添加一个 Microsoft.VisualBasic.dll 对项目的引用。
  • 创建 MyApplication
  • 覆盖 OnCreateMainForm 并分配所需的成为 MainForm 属性的启动表单。
  • 覆盖 OnCreateSplashScreen 并将要显示为初始屏幕的表单分配给 SplashScreen 属性。

  • Add a reference of Microsoft.VisualBasic.dll to your project.
  • Create a MyApplication class by deriving from WindowsFormsApplicationBase
  • override OnCreateMainForm and assign the from that you want to be the startup form to MainForm property.
  • Override OnCreateSplashScreen and assign the form that you want to show as splash screen to SplashScreen property.

    在您的 Main 方法中,创建 MyApplication 的实例并调用其运行方法。

    In your Main method, create an instance of MyApplication and call its Run method.

    示例

    using System; using System.Windows.Forms; using Microsoft.VisualBasic.ApplicationServices; static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(true); var app = new MyApplication(); app.Run(Environment.GetCommandLineArgs()); } } public class MyApplication : WindowsFormsApplicationBase { protected override void OnCreateMainForm() { MainForm = new YourMainForm(); } protected override void OnCreateSplashScreen() { SplashScreen = new YourSplashForm(); } }

    选项2-使用不同的UI线程

    您可以通过在其他UI线程中显示初始屏幕来自己实现此功能。为此,您可以预订 Program 类中主窗体的 Load 事件,并显示并关闭启动画面

    You can implement the feature yourself by showing the splash screen in a different UI thread. To do so, you can subscribe to Load event of the main form in Program class, and show and close your splash screen there.

    示例

    using System; using System.Threading; using System.Windows.Forms; static class Program { static Form SplashScreen; static Form MainForm; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Show Splash Form SplashScreen = new Form(); var splashThread = new Thread(new ThreadStart( () => Application.Run(SplashScreen))); splashThread.SetApartmentState(ApartmentState.STA); splashThread.Start(); //Create and Show Main Form MainForm = new Form8(); MainForm.Load += MainForm_LoadCompleted; Application.Run(MainForm); } private static void MainForm_LoadCompleted(object sender, EventArgs e) { if (SplashScreen != null && !SplashScreen.Disposing && !SplashScreen.IsDisposed) SplashScreen.Invoke(new Action(() => SplashScreen.Close())); MainForm.TopMost = true; MainForm.Activate(); MainForm.TopMost = false; } }

    注意:显示光滑的边缘自定义形状的初始屏幕,请看这篇文章: Windows窗体透明背景图像。

  • 更多推荐

    Windows窗体初始屏幕

    本文发布于:2023-10-08 01:12:07,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1471074.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:窗体   屏幕   Windows

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!