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)

註冊回呼 (Callback),以檢查訊息迴圈是否在裝載環境中執行。

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

發生在應用程式處理完畢並即將進入閒置 (Idle) 狀態時。

LeaveThreadModal

在應用程式即將離開強制回應狀態時發生。

ThreadException

發生於未被截取的執行緒擲回例外狀況 (Exception) 時。

ThreadExit

發生在執行緒即將關閉時。 當應用程式的主執行緒即將關閉時,會先引發這個事件,接著是 ApplicationExit 事件。

適用於