[英]What is the best way to hide the main form on application's startup
What is the best way to hide the main form on application's startup to show it later? 在应用程序启动时隐藏主窗体以在以后显示它的最佳方法是什么?
If I just call the Hide
method in the Load
event of this form it gives a terrible flash for some time before actually hiding it. 如果我只是在此窗体的Load
事件中调用Hide
方法,则在实际隐藏它之前会发出一段时间的可怕闪光。
Thanks in advance. 提前致谢。
The simplest way is to set Opacity = 0
in the designer. 最简单的方法是在设计器中将Opacity = 0
设置Opacity = 0
。 Of course you will want to set it back to 100
at some point later.. 当然,您稍后会希望将其设置回100
。
Or you may want to use a splash screen, maybe like this: 或者您可能想要使用启动屏幕,例如:
static class Program
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Splash splash = new Splash();
splash.Show();
Application.Run();
}
}
With a splash screen: 使用启动画面:
public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
}
Form1 form1 = new Form1();
private void Splash_Load(object sender, EventArgs e)
{
form1.WindowState = FormWindowState.Minimized;
form1.Hide();
}
}
You can then show it for example when the splash screen is closed: 然后,您可以在例如初始屏幕关闭时显示它:
private void Splash_FormClosed(object sender, FormClosedEventArgs e)
{
form1.Show();
form1.WindowState = FormWindowState.Normal;
}
Which would happen whenever you want or maybe after some time: 每当您需要时或在一段时间后会发生这种情况:
public Splash()
{
InitializeComponent();
Timer timer = new Timer();
timer.Interval = 5000;
timer.Enabled = true;
timer.Tick += (s,e) =>{ this.Close();};
}
Since the program is not watching a Form to close we also need to add this to the main form's closed event: 由于程序没有监视Form的关闭,因此我们还需要将其添加到主窗体的close事件中:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
If you don't want the splash screen to be visible at all you can hide it like this: 如果您根本不希望启动屏幕可见,则可以将其隐藏,如下所示:
public Splash()
{
InitializeComponent();
this.Opacity = 0;
But please make sure you don't leave the users in the blind: When I start a program I want immediate response!! 但是请确保您不会盲目地吸引用户:当我启动程序时,我想立即得到响应!
You can proceed like this: 您可以这样进行:
private void Form1_Load(object sender, EventArgs e)
{
if (Settings.Instance.HideAtStartup)
{
BeginInvoke(new MethodInvoker(delegate
{
Hide();
}));
}
}
An alternative way can be to use the Application.Run(Form) method. 另一种方法是使用Application.Run(Form)方法。 You can create the main form with its Visible property initially set to false , and provide no argument to Application.Run() in main loop. 您可以使用初始设置为false的 Visible属性创建主窗体,并且在主循环中不向Application.Run()提供任何参数。
Modify your Program class - this is where the form is created and shown: 修改您的Program类-这是创建和显示表单的地方:
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main () {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 frm = new Form1();
frm.Visible = false;
Application.Run();
}
}
Hopefully your adding some sort of user interface? 希望您添加某种用户界面?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.