65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Windows Forms

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (9投票s)

2008年1月5日

CPOL

6分钟阅读

viewsIcon

35316

本文旨在解释 Forms 以及如何将逻辑插入到 UI 中。

学习 Windows Forms:编写和部署 Visual Basic 程序

本文面向 Visual Basic 和 Microsoft Visual Studio 2005(或 2008 beta)的初学者。本文假定读者具备 Visual Basic 2005 的基础到中级知识,以及其向 Visual Basic 2006 的迁移知识。

本文在很大程度上引用了 MSDN 库和 Microsoft 的教育性著作。我的目的是描述 Windows Forms 的基础知识,以及开发者如何通过 OLE 统一数据传输(OLE Uniform Data Transfer)为用户界面添加逻辑。MFC 和 ATL 等框架的创建是为了帮助 Windows 应用程序开发者更专注于解决业务问题,而不是处理 Windows 消息。然而,从 16 位 DOS 到 Windows 的过渡带来了 Windows 消息的引入。对于鼠标的每一次移动(或键盘的每一次按键),Windows 操作系统都会创建一个消息发送给相关的应用程序。MFC 和 ATL 框架提供了 Windows 应用程序的“管道”或“模板”。之后,就取决于开发者自己构建业务逻辑了。虽然这些框架简化了 Windows 应用程序的开发过程,但它们并不提供将更多逻辑插入用户界面所需的控件。这就是为什么许多开发者选择 Visual Basic 的原因。为了理解如何开发一个应用程序,将其转换为可执行文件,甚至可能将其作为可分发版本进行部署,有必要从基础开始。

  • 使用 Visual Studio 工具箱中提供的控件创建用户界面。
  • 通过设置控件的属性来自定义这些控件。
  • 准确确定你的程序应该做什么,然后相应地编写代码。

至少,一个 Windows Forms 应用程序包含以下元素:

  • 一个或多个派生自 System.Windows.Forms.Form 的类。
  • 一个 main 方法,该方法调用 staticShared)的 System.Windows.Forms.Application.Run 方法,并将一个 Form 实例传递给它。

应用程序的 Run 方法处理来自操作系统到应用程序的消息。

以下代码说明了 Windows Forms 应用程序的必要元素:

Option Explicit          //all variables must be declared prior to use
Option Strict

Imports System
Import System.Windows.Forms

Public Class MyForm
  Inherits Form
  Public Sub New()
       Me.Text = "Hello, World!"
  End Sub 'New

<stathread>
Public Shared Sub Main()
  Dim aform as New MyForm()
    ' the Application.Run method processes messages from the operating system
    ' to your application. If you comment the next lines of code,
    ' your application will compile and execute, but because it is not in the message loop
    ' it will execute after an instance of the form is created
 Application.Run(aform)
 End Sub
End Class

要在 .NET Framework 上编译此代码:vbc.exe /t:winexe your_code.vb

理解消息循环的一个简单方法是想象一个名为**拖放(drag and drop)**的数据传输过程。拖动类似于剪切/复制;放置类似于粘贴。这发生在你在桌面上拖动一个图标进行传输时。当操作开始时(即,当源检测到适当的鼠标点击时),源将它自己的数据对象和 IDropSource 接口传递给 OLE,OLE 进入一个消息循环,该循环会检测鼠标事件和键盘事件(标准输入)。当鼠标悬停在目标上(或更确切地说,一个已注册的窗口)后放置数据对象,OLE 会告诉目标 IDropTarget。数据对象被放置,消息循环完成。我们回顾一下,许多 IDE 都拥有支持拖放操作的工具箱。

所以,这里有一个使用 Microsoft Visual Studio 2005 开发的程序:Lucky Seven。

这个程序将在每次出现数字七时输出一个风景优美的图形图像。你点击“Spin”按钮,如果三个数字输出框中的一个出现七,则图像就会出现。

编程步骤

编程步骤 项目数量
创建用户界面 7 个数据对象
设置属性 13 个属性
编写程序代码 2 个对象

创建用户界面

创建一个新项目。

  1. 启动 Visual Studio
  2. 在“文件”菜单上,单击“新建项目”。
  3. 单击“Windows 应用程序”模板。
  4. 在名称框中,键入 myGame。
  5. 点击“确定”。

为了设置窗体的大小,将鼠标指针放在右下角,直到其形状变为调整大小指针,然后拖动直到窗体足够大以容纳将要拖到其上的对象:Form1 属性容器的大小应设置为大约 485 像素宽 x 278 像素高。现在转到菜单上的“视图”选项,然后单击“工具箱”选项卡。使用标准工具,双击工具箱中的 Button 控件,然后将鼠标指针移开。您会注意到按钮会自动吸附到左上角。将鼠标放在“Button1”上,使其变为四向箭头,然后将按钮向下右拖动。对将要放置在第一个按钮下方的下一个按钮重复此过程。现在我们将向程序添加标签,当我们的按钮在程序运行时被按下时,这些标签将作为用户界面元素来显示数字。双击标签按钮,然后将其拖到 Button1 的右侧。执行此操作,直到第一个按钮的右侧有三个标签框。双击第四个标签对象,然后将其拖到 Button2 的下方。请记住,当按下“Spin”按钮并且出现数字七时,图形对象就会出现;因此,我们需要单击工具箱中的图像(或 Picture Box)对象。界面已创建,现在我们必须设置属性。

设置属性

设置按钮属性
  1. 按 F4 键打开“属性”窗口。
  2. 单击第一个按钮(Button1)。
  3. 滚动到 Text 属性。
  4. 双击文本属性窗口。
  5. 键入“Spin”。
  6. Button2 重复此操作,并键入“End”。

对于标签对象的属性,将 AutoSize 属性设置为 false。将 TextAlign 属性设置为 MiddleCenter。单击 BorderStyle 属性,将其设置为 FixedSingle。单击 Font 属性,然后单击省略号按钮(旁边有三个点的按钮,位于当前字体设置旁边)。将字体更改为 TimesNewRoman,字体样式更改为 Bold,字号更改为 24。现在双击 Text 属性,并在上面三个标签框中键入数字 0。对于第四个标签框,将 Text 属性更改为 LuckySeven。将字体属性更改为 Ariel,字体样式更改为 Bold,字号更改为 18。将 AutoSize 属性设置为 true(仅适用于 Label4)。下面的 Visual Basic 源代码应该有助于阐明这一推理。

Public Class Form1 

    Private Sub Button2_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Visible = False    'hide picture
        Label1.Text = CStr(Int(Rnd() * 10))    'pick numbers
        Label2.Text = CStr(Int(Rnd() * 10))
        Label3.Text = CStr(Int(Rnd() * 10))
        ' if any number is 7 display picture and beep
        If (Label1.Text = "7") Or (Label2.Text = "7") _
        Or (Label3.Text = "7") Then
            PictureBox1.Visible = True
            Beep()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
    End Sub
End Class

现在是 PictureBox 属性

设置 Picture Box 属性
  1. 单击窗体上的 PictureBox
  2. SizeMode 属性设置为 StretchImage
  3. 将图片大小调整为 148 像素宽 x 138 像素高。
  4. 双击 Image 属性,然后单击省略号按钮。
  5. 将出现“选择资源”对话框。
  6. 在“打开”对话框中,导航到解压下载文件所在的位置。
  7. 选择 PayCoins.jpg,然后单击“打开”。
  8. Visible 属性设置为 False

这是在容器解决方案中生成的代码:

<global.microsoft.visualbasic.compilerservices.designergenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <system.diagnostics.debuggernonusercode() /> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    <system.diagnostics.debuggerstepthrough() /> _
    Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = _
                      New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(12, 12)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Spin"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(12, 41)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(75, 24)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "End"
        '
        'Label1
        '
        Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label1.Font = New System.Drawing.Font("Times New Roman", _
           24.0!, System.Drawing.FontStyle.Bold, _
           System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(125, 25)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(71, 57)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "0"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label2
        '
        Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label2.Font = New System.Drawing.Font("Times New Roman", _
           24.0!, System.Drawing.FontStyle.Bold, _
           System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(214, 25)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(70, 57)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "0"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label3
        '
        Me.Label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label3.Font = New System.Drawing.Font("Times New Roman", _
           24.0!, System.Drawing.FontStyle.Bold, _
           System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(304, 25)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(71, 57)
        Me.Label3.TabIndex = 4
        Me.Label3.Text = "0"
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Font = New System.Drawing.Font("Arial", 18.0!, _
           System.Drawing.FontStyle.Bold, _
           System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label4.ForeColor = System.Drawing.Color.Purple
        Me.Label4.Location = New System.Drawing.Point(12, 104)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(153, 29)
        Me.Label4.TabIndex = 5
        Me.Label4.Text = "Lucky Seven"
        '
        'PictureBox1
        '
        Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), _
                                     System.Drawing.Image)
        Me.PictureBox1.Location = New System.Drawing.Point(214, 94)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(148, 138)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox1.TabIndex = 6
        Me.PictureBox1.TabStop = False
        Me.PictureBox1.Visible = False
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(438, 244)
        Me.Controls.Add(Me.PictureBox1)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

End Class
© . All rights reserved.