Application 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供 static 方法和属性来管理应用程序(例如启动和停止应用程序的方法),以处理Windows消息和属性以获取有关应用程序的信息。 此类不能被继承。
public ref class Application sealed
public sealed class Application
type Application = class
Public NotInheritable Class Application
- 继承
-
Application
示例
下面的代码示例列出窗体上列表框中的数字。 每次单击 button1时,应用程序都会向列表中添加另一个数字。
该方法 Main 调用 Run 以启动应用程序,该应用程序将创建窗体, listBox1 以及 button1。 当用户单击 button1时,该方法 button1_Click 将显示一个 MessageBox。 如果用户单击 No 该 MessageBox数字,该方法会将 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 在当前线程上启动应用程序消息循环,并且(可选)使窗体可见。
Exit 或 ExitThread 停止消息循环。
DoEvents 在程序处于循环状态时处理消息。
AddMessageFilter向应用程序消息泵添加消息筛选器,以监视Windows消息。
IMessageFilter 允许在调用事件处理程序之前停止引发或执行特殊操作的事件。
此类具有和CurrentCultureCurrentInputLanguage属性来获取或设置当前线程的区域性信息。
不能创建此类的实例。
属性
| 名称 | 说明 |
|---|---|
| AllowQuit |
获取一个值,该值指示调用方是否可以退出此应用程序。 |
| ColorMode |
获取应用程序的默认颜色模式(深色模式)。 |
| CommonAppDataPath |
获取所有用户之间共享的应用程序数据的路径。 |
| CommonAppDataRegistry |
获取所有用户之间共享的应用程序数据的注册表项。 |
| CompanyName |
获取与应用程序关联的公司名称。 |
| CurrentCulture |
获取或设置当前线程的区域性信息。 |
| CurrentInputLanguage |
获取或设置当前线程的当前输入语言。 |
| ExecutablePath |
获取启动应用程序的可执行文件的路径,包括可执行文件名称。 |
| HighDpiMode |
获取应用程序的当前高 DPI 模式。 |
| IsDarkModeEnabled |
获取一个值,该值指示应用程序是否在深色系统颜色上下文中运行。 |
| LocalUserAppDataPath |
获取本地非漫游用户的应用程序数据的路径。 |
| MessageLoop |
获取一个值,该值指示此线程上是否存在消息循环。 |
| OpenForms |
获取应用程序拥有的打开窗体的集合。 |
| ProductName |
获取与此应用程序关联的产品名称。 |
| ProductVersion |
获取与此应用程序关联的产品版本。 |
| RenderWithVisualStyles |
获取一个值,该值指定当前应用程序是否使用视觉样式绘制控件。 |
| SafeTopLevelCaptionFormat |
获取或设置在显示带有警告横幅时要应用于顶级窗口标题的格式字符串。 |
| StartupPath |
获取启动应用程序的可执行文件的路径,不包括可执行文件名称。 |
| SystemColorMode |
获取 OS 系统环境的系统颜色模式设置。 |
| UserAppDataPath |
获取用户的应用程序数据的路径。 |
| UserAppDataRegistry |
获取用户的应用程序数据的注册表项。 |
| UseVisualStyles |
获取一个值,该值指示是否为应用程序启用视觉样式。 |
| UseWaitCursor |
获取或设置是否将等待游标用于应用程序的所有打开形式。 |
| VisualStyleState |
获取一个值,该值指定如何将视觉样式应用于应用程序窗口。 |
方法
活动
| 名称 | 说明 |
|---|---|
| ApplicationExit |
当应用程序即将关闭时发生。 |
| EnterThreadModal |
当应用程序即将进入模式状态时发生。 |
| Idle |
当应用程序完成处理并且即将进入空闲状态时发生。 |
| LeaveThreadModal |
当应用程序即将离开模式状态时发生。 |
| ThreadException |
引发未捕获的线程异常时发生。 |
| ThreadExit |
当线程即将关闭时发生。 当应用程序的主线程即将关闭时,将首先引发此事件,然后引发一个 ApplicationExit 事件。 |