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

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

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

过程

创建窗体

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

    Visual Basic
    Imports System
        Imports System.ComponentModel
        Imports System.Drawing
        Imports System.Windows.Forms
        
    using System;
        using System.ComponentModel;
        using System.Drawing;
        using System.Windows.Forms;
        
  2. 声明一个从 Form 类继承的名为 Form1 的类。

    Visual Basic
    Public Class Form1
        Inherits Form
        
    public class Form1 : Form
        
  3. Form1 创建默认构造函数。

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

    Visual Basic
    Public Sub New()
        End Sub 'New
        
    public Form1() {}
        
  4. 向该类添加 Main 方法

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

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

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

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

编译并运行应用程序

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

  2. 编译窗体。

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

      - 或 -

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

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

添加控件并处理事件

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

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

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

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

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

  3. 将按钮添加到窗体上。

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

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

  5. 在单击事件处理程序中,显示带有消息“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
        
    void button1_Click(object sender, EventArgs e)
        {
        MessageBox.Show("Hello World");
        }
        
  6. Click 事件与所创建的方法关联。

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

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

示例

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

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
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());
}
}
}

编译代码

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

请参见

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值