Application 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供 static 方法與屬性以管理應用程式,例如啟動與停止應用程式的方法、處理Windows訊息,以及取得應用程式資訊的屬性。 此類別無法獲得繼承。
public ref class Application sealed
public sealed class Application
type Application = class
Public NotInheritable Class Application
- 繼承
-
Application
範例
以下程式碼範例在表單的清單框中列出數字。 每次點擊 button1,應用程式都會在列表中新增一個數字。
方法呼叫MainRun啟動應用程式,建立表單,listBox1且button1。 當使用者點擊 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 啟動當前執行緒的應用程式訊息迴圈,並可選擇性地顯示表單。
Exit 或 ExitThread 停止訊息迴圈。
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 |
|---|---|
| ApplicationExit |
當應用程式即將關閉時會發生。 |
| EnterThreadModal |
當應用程式即將進入模態狀態時發生。 |
| Idle |
當應用程式完成處理並即將進入閒置狀態時發生。 |
| LeaveThreadModal |
當應用程式即將離開模態狀態時,會發生這種情況。 |
| ThreadException |
當拋出未被捕捉的執行緒例外時,會發生這種情況。 |
| ThreadExit |
當執行緒即將關閉時會發生。 當應用程式的主執行緒即將關閉時,先提出此事件,接著是 ApplicationExit 事件。 |