C# winforms 启动(Splash)表单不隐藏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/510765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# winforms startup (Splash) form not hiding
提问by TK.
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:
我有一个 winforms 应用程序,我在其中使用 2 个表单来显示所有必要的控件。第一个表单是一个闪屏,它告诉用户它正在加载等。所以我使用以下代码:
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. I am currently using the following:
一旦应用程序完成加载,我希望 SplashForm 隐藏或我发送到后面并显示主要来源。我目前正在使用以下内容:
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'. What I am currently doing is clicking on the MainForm to manually bring it to the front. Any ideas on why this is happening?
我看到的是 MainForm 已显示,但 SplashForm 仍然在“顶部”可见。我目前正在做的是单击 MainForm 以手动将其置于最前面。关于为什么会发生这种情况的任何想法?
采纳答案by Grzenio
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):
我在单独的线程上运行飞溅表单(这是 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);
}
回答by Svish
If I have understood correctly, you should just use Application.Run on your main form. So either show your splash first just by using something like:
如果我理解正确,您应该只在主窗体上使用 Application.Run 。因此,要么使用以下内容首先显示您的飞溅:
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. (Possibly setting Visible to false before you show it and back to true afterwards.
或者在您的主窗体 Load 事件处理程序中显示它,然后等待它关闭或其他什么,直到您让 Load 方法完成。(可能在显示之前将 Visible 设置为 false,然后再设置为 true。
回答by TK.
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. So I can use:
我认为实现我所需要的最好方法是从 MainForm 控制一切。所以我可以使用:
Application.Run(new MainForm());
This will then be responsable for showing/updating/hiding the SplashScreen. This way I can have the necessary complex intactions with the rest of the system managed by the MainForm.
这将负责显示/更新/隐藏 SplashScreen。通过这种方式,我可以对由 MainForm 管理的系统的其余部分进行必要的复杂操作。
回答by Luke
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.
将此添加到您的飞溅表单类中。