Application 类

提供 static 方法和属性以管理应用程序,例如启动和停止应用程序、处理 Windows 消息的方法和获取应用程序信息的属性。无法继承此类。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public NotInheritable Class Application
用法
Dim instance As Application
public sealed class Application
public ref class Application sealed
public final class Application
public final class Application

备注

Application 类具有用于启动和停止应用程序和线程以及处理 Windows 消息的方法,如下所示:

  • Run 在当前线程上启动应用程序消息循环,并可以选择使某窗体可见。

  • ExitExitThread 停止消息循环。

  • DoEvents 在您的程序处于某个循环中时处理消息。

  • AddMessageFilter 向应用程序消息泵添加消息筛选器来监视 Windows 消息。

  • IMessageFilter 使您可以阻止引发某事件或在调用某事件处理程序前执行特殊操作。

该类具有用于获取或设置当前线程的区域性信息的 CurrentCultureCurrentInputLanguage 属性。

不能创建此类的实例。

示例

下面的代码示例在窗体上的列表框中列出编号。每次单击 button1 时,应用程序均向列表中另外添加一个编号。

Main 方法调用 Run,以启动创建窗体、listBox1button1 的应用程序。当用户单击 button1 时,button1_Click 方法将显示 MessageBox。如果用户在 MessageBox 上单击 No,则 button1_Click 方法将向列表添加一个编号。如果用户单击 Yes,则应用程序调用 Exit 来处理队列中的所有剩余消息,然后退出。

提示

在部分信任中对 Exit 的调用将失败。

Public Class Form1 
    Inherits Form

    <STAThread()> _
     Shared Sub Main()
        ' Start the application.
        Application.Run(New Form1)
    End Sub

    Private WithEvents button1 As Button
    Private WithEvents listBox1 As ListBox

    Public Sub New()
        button1 = New Button
        button1.Left = 200
        button1.Text = "Exit"

        listBox1 = New ListBox
        Me.Controls.Add(button1)
        Me.Controls.Add(listBox1)
    End Sub

    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles button1.Click
        Dim count As Integer = 1
        ' Check to see whether the user wants to exit the application.
        ' If not, add a number to the list box.
        While (MessageBox.Show("Exit application?", "", _
            MessageBoxButtons.YesNo) = DialogResult.No)

            listBox1.Items.Add(count)
            count += 1

        End While

        ' The user wants to exit the application. 
        ' Close everything down.
        Application.Exit()
    End Sub

End Class
public class Form1 : Form
{
    [STAThread]
    public static void Main()
    {
        // Start the application.
        Application.Run(new Form1());
    }

    private Button button1;
    private ListBox listBox1;

    public Form1()
    {
        button1 = new Button();
        button1.Left = 200;
        button1.Text = "Exit";
        button1.Click += new EventHandler(button1_Click);

        listBox1 = new ListBox();
        this.Controls.Add(button1);
        this.Controls.Add(listBox1);
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        int count = 1;
        // Check to see whether the user wants to exit the application.
        // If not, add a number to the list box.
        while (MessageBox.Show("Exit application?", "", 
            MessageBoxButtons.YesNo)==DialogResult.No)
        {
            listBox1.Items.Add(count);
            count += 1;
        }

        // The user wants to exit the application. 
        // Close everything down.
        Application.Exit();
    }
}
public ref class Form1: public System::Windows::Forms::Form
{
private:
   Button^ button1;
   ListBox^ listBox1;

public:
   Form1()
   {
      button1 = gcnew Button;
      button1->Left = 200;
      button1->Text =  "Exit";
      button1->Click += gcnew EventHandler( this, &Form1::button1_Click );
      listBox1 = gcnew ListBox;
      this->Controls->Add( button1 );
      this->Controls->Add( listBox1 );
   }

private:
   void Form1::button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      int count = 1;
      
      // Check to see whether the user wants to exit 
      // the application. If not, add a number to the list box.
      while ( MessageBox::Show(  "Exit application?",  "", MessageBoxButtons::YesNo ) == ::DialogResult::No )
      {
         listBox1->Items->Add( count );
         count += 1;
      }

      
      // The user wants to exit the application. 
      // Close everything down.
      Application::Exit();
   }

};

int main()
{
   
   // Starts the application.
   Application::Run( gcnew Form1 );
}
public class Form1 extends Form
{
    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        // Start the application.
        Application.Run(new Form1());
    } //main

    private Button button1;
    private ListBox listBox1;

    public Form1()
    {
        button1 = new Button();
        button1.set_Left(200);
        button1.set_Text("Exit");
        button1.add_Click(new EventHandler(button1_Click));
        listBox1 = new ListBox();
        this.get_Controls().Add(button1);
        this.get_Controls().Add(listBox1);
    } //Form1

    public void button1_Click(Object sender, System.EventArgs e)
    {
        int count = 1;

        // Check to see whether the user wants to exit the application.
        // If not, add a number to the list box.
        while ((MessageBox.Show("Exit application?", "", 
                MessageBoxButtons.YesNo).Equals(get_DialogResult().No))) {
            listBox1.get_Items().Add(new Integer(count));
            count += 1;
        }
        // The user wants to exit the application. 
        // Close everything down.
        Application.Exit();
    } //button1_Click
} //Form1

继承层次结构

System.Object
  System.Windows.Forms.Application

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Application 成员
System.Windows.Forms 命名空间