且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在显示 winforms 应用程序的主屏幕之前显示欢迎屏幕?

更新时间:2023-12-06 16:45:40

并不是一个好的模式.

因为应用程序实例是用来管理被激活"的主窗体的.直到应用程序退出.

您可以在主表单加载中将欢迎屏幕显示为对话框:

MainForm.cs

private void MainForm_Load(object sender, EventArgs e){WelcomeForm.Run(this);}

WelcomeForm.cs

public partial class WelcomeForm : Form{静态私有表单发件人;静态公共无效运行(表单发送者){如果(发件人 == 空)抛出新的 ArgumentNullException();发件人 = 发件人;new WelcomeForm().ShowDialog();}私有无效ButtonClose_Click(对象发送者,EventArgs e){关闭();}}

因此欢迎屏幕可以控制主窗体并设置所需的任何公共数据.

MainForm 是在 Main 方法中创建的,Application 实例取得控制权.因此调用 MainForm 的加载事件,最后调用 WelcomeForm 的 Run 静态方法,该方法创建一个实例并将其显示为对话框.这将停止尚未显示的 MainForm,直到欢迎屏幕关闭.然后就可以显示 MainForm.

这是一种方式,但很简单,只要它符合您的目标.

I want to load a welcome screen on the startup of the application then the user clicks a button on the welcome screen and then closes the welcome screen. Finally then shows the main screen.

static void Main() //startup method being called
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new ACM812_DB()); // welcome screen
}

When a button is clicked on the welcome screen it then hides the welcome window and then brings up the main window. As shown below.

private void button1_Click(object sender, EventArgs e)
{
   Form1 form1 = new Form1();
   form1.Show(); // main window
   this.Hide();
}

It does work successfully but is it the correct way to implement this?

Updated code:

Main form startup (MainForm.cs)

namespace System
{
  public partial class MainForm : Form
  {
     private void MainForm_Load(object sender, EventArgs e)
     {
        WelcomeForm.Run(this);
     }

     public MainForm()
     {
        InitializeComponent();
     }

   }
 }

Welcome screen then called

public partial class WelcomeForm : Form
{
    static private Form Sender;

    static public void Run(Form sender)
    {
      if (sender == null) 
      throw new ArgumentNullException();
      Sender = sender;
      new WelcomeForm().ShowDialog();
    }

    private void ButtonClose_Click(object sender, EventArgs e)
    {
      Close();
    }
}

Not really a good pattern.

Because the Application instance is made to manage the main form that is made to be "alive" until the application exit.

You can show the welcome screen in the main form load as a dialog:

MainForm.cs

private void MainForm_Load(object sender, EventArgs e)
{
  WelcomeForm.Run(this);
}

WelcomeForm.cs

public partial class WelcomeForm : Form
{
  static private Form Sender;
  static public void Run(Form sender)
  {
    if (sender == null) 
      throw new ArgumentNullException();
    Sender = sender;
    new WelcomeForm().ShowDialog();
  }
  private void ButtonClose_Click(object sender, EventArgs e)
  {
    Close();
  }
}

Hence the welcome screen can control the main form and set any public data needed.

The MainForm is created in the Main method and the Application instance take control. So the load event of MainForm is called and this last calls the Run static method of the WelcomeForm that creates an instance and shows it as a dialog. This stop the MainForm, not shown yet, until the welcome screen is closed. Then the MainForm can be shown.

It is one way among others, but it is simple, if it matches your goal.