如何:使启动 Windows 窗体不可见

更新:2007 年 11 月

要使基于 Windows 的应用程序的主窗体在应用程序启动时不可见,必须将应用程序的启动逻辑移动到一个单独的类中。不能简单地将其 Visible 属性设置为 false。

将应用程序的生存期与窗体的生存期分隔开以后,可以使窗体可见(和不可见),因为当您“关闭”用于启动应用程序的类时,应用程序将结束。

说明:

由于在模块的代码运行时,该模块将不可见,下面的过程将包含一个在启动模块中添加消息框的步骤,其目的只是表明应用程序正在运行。

将窗体设置为在开始时不可见

  1. 执行下列操作之一:

    1. 在 Visual Basic 中,右击项目并选择“添加模块”以将模块添加到基于 Windows 的应用程序中。

    2. 在 Visual C# 中,创建一个新类。

    3. 在 Visual C++ 中,打开基于 Windows 的应用程序的 Form1.cpp。

      有关创建基于 Windows 的应用程序的更多信息,请参见 如何:创建 Windows 应用程序项目

  2. 在模块或类中,开发可以作为项目启动对象的 Main 子例程。

    下面的代码示例演示如何实现上述过程。

    Sub Main()
       ' Instantiate a new instance of Form1.
       Dim f1 as New Form1()
       ' Display a messagebox. This shows the application is running, 
       ' yet there is nothing shown to the user. This is the point at 
       ' which you customize your form.
       System.Windows.Forms.MessageBox.Show( _
          "The application is running now, but no forms have been shown.")
       ' Customize the form.
       f1.Text = "Running Form"
       ' Show the instance of the form modally.
       f1.ShowDialog()
    End Sub
    
    // All methods must be contained in a class.
    // This class is added to the namespace containing the Form1 class.
    class MainApplication
    {
       public static void Main()
       {
          // Instantiate a new instance of Form1.
          Form1 f1 = new Form1();
          // Display a messagebox. This shows the application 
          // is running, yet there is nothing shown to the user. 
          // This is the point at which you customize your form.
          System.Windows.Forms.MessageBox.Show("The application "
             + "is running now, but no forms have been shown.");
          // Customize the form.
          f1.Text = "Running Form";
          // Show the instance of the form modally.
          f1.ShowDialog();
       }
    }
    
    // You can use this Mian method in the Program class (Program.jsl):
    public static void main(String[] args)
    {
       // Instantiate a new instance of Form1.
       Form1 f1 = new Form1();
       // Display a messagebox. This shows the application 
       // is running, yet there is nothing shown to the user. 
       // This is the point at which you customize your form.
       System.Windows.Forms.MessageBox.Show("The application "
          + "is running now, but no forms have been shown.");
       // Customize the form.
       f1.set_Text("Running Form");
       // Show the instance of the form modally.
       f1.ShowDialog();
    }
    
    void Main()
    {
       // Instantiate a new instance of Form1.
       Form1^ f1 = gcnew Form1();
       // Display a messagebox. This shows the application 
       // is running, yet there is nothing shown to the user. 
       // This is the point at which you customize your form.
       System::Windows::Forms::MessageBox::Show(
          "The application is running now, "
          "but no forms have been shown.");
       // Customize the form.
       f1->Text = "Running Form";
       // Show the instance of the form modally.
       f1->ShowDialog();
    }
    
    说明:

    上述代码示例中的消息框是用完全限定命名空间指定的,这是因为创建的模块不同于标准 Windows 窗体,默认情况下不导入任何命名空间。有关导入命名空间的更多信息,请参见引用和 Imports 语句 (Visual Basic)、using 指令(C# 参考) (Visual C#) 或 using Directive (C++) (Visual C++)。Application.Run() 将启动消息泵,这对某些应用程序的行为极端重要,并能够在应用程序生命周期的某个时段(如关闭时)影响窗体行为。有关更多信息,请参见 Application.Run 方法

  3. 将项目的启动对象从 Form1 更改为 Sub Main。

    对于 Visual C#,将启动对象设置为上面的代码示例中的类的名称。有关更多信息,请参见如何:在 Windows 应用程序中选择启动窗体以及如何:更改应用程序的启动对象 (Visual Basic)

    说明:

    对于使用 Visual C++ 开发的基于 Windows 的应用程序,请跳过此步骤。

  4. 按 F5 运行项目。

    当应用程序运行时, Main() 中的代码首先执行,而 Form1 的实例则等待并隐藏,直到显示它的代码运行。这使您可以在后台在 Form1 的实例中做任何想做的事情而不让用户知道。

请参见

任务

如何:显示有模式和无模式 Windows 窗体

如何:设置 Windows 窗体的屏幕位置

其他资源

更改 Windows 窗体外观