简体   繁体   English

C#winforms启动(Splash)表单没有隐藏

[英]C# winforms startup (Splash) form not hiding

I have a winforms application in which I am using 2 Forms to display all the necessary controls. 我有一个winforms应用程序,我在其中使用2个表单来显示所有必要的控件。 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() );

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. 一旦应用程序完成加载,我希望SplashForm隐藏或我发送到后面和主要显示。 I am currently using the following: 我目前正在使用以下内容:

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

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

What I am seeing is that the MainForm is shown, but the SplashForm is still visible 'on top'. 我所看到的是显示了MainForm,但SplashForm仍然可以在“顶部”显示。 What I am currently doing is clicking on the MainForm to manually bring it to the front. 我目前正在做的是点击MainForm手动将它带到前面。 Any ideas on why this is happening? 有关为什么会发生这种情况的任何想法?

Probably you just want to close the splash form, and not send it to back. 可能你只想关闭飞溅形式,而不是发送回来。

I run the splash form on a separate thread (this is class SplashForm): 我在一个单独的线程上运行splash表单(这是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();
        splashForm = null;
    }
...
}

and the main program function looks like this: 并且主程序功能如下所示:

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

This is crucial to prevent your splash screen from stealing your focus and pushing your main form to the background after it closes: 这对于防止您的启动屏幕在关闭后阻止您的焦点并将主窗体推送到后台至关重要:

protected override bool ShowWithoutActivation {
    get { return true; }
}

Add this to you splash form class. 将此添加到您的splash表单类。

If I have understood correctly, you should just use Application.Run on your main form. 如果我理解正确,您应该只在主窗体上使用Application.Run。 So either show your splash first just by using something like: 因此,要么首先使用以下内容显示您的启动:

using(MySplash form = new MySplash())
   form.ShowDialog();

And then close it manually in MySplash whenever you want to. 然后随时在MySplash中手动关闭它。

Or show it in your main forms Load event handler and then wait for it to close or whatever until you let the Load method complete. 或者在主窗体中显示它加载事件处理程序,然后等待它关闭或其他任何东西,直到你让Load方法完成。 (Possibly setting Visible to false before you show it and back to true afterwards. (可能在显示之前将Visible设置为false,然后再返回true。

I believe that it might be a design flaw in my current design! 我相信这可能是我目前设计中的一个设计缺陷!

I think the best Way to achieve what I need is to have everything controled from the MainForm. 我认为实现我需要的最好方法是让所有东西都能从MainForm中控制出来。 So I can use: 所以我可以用:

Application.Run(new MainForm());

This will then be responsable for showing/updating/hiding the SplashScreen. 然后,这将负责显示/更新/隐藏SplashScreen。 This way I can have the necessary complex intactions with the rest of the system managed by the MainForm. 通过这种方式,我可以与MainForm管理的系统的其余部分进行必要的复杂操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM