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.

方法會呼叫Run來啟動應用程式,以建立表單listBox1button1MainThe Main method calls Run to start the application, which creates the form, listBox1 and button1. 當使用者按一下button1button1_Click ,方法會顯示MessageBoxWhen the user clicks button1, the button1_Click method displays a MessageBox. 如果使用者按一下No MessageBox,方法會在button1_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.

注意

在部分信任Exit中,對的呼叫將會失敗。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

備註

Application類別具有啟動和停止應用程式和執行緒,以及處理 Windows 訊息的方法,如下所示:The Application class has methods to start and stop applications and threads, and to process Windows messages, as follows:

  • Run在目前的執行緒上啟動應用程式訊息迴圈,並選擇性地使表單可見。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 的淺層複本 (Shallow Copy)。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)

註冊回呼 (Callback),以檢查訊息迴圈是否在裝載環境中執行。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

發生在應用程式處理完畢並即將進入閒置 (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

發生於未被截取的執行緒擲回例外狀況 (Exception) 時。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.

適用於