Application 类

定义

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

public ref class Application sealed
public sealed class Application
type Application = class
Public NotInheritable Class Application
继承
Application

示例

下面的代码示例列出窗体上列表框中的数字。 每次单击 button1时,应用程序都会向列表中添加另一个数字。

方法 Main 调用 Run 以启动应用程序,这将创建窗体 listBox1button1。 当用户单击 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

获取指示调用方能否退出该应用程序的值。

CommonAppDataPath

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

CommonAppDataRegistry

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

CompanyName

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

CurrentCulture

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

CurrentInputLanguage

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

ExecutablePath

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

HighDpiMode

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

LocalUserAppDataPath

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

MessageLoop

获取指示该线程上是否存在消息循环的值。

OpenForms

获取为应用程序所有的已打开窗体的集合。

ProductName

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

ProductVersion

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

RenderWithVisualStyles

获取指定当前应用程序是否使用可视样式绘制控件的值。

SafeTopLevelCaptionFormat

获取或设置当顶级窗口标题与版权警告标志一起显示时,要应用于顶级窗口标题的格式字符串。

StartupPath

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

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)

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

SetCompatibleTextRenderingDefault(Boolean)

UseCompatibleTextRendering 在某些控件上定义的 属性设置应用程序范围的默认值。

SetDefaultFont(Font)

设置进程的默认值 Font

SetHighDpiMode(HighDpiMode)

设置进程的高 DPI 模式。

SetSuspendState(PowerState, Boolean, Boolean)

挂起系统或使系统休眠,或者请求系统挂起或休眠。

SetUnhandledExceptionMode(UnhandledExceptionMode)

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

SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean)

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

ToString()

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

(继承自 Object)
UnregisterMessageLoop()

注销使用 RegisterMessageLoop(Application+MessageLoopCallback) 发出的消息循环回调。

事件

ApplicationExit

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

EnterThreadModal

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

Idle

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

LeaveThreadModal

当应用程序即将退出模式状态时发生。

ThreadException

在发生未捕获线程异常时发生。

ThreadExit

在某线程即将关闭时发生。 当应用程序的主线程即将关闭时,首先引发该事件,随后是 ApplicationExit 事件。

适用于