C#的WinForm - 载入画面(C# WinForm - loading screen)

2019-08-19 20:14发布

我想问一下如何制作加载屏幕(只是一个图片或东西)正在加载的程序而出现,并消失在程序加载完毕。

在发烧友的版本,我已经看到了显示进度条(%)。 你怎么能有一个,你怎么计算%上显示呢?

我知道有一个的Form_Load()事件,但我没有看到一个Form_Loaded()事件,或%的作为一种属性/属性的任何地方。

Answer 1:

所有你需要创建一个形式,启动画面在你面前表现出来的主要开始显示着陆页,一旦着陆页加载关闭此飞溅。

using System.Threading;
using System.Windows.Forms;

namespace MyTools
{
    public class SplashForm : Form
    {
        //Delegate for cross thread call to close
        private delegate void CloseDelegate();

        //The type of form to be displayed as the splash screen.
        private static SplashForm splashForm;

        static public void ShowSplashScreen()
        {
            // Make sure it is only launched once.

            if (splashForm != null)
                return;
            Thread thread = new Thread(new ThreadStart(SplashForm.ShowForm));
            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        static private void ShowForm()
        {
            splashForm = new SplashForm();
            Application.Run(splashForm);
        }

        static public void CloseForm()
        {
            splashForm.Invoke(new CloseDelegate(SplashForm.CloseFormInternal));
        }

        static private void CloseFormInternal()
        {
            splashForm.Close();
            splashForm = null;
        }
    }
}

和主程序功能如下:

[STAThread]
static void Main(string[] args)
{
    SplashForm.ShowSplashScreen();
    MainForm mainForm = new MainForm(); //this takes ages
    SplashForm.CloseForm();
    Application.Run(mainForm);
}


Answer 2:

如果你要显示SplashForm超过你的应用程序一次,一定要设置splashForm变量设置为null,否则你会得到一个错误。

static private void CloseFormInternal()
{
    splashForm.Close();
    splashForm = null;
}


Answer 3:

我有问题,与所有其他解决方案,我发现,特别是那些显示Splash在其他线程不是GUI线程,并专门在Citrix。

例:

  • 飞溅永远不会关闭
  • 开机显示错误的监视器上
  • 飞溅节目确定,但MainForm的是显示所有其他窗口的后面

我结束了这一点,它似乎运作良好。

飞溅的形式:

public partial class Splash : Form
{
    public Splash()
    {
        InitializeComponent();
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }
}

飞溅的形式续:

partial class Splash
{
    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Splash));
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
        this.pictureBox1.Location = new System.Drawing.Point(0, 0);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(512, 224);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
        // 
        // Splash
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(512, 224);
        this.ControlBox = false;
        this.Controls.Add(this.pictureBox1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Splash";
        this.ShowIcon = false;
        this.ShowInTaskbar = false;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Splash";
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.PictureBox pictureBox1;
}

主要:

[STAThread]
static void Main(string[] _args)
{
    ShowSplash();
    MainForm mainForm = new MainForm();
    Application.Run(mainForm);
}

private static void ShowSplash()
{
    Splash sp = new Splash();
    sp.Show();
    Application.DoEvents();

    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
    t.Interval = 1000;
    t.Tick += new EventHandler((sender, ea) =>
    {
        sp.BeginInvoke(new Action(() =>
        {
            if (sp != null && Application.OpenForms.Count > 1)
            {
                sp.Close();
                sp.Dispose();
                sp = null;
                t.Stop();
                t.Dispose();
                t = null;
            }
        }));
    });
    t.Start();
}


文章来源: C# WinForm - loading screen