Application 类

定义

提供 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。 如果用户单击 NoMessageBox数字,该方法会将 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 在当前线程上启动应用程序消息循环,并且(可选)使窗体可见。

  • ExitExitThread 停止消息循环。

  • DoEvents 在程序处于循环状态时处理消息。

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

  • IMessageFilter 允许在调用事件处理程序之前停止引发或执行特殊操作的事件。

此类具有和CurrentCultureCurrentInputLanguage属性来获取或设置当前线程的区域性信息。

不能创建此类的实例。

属性

名称 说明
AllowQuit

获取一个值,该值指示调用方是否可以退出此应用程序。

ColorMode

获取应用程序的默认颜色模式(深色模式)。

CommonAppDataPath

获取所有用户之间共享的应用程序数据的路径。

CommonAppDataRegistry

获取所有用户之间共享的应用程序数据的注册表项。

CompanyName

获取与应用程序关联的公司名称。

CurrentCulture

获取或设置当前线程的区域性信息。

CurrentInputLanguage

获取或设置当前线程的当前输入语言。

ExecutablePath

获取启动应用程序的可执行文件的路径,包括可执行文件名称。

HighDpiMode

获取应用程序的当前高 DPI 模式。

IsDarkModeEnabled

获取一个值,该值指示应用程序是否在深色系统颜色上下文中运行。

LocalUserAppDataPath

获取本地非漫游用户的应用程序数据的路径。

MessageLoop

获取一个值,该值指示此线程上是否存在消息循环。

OpenForms

获取应用程序拥有的打开窗体的集合。

ProductName

获取与此应用程序关联的产品名称。

ProductVersion

获取与此应用程序关联的产品版本。

RenderWithVisualStyles

获取一个值,该值指定当前应用程序是否使用视觉样式绘制控件。

SafeTopLevelCaptionFormat

获取或设置在显示带有警告横幅时要应用于顶级窗口标题的格式字符串。

StartupPath

获取启动应用程序的可执行文件的路径,不包括可执行文件名称。

SystemColorMode

获取 OS 系统环境的系统颜色模式设置。

UserAppDataPath

获取用户的应用程序数据的路径。

UserAppDataRegistry

获取用户的应用程序数据的注册表项。

UseVisualStyles

获取一个值,该值指示是否为应用程序启用视觉样式。

UseWaitCursor

获取或设置是否将等待游标用于应用程序的所有打开形式。

VisualStyleState

获取一个值,该值指定如何将视觉样式应用于应用程序窗口。

方法

名称 说明
AddMessageFilter(IMessageFilter)

添加消息筛选器,以便在消息路由到目标时监视Windows消息。

DoEvents()

处理消息队列中当前的所有Windows消息。

EnableVisualStyles()

为应用程序启用视觉样式。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
Exit()

通知所有消息泵,它们必须终止,然后在处理消息后关闭所有应用程序窗口。

Exit(CancelEventArgs)

通知所有消息泵,它们必须终止,然后在处理消息后关闭所有应用程序窗口。

ExitThread()

退出当前线程上的消息循环,并关闭线程上的所有窗口。

FilterMessage(Message)

针对窗口消息运行任何筛选器,并返回修改后的消息的副本。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
OleRequired()

在当前线程上初始化 OLE。

OnThreadException(Exception)

引发 ThreadException 事件。

RaiseIdle(EventArgs)

Idle在托管方案中引发事件。

RegisterMessageLoop(Application+MessageLoopCallback)

注册一个回调,用于检查消息循环是否在托管环境中运行。

RemoveMessageFilter(IMessageFilter)

从应用程序的消息泵中删除消息筛选器。

Restart()

关闭应用程序并立即启动一个新实例。

Run()

开始在当前线程上运行标准应用程序消息循环,而无需窗体。

Run(ApplicationContext)

开始在当前线程上运行标准应用程序消息循环,并运行一个 ApplicationContext

Run(Form)

开始在当前线程上运行标准应用程序消息循环,并使指定的窗体可见。

SetColorMode(SystemColorMode)

设置应用程序的默认颜色模式(深色模式)。

SetCompatibleTextRenderingDefault(Boolean)

为特定控件上定义的属性设置应用程序范围的默认值 UseCompatibleTextRendering

SetDefaultFont(Font)

设置进程的默认值 Font

SetHighDpiMode(HighDpiMode)

设置进程的高 DPI 模式。

SetSuspendState(PowerState, Boolean, Boolean)

暂停或休眠系统,或请求系统暂停或休眠。

SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean)

指示应用程序如何响应未经处理的异常,可以选择应用特定于线程的行为。

SetUnhandledExceptionMode(UnhandledExceptionMode)

指示应用程序如何响应未经处理的异常。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
UnregisterMessageLoop()

注销使用 RegisterMessageLoop(Application+MessageLoopCallback). 进行的消息循环回调。

活动

名称 说明
ApplicationExit

当应用程序即将关闭时发生。

EnterThreadModal

当应用程序即将进入模式状态时发生。

Idle

当应用程序完成处理并且即将进入空闲状态时发生。

LeaveThreadModal

当应用程序即将离开模式状态时发生。

ThreadException

引发未捕获的线程异常时发生。

ThreadExit

当线程即将关闭时发生。 当应用程序的主线程即将关闭时,将首先引发此事件,然后引发一个 ApplicationExit 事件。

适用于