Application 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供 static 方法和属性以管理应用程序,例如启动和停止应用程序、处理 Windows 消息的方法和获取应用程序信息的属性。 此类不能被继承。
public ref class Application sealed
	public sealed class Application
	type Application = class
	Public NotInheritable Class Application
		- 继承
 - 
				Application
 
示例
下面的代码示例列出窗体上列表框中的数字。 每次单击 button1时,应用程序都会向列表中添加另一个数字。
方法 Main 调用 Run 以启动应用程序,这将创建窗体 listBox1 和 button1。 当用户单击 button1时, button1_Click 方法将显示 。MessageBox 如果用户单击 No , MessageBox该方法会将 button1_Click 一个数字添加到列表中。 如果用户单击 Yes,应用程序将调用 Exit 以处理队列中的所有剩余消息,然后退出。
注意
在部分信任中,对 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 : 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 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
	注解
类 Application 具有启动和停止应用程序和线程以及处理 Windows 消息的方法,如下所示:
Run 在当前线程上启动应用程序消息循环,并选择性地使窗体可见。
Exit 或 ExitThread 停止消息循环。
DoEvents 在程序循环时处理消息。
AddMessageFilter 将消息筛选器添加到应用程序消息泵以监视 Windows 消息。
IMessageFilter 允许在调用事件处理程序之前停止引发事件或执行特殊操作。
此类具有 CurrentCulture 和 CurrentInputLanguage 属性,用于获取或设置当前线程的区域性信息。
无法创建此类的实例。
属性
| AllowQuit | 
		 获取指示调用方能否退出该应用程序的值。  | 
        	
| ColorMode | 
		 提供   | 
        	
| CommonAppDataPath | 
		 获取所有用户共享的应用程序数据的路径。  | 
        	
| CommonAppDataRegistry | 
		 获取所有用户共享的应用程序数据的注册表项。  | 
        	
| CompanyName | 
		 获取与该应用程序关联的公司名称。  | 
        	
| CurrentCulture | 
		 获取或设置当前线程的区域性信息。  | 
        	
| CurrentInputLanguage | 
		 获取或设置当前线程的当前输入语言。  | 
        	
| ExecutablePath | 
		 获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。  | 
        	
| HighDpiMode | 
		 获取应用程序的当前高 DPI 模式。  | 
        	
| IsDarkModeEnabled | 
		 提供   | 
        	
| LocalUserAppDataPath | 
		 获取本地、非漫游用户的应用程序数据的路径。  | 
        	
| MessageLoop | 
		 获取指示该线程上是否存在消息循环的值。  | 
        	
| OpenForms | 
		 获取为应用程序所有的已打开窗体的集合。  | 
        	
| ProductName | 
		 获取与该应用程序关联的产品名称。  | 
        	
| ProductVersion | 
		 获取与该应用程序关联的产品版本。  | 
        	
| RenderWithVisualStyles | 
		 获取指定当前应用程序是否使用可视样式绘制控件的值。  | 
        	
| SafeTopLevelCaptionFormat | 
		 获取或设置当顶级窗口标题与版权警告标志一起显示时,要应用于顶级窗口标题的格式字符串。  | 
        	
| StartupPath | 
		 获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。  | 
        	
| SystemColorMode | 
		 提供   | 
        	
| UserAppDataPath | 
		 获取用户的应用程序数据的路径。  | 
        	
| UserAppDataRegistry | 
		 获取用户的应用程序数据的注册表项。  | 
        	
| UseVisualStyles | 
		 获取一个值,该值指示是否为应用程序启用视觉样式。  | 
        	
| UseWaitCursor | 
		 获取或设置是否对应用程序的所有打开窗体使用等待光标。  | 
        	
| VisualStyleState | 
		 获取指定可视样式如何应用于应用程序窗口的值。  | 
        	
方法
事件
| ApplicationExit | 
		 在应用程序即将关闭时发生。  | 
        	
| EnterThreadModal | 
		 当应用程序即将进入模式状态时发生。  | 
        	
| Idle | 
		 当应用程序完成处理并即将进入空闲状态时发生。  | 
        	
| LeaveThreadModal | 
		 当应用程序即将退出模式状态时发生。  | 
        	
| ThreadException | 
		 在发生未捕获线程异常时发生。  | 
        	
| ThreadExit | 
		 在某线程即将关闭时发生。 当应用程序的主线程即将关闭时,首先引发该事件,随后是 ApplicationExit 事件。  |