加载主窗体中显示启动画面 [英] Show Splash Screen during Loading the Main Form

查看:166
本文介绍了加载主窗体中显示启动画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让出现闪屏第一和飞溅之后,的MainForm 出现。但是,进度条,我有闪屏没有得到该栏的末尾。程序继续运行,而不是工作。



我怎样才能加载的主要形式时显示启动画面?



我的代码这是类似的东西:

 公共部分类SplashForm:表
{
公共SplashForm ()
{
的InitializeComponent();
}
私人无效SplashForm_Load(对象发件人,EventArgs五)
{
timer1.Enabled = TRUE;
timer1.Start();
timer1.Interval = 1000;
progressBar1.Maximum = 10;
timer1.Tick + =新的EventHandler(timer1_Tick);将
}
公共无效timer1_Tick(对象发件人,EventArgs五)
{
如果(progressBar1.Value = 10!)
{
progressBar1.Value ++;
}
,否则
{
timer1.Stop();
Application.Exit();
}
}
}

下面是第一部分

 公共部分类的MainForm:在的MainForm 的代码形式
{
公众的MainForm()
{
的InitializeComponent();
Application.Run(新SplashForm());
}
}


解决方案

有创建闪屏的方式不同。这是更好地展示并从您的主要形式逻辑关闭闪屏的逻辑分离。



要做到这一点,你可以创建一个 LoadCompleted 事件,然后在认购其。程序类,表演和关闭闪屏有



下面是一个什么样上述我的实现:



1-在你的的MainForm ,添加一个 LoadCompleted 事件,然后覆盖的OnLoad 方法引发事件。 (大概所示事件是适用的,而不是我们的自定义事件。)

 公共事件的EventHandler LoadCompleted; 
保护覆盖无效的OnLoad(EventArgs的发送)
{
base.OnLoad(E);
this.OnLoadCompleted(EventArgs.Empty);
}
保护OnLoadCompleted(EventArgs五)
{
VAR处理器= LoadCompleted虚拟无效;
如果(处理!= NULL)
处理器(这一点,E);
}
私人无效MainForm_Load(对象发件人,EventArgs五)
{
//只是为了测试,你可以做一个延时,模拟耗时的任务
//在实际应用中,你在这里加载数据和其他设置
}

2 - 在程序类,表演 SplashForm 则认购 LoadCompleted 你的的MainForm 并显示事件的MainForm ,然后在 LoadCompleted ,接近 SplashForm

 静态类节目
{
上配置静态SplashForm mySplashForm;
静态的MainForm myMainForm;
[STAThread]
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
//显示启动表格
mySplashForm =新SplashForm();
如果(mySplashForm!= NULL)
{
螺纹splashThread =新主题(新的ThreadStart(
()=> {Application.Run(mySplashForm);}));
splashThread.SetApartmentState(ApartmentState.STA);
splashThread.Start();
}
//创建和显示主窗体
myMainForm =新的MainForm();
myMainForm.LoadCompleted + = MainForm_LoadCompleted;
Application.Run(minScreen);
}
私有静态无效MainForm_LoadCompleted(对象发件人,EventArgs五)
{
如果(mySplashForm == NULL || mySplashForm.Disposing || mySplashForm.IsDisposed)
返回;
mySplashForm.Invoke(新动作(()=> {mySplashForm.Close();}));
mySplashForm.Dispose();
mySplashForm = NULL;
myMainForm.Activate();
}
}


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?

My code It's something like that :

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

Here are the first part of the code of the MainForm:

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

解决方案

There are different way of creating splash screens. It's better to separate the logic of showing and closing splash screen from logic of your main form.

To do so, you can create a LoadCompleted event and then subscribe for it in Program class, and show and close your splash screen there.

Here is an implementation of what I described above:

1- In your MainForm, add a LoadCompleted event and then override OnLoad method to raise the event. (Probably Shown event is applicable instead of our custom event.)

public event EventHandler LoadCompleted;
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    this.OnLoadCompleted(EventArgs.Empty);
}
protected virtual void OnLoadCompleted(EventArgs e)
{
    var handler = LoadCompleted;
    if (handler != null)
        handler(this, e);
}
private void MainForm_Load(object sender, EventArgs e)
{
    //Just for test, you can make a delay to simulate a time-consuming task
    //In a real application here you load your data and other settings
}

2- In the Program class, show SplashForm then subscribe for LoadCompleted event of your MainForm and show MainForm, then in LoadCompleted, close SplashForm.

static class Program
{
    static SplashForm mySplashForm;
    static MainForm myMainForm;
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //Show Splash Form
        mySplashForm = new SplashForm();
        if (mySplashForm != null)
        {
            Thread splashThread = new Thread(new ThreadStart(
                () => { Application.Run(mySplashForm); }));
            splashThread.SetApartmentState(ApartmentState.STA);
            splashThread.Start();
        }
        //Create and Show Main Form
        myMainForm = new MainForm();
        myMainForm.LoadCompleted += MainForm_LoadCompleted;
        Application.Run(minScreen);
    }
    private static void MainForm_LoadCompleted(object sender, EventArgs e)
    {
        if (mySplashForm == null || mySplashForm.Disposing || mySplashForm.IsDisposed)
            return;
        mySplashForm.Invoke(new Action(() => { mySplashForm.Close(); }));
        mySplashForm.Dispose();
        mySplashForm = null;
        myMainForm.Activate();
    }
}

这篇关于加载主窗体中显示启动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆