如何在显示 winforms 应用程序的主屏幕之前显示欢迎屏幕? [英] How to show a welcome screen before the main screen of a winforms application is shown?

查看:31
本文介绍了如何在显示 winforms 应用程序的主屏幕之前显示欢迎屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在应用程序启动时加载欢迎屏幕,然后用户单击欢迎屏幕上的按钮,然后关闭欢迎屏幕.最后然后显示主屏幕.

static void Main()//被调用的启动方法{Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new ACM812_DB());//欢迎屏幕}

当点击欢迎屏幕上的按钮时,它会隐藏欢迎窗口,然后调出主窗口.如下图.

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

它确实工作成功,但这是实现它的正确方法吗?

更新代码:

主窗体启动(MainForm.cs)

命名空间系统{公共部分类 MainForm : 表单{private void MainForm_Load(object sender, EventArgs e){WelcomeForm.Run(this);}公共主窗体(){初始化组件();}}}

然后调用欢迎屏幕

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

解决方案

并不是一个好的模式.

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

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

<块引用>

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.

这篇关于如何在显示 winforms 应用程序的主屏幕之前显示欢迎屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆