Application 類別

定義

提供 static 方法與屬性以管理應用程式,例如啟動與停止應用程式的方法、處理Windows訊息,以及取得應用程式資訊的屬性。 此類別無法獲得繼承。

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

範例

以下程式碼範例在表單的清單框中列出數字。 每次點擊 button1,應用程式都會在列表中新增一個數字。

方法呼叫MainRun啟動應用程式,建立表單,listBox1button1。 當使用者點擊 button1時, button1_Click 方法會顯示一個 MessageBox。 如果使用者點擊 NoMessageBox,該 button1_Click 方法會在列表中加入一個數字。 如果使用者點擊 Yes,應用程式會呼叫 Exit 處理佇列中所有剩餘訊息,然後退出。

Note

呼喚 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屬性,可以用來取得或設定當前執行緒的文化資訊。

您無法建立這個類別的實體。

屬性

名稱 Description
AllowQuit

會取得一個值,表示呼叫者是否可以退出此應用程式。

CommonAppDataPath

取得所有使用者共享的應用程式資料路徑。

CommonAppDataRegistry

取得所有使用者共享的應用程式登錄檔金鑰。

CompanyName

這樣會取得與應用程式相關的公司名稱。

CurrentCulture

取得或設定當前執行緒的文化資訊。

CurrentInputLanguage

取得或設定目前執行緒的輸入語言。

ExecutablePath

取得啟動應用程式的可執行檔路徑,包括執行檔名稱。

LocalUserAppDataPath

取得本地非漫遊使用者的應用程式資料路徑。

MessageLoop

會取得一個值,表示此執行緒中是否存在訊息迴圈。

OpenForms

會拿到一組由應用程式擁有的開放表單。

ProductName

取得與此應用程式相關的產品名稱。

ProductVersion

取得與此應用程式相關的產品版本。

RenderWithVisualStyles

會取得一個值,指定目前應用程式是否繪製帶有視覺樣式的控制項。

SafeTopLevelCaptionFormat

取得或設定格式字串,當頂層視窗標題以警告橫幅顯示時套用。

StartupPath

取得啟動應用程式的可執行檔路徑,但不含執行檔名稱。

UserAppDataPath

取得使用者應用程式資料的路徑。

UserAppDataRegistry

取得使用者應用程式資料的登錄檔金鑰。

UseWaitCursor

取得或設定等待游標是否適用於所有開啟的應用程式形式。

VisualStyleState

會有一個值,指定視覺樣式如何套用到應用程式視窗。

方法

名稱 Description
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 的全應用程式預設值。

SetSuspendState(PowerState, Boolean, Boolean)

暫停或休眠系統,或請求暫停或休眠。

SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean)

指示應用程式如何回應未處理的異常,並可選擇執行緒特定的行為。

SetUnhandledExceptionMode(UnhandledExceptionMode)

指示應用程式如何回應未處理的異常。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
UnregisterMessageLoop()

取消用 的訊息迴圈回撥 RegisterMessageLoop(Application+MessageLoopCallback)

事件

名稱 Description
ApplicationExit

當應用程式即將關閉時會發生。

EnterThreadModal

當應用程式即將進入模態狀態時發生。

Idle

當應用程式完成處理並即將進入閒置狀態時發生。

LeaveThreadModal

當應用程式即將離開模態狀態時,會發生這種情況。

ThreadException

當拋出未被捕捉的執行緒例外時,會發生這種情況。

ThreadExit

當執行緒即將關閉時會發生。 當應用程式的主執行緒即將關閉時,先提出此事件,接著是 ApplicationExit 事件。

適用於