Application Application Application Application Class

定义

提供 static 方法和属性以管理应用程序,例如启动和停止应用程序、处理 Windows 消息的方法和获取应用程序信息的属性。Provides static methods and properties to manage an application, such as methods to start and stop an application, to process Windows messages, and properties to get information about an application. 此类不能被继承。This class cannot be inherited.

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

示例

下面的代码示例在窗体上的列表框中列出了数字。The following code example lists numbers in a list box on a form. 每次单击 button1 时,应用程序都会向列表中添加另一个编号。Each time you click button1, the application adds another number to the list.

@No__t-0 方法调用 Run 以启动应用程序,该应用程序将创建窗体,listBox1 和 @no__t。The Main method calls Run to start the application, which creates the form, listBox1 and button1. 当用户单击 button1 时,button1_Click 方法将显示 MessageBoxWhen the user clicks button1, the button1_Click method displays a MessageBox. 如果用户在 MessageBox 上单击 Nobutton1_Click 方法将向列表中添加一个数字。If the user clicks No on the MessageBox, the button1_Click method adds a number to the list. 如果用户单击 Yes,则应用程序会调用 Exit 处理队列中所有剩余的消息,然后退出。If the user clicks Yes, the application calls Exit to process all remaining messages in the queue and then to quit.

备注

在部分信任环境中,对 @no__t 的调用将失败。The call to Exit will fail in partial trust.

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

注解

@No__t-0 类具有启动和停止应用程序和线程以及处理 Windows 消息的方法,如下所示:The Application class has methods to start and stop applications and threads, and to process Windows messages, as follows:

  • @no__t 在当前线程上启动应用程序消息循环,并且可以选择使窗体可见。Run starts an application message loop on the current thread and, optionally, makes a form visible.

  • ExitExitThread 将停止消息循环。Exit or ExitThread stops a message loop.

  • DoEvents 在程序处于循环中时处理消息。DoEvents processes messages while your program is in a loop.

  • AddMessageFilter 将消息筛选器添加到应用程序消息泵以监视 Windows 消息。AddMessageFilter adds a message filter to the application message pump to monitor Windows messages.

  • IMessageFilter 使你可以停止引发事件,或在调用事件处理程序之前执行特殊操作。IMessageFilter lets you stop an event from being raised or perform special operations before invoking an event handler.

此类有 CurrentCultureCurrentInputLanguage 属性,可获取或设置当前线程的区域性信息。This class has CurrentCulture and CurrentInputLanguage properties to get or set culture information for the current thread.

不能创建此类的实例。You cannot create an instance of this class.

属性

AllowQuit AllowQuit AllowQuit AllowQuit

获取指示调用方能否退出该应用程序的值。Gets a value indicating whether the caller can quit this application.

CommonAppDataPath CommonAppDataPath CommonAppDataPath CommonAppDataPath

获取所有用户共享的应用程序数据的路径。Gets the path for the application data that is shared among all users.

CommonAppDataRegistry CommonAppDataRegistry CommonAppDataRegistry CommonAppDataRegistry

获取所有用户共享的应用程序数据的注册表项。Gets the registry key for the application data that is shared among all users.

CompanyName CompanyName CompanyName CompanyName

获取与该应用程序关联的公司名称。Gets the company name associated with the application.

CurrentCulture CurrentCulture CurrentCulture CurrentCulture

获取或设置当前线程的区域性信息。Gets or sets the culture information for the current thread.

CurrentInputLanguage CurrentInputLanguage CurrentInputLanguage CurrentInputLanguage

获取或设置当前线程的当前输入语言。Gets or sets the current input language for the current thread.

ExecutablePath ExecutablePath ExecutablePath ExecutablePath

获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。Gets the path for the executable file that started the application, including the executable name.

HighDpiMode HighDpiMode HighDpiMode HighDpiMode

获取应用程序的当前高 DPI 模式。Gets the current high DPI mode for the application.

LocalUserAppDataPath LocalUserAppDataPath LocalUserAppDataPath LocalUserAppDataPath

获取本地、非漫游用户的应用程序数据的路径。Gets the path for the application data of a local, non-roaming user.

MessageLoop MessageLoop MessageLoop MessageLoop

获取指示该线程上是否存在消息循环的值。Gets a value indicating whether a message loop exists on this thread.

OpenForms OpenForms OpenForms OpenForms

获取为应用程序所有的已打开窗体的集合。Gets a collection of open forms owned by the application.

ProductName ProductName ProductName ProductName

获取与该应用程序关联的产品名称。Gets the product name associated with this application.

ProductVersion ProductVersion ProductVersion ProductVersion

获取与该应用程序关联的产品版本。Gets the product version associated with this application.

RenderWithVisualStyles RenderWithVisualStyles RenderWithVisualStyles RenderWithVisualStyles

获取指定当前应用程序是否使用可视样式绘制控件的值。Gets a value specifying whether the current application is drawing controls with visual styles.

SafeTopLevelCaptionFormat SafeTopLevelCaptionFormat SafeTopLevelCaptionFormat SafeTopLevelCaptionFormat

获取或设置当顶级窗口标题与版权警告标志一起显示时,要应用于顶级窗口标题的格式字符串。Gets or sets the format string to apply to top-level window captions when they are displayed with a warning banner.

StartupPath StartupPath StartupPath StartupPath

获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。Gets the path for the executable file that started the application, not including the executable name.

UserAppDataPath UserAppDataPath UserAppDataPath UserAppDataPath

获取用户的应用程序数据的路径。Gets the path for the application data of a user.

UserAppDataRegistry UserAppDataRegistry UserAppDataRegistry UserAppDataRegistry

获取用户的应用程序数据的注册表项。Gets the registry key for the application data of a user.

UseVisualStyles UseVisualStyles UseVisualStyles UseVisualStyles

获取一个值,该值指示是否为应用程序启用视觉样式。Gets a value that indicates whether visual styles are enabled for the application.

UseWaitCursor UseWaitCursor UseWaitCursor UseWaitCursor

获取或设置是否对应用程序的所有打开窗体使用等待光标。Gets or sets whether the wait cursor is used for all open forms of the application.

VisualStyleState VisualStyleState VisualStyleState VisualStyleState

获取指定可视样式如何应用于应用程序窗口的值。Gets a value that specifies how visual styles are applied to application windows.

方法

AddMessageFilter(IMessageFilter) AddMessageFilter(IMessageFilter) AddMessageFilter(IMessageFilter) AddMessageFilter(IMessageFilter)

添加消息筛选器以便在向目标传送 Windows 消息时监视这些消息。Adds a message filter to monitor Windows messages as they are routed to their destinations.

DoEvents() DoEvents() DoEvents() DoEvents()

处理当前在消息队列中的所有 Windows 消息。Processes all Windows messages currently in the message queue.

EnableVisualStyles() EnableVisualStyles() EnableVisualStyles() EnableVisualStyles()

启用应用程序的可视样式。Enables visual styles for the application.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Exit() Exit() Exit() Exit()

通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口。Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

Exit(CancelEventArgs) Exit(CancelEventArgs) Exit(CancelEventArgs) Exit(CancelEventArgs)

通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口。Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

ExitThread() ExitThread() ExitThread() ExitThread()

退出当前线程上的消息循环,并关闭该线程上的所有窗口。Exits the message loop on the current thread and closes all windows on the thread.

FilterMessage(Message) FilterMessage(Message) FilterMessage(Message) FilterMessage(Message)

对窗口消息运行任何筛选器,并返回修改后的消息的副本。Runs any filters against a window message, and returns a copy of the modified message.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OleRequired() OleRequired() OleRequired() OleRequired()

初始化当前线程上的 OLE。Initializes OLE on the current thread.

OnThreadException(Exception) OnThreadException(Exception) OnThreadException(Exception) OnThreadException(Exception)

引发 ThreadException 事件。Raises the ThreadException event.

RaiseIdle(EventArgs) RaiseIdle(EventArgs) RaiseIdle(EventArgs) RaiseIdle(EventArgs)

在宿主方案中引发 Idle 事件。Raises the Idle event in hosted scenarios.

RegisterMessageLoop(Application+MessageLoopCallback) RegisterMessageLoop(Application+MessageLoopCallback) RegisterMessageLoop(Application+MessageLoopCallback) RegisterMessageLoop(Application+MessageLoopCallback)

注册一个回调以检查消息循环是否正在宿主环境中运行。Registers a callback for checking whether the message loop is running in hosted environments.

RemoveMessageFilter(IMessageFilter) RemoveMessageFilter(IMessageFilter) RemoveMessageFilter(IMessageFilter) RemoveMessageFilter(IMessageFilter)

从应用程序的消息泵移除一个消息筛选器。Removes a message filter from the message pump of the application.

Restart() Restart() Restart() Restart()

关闭应用程序并立即启动一个新实例。Shuts down the application and starts a new instance immediately.

Run() Run() Run() Run()

在没有窗体的情况下,在当前线程上开始运行标准应用程序消息循环。Begins running a standard application message loop on the current thread, without a form.

Run(ApplicationContext) Run(ApplicationContext) Run(ApplicationContext) Run(ApplicationContext)

在特定的 ApplicationContext 中,在当前线程上开始运行标准应用程序消息循环。Begins running a standard application message loop on the current thread, with an ApplicationContext.

Run(Form) Run(Form) Run(Form) Run(Form)

在当前线程上开始运行标准应用程序消息循环,并使指定窗体可见。Begins running a standard application message loop on the current thread, and makes the specified form visible.

SetCompatibleTextRenderingDefault(Boolean) SetCompatibleTextRenderingDefault(Boolean) SetCompatibleTextRenderingDefault(Boolean) SetCompatibleTextRenderingDefault(Boolean)

将某些控件上定义的 UseCompatibleTextRendering 属性设置为应用程序范围内的默认值。Sets the application-wide default for the UseCompatibleTextRendering property defined on certain controls.

SetHighDpiMode(HighDpiMode) SetHighDpiMode(HighDpiMode) SetHighDpiMode(HighDpiMode) SetHighDpiMode(HighDpiMode)

设置进程的高 DPI 模式。Sets the high DPI mode of the process.

SetSuspendState(PowerState, Boolean, Boolean) SetSuspendState(PowerState, Boolean, Boolean) SetSuspendState(PowerState, Boolean, Boolean) SetSuspendState(PowerState, Boolean, Boolean)

挂起系统或使系统休眠,或者请求系统挂起或休眠。Suspends or hibernates the system, or requests that the system be suspended or hibernated.

SetUnhandledExceptionMode(UnhandledExceptionMode) SetUnhandledExceptionMode(UnhandledExceptionMode) SetUnhandledExceptionMode(UnhandledExceptionMode) SetUnhandledExceptionMode(UnhandledExceptionMode)

指示应用程序如何响应未经处理的异常。Instructs the application how to respond to unhandled exceptions.

SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean) SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean) SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean) SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean)

指示应用程序如何响应未经处理的异常,同时可选择应用特定于线程的行为。Instructs the application how to respond to unhandled exceptions, optionally applying thread-specific behavior.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
UnregisterMessageLoop() UnregisterMessageLoop() UnregisterMessageLoop() UnregisterMessageLoop()

注销使用 RegisterMessageLoop(Application+MessageLoopCallback) 发出的消息循环回调。Unregisters the message loop callback made with RegisterMessageLoop(Application+MessageLoopCallback).

事件

ApplicationExit ApplicationExit ApplicationExit ApplicationExit

在应用程序即将关闭时发生。Occurs when the application is about to shut down.

EnterThreadModal EnterThreadModal EnterThreadModal EnterThreadModal

当应用程序即将进入模式状态时发生。Occurs when the application is about to enter a modal state.

Idle Idle Idle Idle

当应用程序完成处理并即将进入空闲状态时发生。Occurs when the application finishes processing and is about to enter the idle state.

LeaveThreadModal LeaveThreadModal LeaveThreadModal LeaveThreadModal

当应用程序即将退出模式状态时发生。Occurs when the application is about to leave a modal state.

ThreadException ThreadException ThreadException ThreadException

在发生未捕获线程异常时发生。Occurs when an untrapped thread exception is thrown.

ThreadExit ThreadExit ThreadExit ThreadExit

在某线程即将关闭时发生。Occurs when a thread is about to shut down. 当应用程序的主线程即将关闭时,首先引发该事件,随后是 ApplicationExit 事件。When the main thread for an application is about to be shut down, this event is raised first, followed by an ApplicationExit event.

适用于