【问题标题】:Windows Forms Splash Screen - Show a form while loading main formWindows 窗体启动画面 - 在加载主窗体时显示窗体
【发布时间】:2015-12-01 19:55:52
【问题描述】:

我试图让启动画面首先出现,然后在启动之后出现MainForm。但是我在初始屏幕中的进度条没有到达进度条的末尾。并且程序继续运行但不起作用。

如何在加载主窗体时显示启动画面?

我的代码是这样的:

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());
    }
}

【问题讨论】:

  • 你能展示创建这个表单的代码吗? pbcarrega 在哪里初始化?
  • pbcarrega 是 ProgressBar 的名称
  • 这个代码是Form Splash Screen的代码,但是你想知道Form Main吗?
  • @Novatec - 您需要向我们提供足够的代码,以便我们重现您的问题。仅仅选择几行你认为相关的代码是不够的。请发布minimal, complete, and verifiable 代码以复制您的问题。
  • 我现在发布了 Form Main 的部分,我正在查看,我认为我在 Application.Run() 的那部分做错了

标签: c# .net winforms splash-screen


【解决方案1】:

创建启动画面的方法有多种:

  • 你可以依赖WindowsFormsApplicationBase的闪屏功能

  • 您可以通过在不同的 UI 线程上显示一个表单并在主程序成功加载后将其隐藏来显示自己实现该功能。

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

注意:那些正在寻找显示加载窗口或加载 加载数据时的gif动画,可以看看这个帖子:Show Loading animation during loading data in other thread

选项 1 - 使用 WindowsFormsApplicationBase 启动画面功能

  1. Microsoft.VisualBasic.dll 的引用添加到您的项目中。
  2. 通过从WindowsFormsApplicationBase 派生来创建MyApplication
  3. 覆盖 OnCreateMainForm 并将您希望作为启动表单的 from 分配给 MainForm 属性。
  4. 覆盖 OnCreateSplashScreen 并将要显示为初始屏幕的表单分配给 SplashScreen 属性。

  5. 在您的Main 方法中,创建MyApplication 的实例并调用其Run 方法。

例子

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 事件,并在那里显示和关闭您的启动画面。

例子

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 Forms Transparent Background Image.

【讨论】:

  • 我更改了名称以便对您更友好。现在将此代码与您的表单名称一起使用,它足以找到所有 SplashForm 并将其替换为您想要的启动表单 (FormSplash) 的名称,并查找并替换所有 MainForm 为您的主表单所需的名称 (表格1)。
  • 我可以使用相同的代码更改某些部分以在 ProgressBar 中使用吗?因为 Thread.Sleep 我暂时不会使用。
  • @Novatec 您可以更改代码的任何部分并按照您的意愿进行修改,但我不建议您在启动画面中使用进度条,而是仅显示加载图像(动画 .gif) 在初始屏幕中。我认为这是一个很好的启动画面实现,对其他用户也有帮助:)
  • 我尝试了这个示例及其工作,但是当我的启动画面关闭时,我的表单进入后台。任何可以克服的建议
  • @Rawat 我无法重现该问题,但作为建议,在 MainForm_LoadCompleted 事件处理程序中,您可以使用 myMainForm.Activate(); 而不是 myMainForm.TopMost = true; myMainForm.Activate(); myMainForm.TopMost = false;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-18
相关资源
最近更新 更多