C#的WinForms启动(开机)的形式没有隐瞒 [英] C# winforms startup (Splash) form not hiding

查看:140
本文介绍了C#的WinForms启动(开机)的形式没有隐瞒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我使用的2种形式来显示所有的必要的控制一个WinForms应用程序。第一种形式是一个启动画面中,它告诉它,它加载等,所以我使用以下code用户:

I have a winforms application in which I am using 2 Forms to display all the necessary controls. The first Form is a splash screen in which it tells the user that it it loading etc. So I am using the following code:

Application.Run( new SplashForm() );

一旦应用程序加载完成我想要的SplashForm隐藏或我发送到背面和从主要是表演。我目前使用以下内容:

Once the application has completed loading I want the SplashForm to hide or me sent to the back and the main from to be show. I am currently using the following:

private void showMainForm()
{
    this.Hide();
    this.SendToBack();

    // Show the GUI
    mainForm.Show();
    mainForm.BringToFront();
}

我所看到的是,在MainForm中显示,但SplashForm仍是可见的顶部。什么我目前做的是点击MainForm的手动把它带到前面。为什么任何想法是怎么回事?

What I am seeing is that the MainForm is shown, but the SplashForm is still visible 'on top'. What I am currently doing is clicking on the MainForm to manually bring it to the front. Any ideas on why this is happening?

推荐答案

也许你只是想关闭飞溅的形式,而不是把它背。

Probably you just want to close the splash form, and not send it to back.

我运行一个单独的线程飞溅的形式(这是类​​SplashForm):

I run the splash form on a separate thread (this is class SplashForm):

class SplashForm
{
    //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();
    }
...
}

和主程序功能如下:

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

这篇关于C#的WinForms启动(开机)的形式没有隐瞒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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