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

以下过程描述了必须完成的基本步骤,以便从命令行创建和运行 Windows 窗体应用程序。 Visual Studio 中广泛支持这些过程。 另请参阅演练:在 WPF 中承载 Windows 窗体控件

程序

若要创建窗体

  1. 在空代码文件中,键入以下 Importsusing 语句:

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Windows.Forms
    
  2. 声明一个名为 Form1 的类,该类继承自 Form 类:

    public class Form1 : Form
    
    Public Class Form1
        Inherits Form
    
  3. Form1创建无参数构造函数。

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

    public Form1() {}
    
    Public Sub New()
    
    End Sub
    
  4. 向类添加 Main 方法。

    1. STAThreadAttribute 应用到 C# Main 方法,以指定 Windows 窗体应用程序是一个单线程单元。 (Visual Basic 中不需要此属性,因为使用 Visual Basic 开发的 Windows 窗体应用程序默认使用单线程单元模型。)

    2. 调用 EnableVisualStyles 将操作系统样式应用于应用程序。

    3. 创建窗体的实例并运行它。

    [STAThread]
    public static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
    
    
        Public Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
    
        End Sub
    End Class
    

编译并运行应用程序

  1. 在 .NET Framework 命令提示符处,导航到创建 Form1 类的目录。

  2. 完成表单。

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

      -or-

    • 如果使用 Visual Basic,请键入:vbc form1.vb

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

添加控件并处理事件

前面的过程步骤演示了如何创建编译和运行的基本 Windows 窗体。 下一过程将演示如何创建控件并将其添加到窗体,并处理控件的事件。 有关可添加到 Windows 窗体的控件的详细信息,请参阅 Windows 窗体控件

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

若要声明一个按钮控件和处理其 click 事件

  1. 请声明一个名为 button1 的按钮控件。

  2. 在构造函数中,创建按钮并设置其 SizeLocationText 属性。

  3. 向窗体添加按钮。

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

    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);
    }
    
    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)
       
    End Sub
    
  4. 创建用于处理按钮 Click 事件的方法。

  5. 在 Click 事件处理程序中,显示带有消息“Hello World”的 MessageBox

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

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Hello World");
    }
    
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
        MessageBox.Show("Hello World")
    End Sub
    
  6. Click 事件与创建的方法相关联。

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

    button1.Click += new EventHandler(button1_Click);
    
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
    
  7. 按照前面的过程所述编译并运行应用程序。

下面的代码示例是前面的过程的完整示例:

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);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World");
        }
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }
}
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)
       
    End Sub
   
    Private 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

另请参阅