简体   繁体   English

在应用程序启动时隐藏主窗体的最佳方法是什么

[英]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.

相关问题 WPF,什么是存储要在启动时使用的常量的最佳方法? - WPF, What's the best way to store a constant to be used at startup? 在运行时调整WinForms应用程序主窗体的起始大小和位置的最安全方法是什么? - What's the safest way to adjust my WinForms application main form starting size and position at run time? 监视桌面应用程序的最佳方法是什么? - What's the best way to watchdog a desktop application? c#-保留Windows窗体应用程序事件日志的最佳方法是什么? - c# - What's the best way to keep log for events of windows form application? 打开主窗体后如何隐藏或关闭启动loginlog窗体? - How to hide or close startup loginform after main form is open? 在 Windows 窗体应用程序中发现内存泄漏的最佳方法是什么? - what is the best way to found memory leaks in windows form application? 什么是判断鼠标是否在表单上方的最佳方法? - What's the best way to tell if the mouse is over a form or not? 将数据提取到Web窗体的ListView中的最佳方法是什么? - What is the best way to pull data into a web form's ListView? .NET Windows窗体应用程序更新自身的最佳方法是什么? - What's the best way for a .NET windows forms application to update itself? 在数据库访问应用程序中管理并发的最佳方法是什么? - What's the best way to manage concurrency in a database access application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM