如何:从命令行创建 Windows 窗体应用程序

it2025-03-18  20

如何:从命令行创建 Windows 窗体应用程序

下面的过程描述了从命令行创建和运行 Windows 窗体应用程序时必须完成的基本步骤。Visual Studio 中对这些过程提供了广泛的支持。 有关更多信息,请参见Walkthrough: Creating a Simple Windows Form和Walkthrough: Creating a Simple Windows Form和Walkthrough: Creating a Simple Windows Form。

过程

创建窗体

在空代码文件中,键入下面的导入或使用语句:

Visual Basic 复制代码 Imports System Imports System.ComponentModel Imports System.Drawing Imports System.Windows.Forms C# 复制代码 using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms;

声明一个从 Form 类继承的名为 Form1 的类。

Visual Basic 复制代码 Public Class Form1 Inherits Form C# 复制代码 public class Form1 : Form

为 Form1 创建默认构造函数。

您将在随后的过程中向构造函数添加更多代码。

Visual Basic 复制代码 Public Sub New() End Sub 'New C# 复制代码 public Form1() {}

向该类添加 Main 方法

将 STAThreadAttribute 应用于 Main 方法,以指定 Windows 窗体应用程序是单线程单元。

调用 EnableVisualStyles,使应用程序具有 Windows XP 外观。

创建窗体实例,并运行它。

Visual Basic 复制代码 <STAThread()> _ Public Shared Sub Main() Application.EnableVisualStyles() Application.Run(New Form1()) End Sub End Class C# 复制代码 [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }

编译并运行应用程序

在 .NET Framework 命令提示符处,定位到在其中创建 Form1 类的目录。

编译窗体。

如果使用 C#,请键入:csc form1.cs

- 或 -

如果使用 Visual Basic,请键入:vbc form1.vb /r:system.dll,system.drawing.dll,system.windows.forms.dll

在命令提示处,键入:Form1.exe

添加控件并处理事件

前面的过程步骤演示了如何创建可编译和运行的基本 Windows 窗体。下面的过程将演示如何创建控件并将控件添加到窗体上,然后处理该控件的事件。有关可以添加到 Windows 窗体上的控件的更多信息,请参见 Windows 窗体控件。

除了了解如何创建 Windows 窗体应用程序以外,应当了解基于事件的编程以及如何处理用户输入。有关更多信息,请参见在 Windows 窗体中创建事件处理程序和处理用户输入。

声明按钮控件并处理它的单击事件

声明名为 button1 的按钮控件。

在构造函数中,创建按钮,并设置它的 Size、Location 和 Text 属性。

将按钮添加到窗体上。

下面的代码示例演示如何声明按钮控件:

Visual Basic 复制代码 Public WithEvents button1 As Button Public Sub New() button1 = New Button() button1.Size = New Size(40, 40) button1.Location = New Point(30, 30) button1.Text = "Click me" Me.Controls.Add(button1) AddHandler button1.Click, AddressOf button1_Click End Sub C# 复制代码 public Button button1; public Form1() { button1 = new Button(); button1.Size = new Size(40, 40); button1.Location = new Point(30, 30); button1.Text = "Click me"; this.Controls.Add(button1); button1.Click += new EventHandler(button1_Click); }

创建用于处理按钮的 Click 事件的方法。

在单击事件处理程序中,显示带有消息“Hello World”的 MessageBox。

下面的代码示例演示如何处理按钮控件的单击事件。

Visual Basic 复制代码 Sub button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles button1.Click MessageBox.Show("Hello World") End Sub C# 复制代码 void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello World"); }

将 Click 事件与所创建的方法关联。

下面的代码示例演示如何将事件与方法关联。

Visual Basic 复制代码 Sub button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles button1.Click C# 复制代码 button1.Click += new EventHandler(button1_Click);

按照前面过程中的描述,编译并运行应用程序。

示例

下面的代码示例是上述过程的完整示例。

Visual Basic 复制代码 Imports System Imports System.ComponentModel Imports System.Drawing Imports System.Windows.Forms Public Class Form1 Inherits Form Public WithEvents button1 As Button Public Sub New() button1 = New Button() button1.Size = New Size(40, 40) button1.Location = New Point(30, 30) button1.Text = "Click me" Me.Controls.Add(button1) AddHandler button1.Click, AddressOf button1_Click End Sub Sub button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles button1.Click MessageBox.Show("Hello World") End Sub <STAThread()> _ Shared Sub Main() Application.EnableVisualStyles() Application.Run(New Form1()) End Sub End Class C# 复制代码 using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace FormWithButton { public class Form1 : Form { public Button button1; public Form1() { button1 = new Button(); button1.Size = new Size(40, 40); button1.Location = new Point(30, 30); button1.Text = "Click me"; this.Controls.Add(button1); button1.Click += new EventHandler(button1_Click); } void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello World"); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } } }

编译代码

若要编译代码,请按照上面过程中描述如何编译和运行应用程序的说明进行操作。

请参见

参考

Form Control

其他资源

更改 Windows 窗体外观 增强 Windows 窗体应用程序 Windows 窗体入门

转载于:https://www.cnblogs.com/HappyQQ/archive/2008/01/19/1045600.html

最新回复(0)