雖然VB.NET和VB在語法上有很多相同之處,但從根本上說,VB.NET不僅僅是VB的另外一個(gè)升級(jí)版本,而是一個(gè)全新的語言。VB.NET全面支持面向?qū)ο螅赩B.NET中哪怕是一個(gè)字符串,你也都可以把它看成是一個(gè)對象,一個(gè)實(shí)例,也有自己的屬性和方法。同樣VB.NET中程序窗體(Form)也是一個(gè)實(shí)例,它是由命名空間“System.Windows.Forms”中的“Form”類通過構(gòu)造函數(shù)而成的一個(gè)實(shí)例。下面試著用VB.NET來編寫與窗體相關(guān)的程序。
利用VB.NET來定制窗體的透明度
在下面介紹的程序中,將通過調(diào)整TrackBar組件的數(shù)值來調(diào)整窗體的透明程度。定制透明的窗體,如果要用其他語言來實(shí)現(xiàn),一定是件很復(fù)雜的工作,但對于VB.NET來說,則是一件非常簡單的事情,這是因?yàn)樵?Net FrameWork SDK的“Form”類中,提供了一個(gè)可以設(shè)定窗體透明度的屬性“Opacity”,當(dāng)“Opacity”屬性值為“1”,說明窗體不透明,當(dāng)“Opacity”為“0”,則窗體完全透明。
構(gòu)造整個(gè)程序的主要思路就是:首先要繼承一個(gè)Form對象,程序中名稱為Form1,然后創(chuàng)建一個(gè)TrackBar組件和一個(gè)Label組件,并進(jìn)行初始化,Label組件的名稱為Label1,主要是顯示當(dāng)前窗體的透明度數(shù)值。接著定義各組件相關(guān)事件,程序中只有一個(gè)TrackBar1的“Scroll”事件,并在Form1中加入這些可視組件,這樣組件才能顯示出來。最后提供VB.NET的程序入口函數(shù)“Main”來運(yùn)行這個(gè)程序。下面在VB.NET中通過TrackBar1來定制窗體透明度的完整程序代碼(Form.vb):
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
‘繼承得到一個(gè)窗體
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗體設(shè)計(jì)器生成的代碼 "
Public Sub New ( )
MyBase.New ( )
‘該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
InitializeComponent ( )
‘在 InitializeComponent ( ) 調(diào)用之后添加任何初始化
End Sub
‘窗體重寫處置以清理組件列表。
Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
If disposing Then
If Not ( components Is Nothing ) Then
components.Dispose ( )
End If
End If
MyBase.Dispose ( disposing )
End Sub
‘創(chuàng)建Windows 窗體中的各種組件
Private components As System.ComponentModel.IContainer
‘注意:以下過程是 Windows 窗體設(shè)計(jì)器所必需的
‘可以使用 Windows 窗體設(shè)計(jì)器修改此過程。
‘不要使用代碼編輯器修改它。
Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
‘初始化各種組件,并定義相關(guān)的事件
Private Sub InitializeComponent ( )
Me.TrackBar1 = New System.Windows.Forms.TrackBar ( )
Me.Label1 = New System.Windows.Forms.Label ( )
Me.Label2 = New System.Windows.Forms.Label ( )
CType ( Me.TrackBar1 , System.ComponentModel.
ISupportInitialize ).BeginInit ( )
Me.SuspendLayout ( )
Me.TrackBar1.Location = New System.Drawing.Point ( 12 , 48 )
Me.TrackBar1.Maximum = 100
Me.TrackBar1.Name = "TrackBar1"
Me.TrackBar1.Size = New System.Drawing.Size ( 258 , 42 )
Me.TrackBar1.TabIndex = 1
Me.Label1.Location = New System.Drawing.Point ( 144 , 104 )
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size ( 66 , 24 )
Me.Label1.TabIndex = 2
Me.Label1.Text = "1"
Me.Label2.Location = New System.Drawing.Point ( 62 , 104 )
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size ( 78 , 23 )
Me.Label2.TabIndex = 3
Me.Label2.Text = "透明程度:"
Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 )
Me.ClientSize = New System.Drawing.Size ( 292 , 197 )
‘在窗體中加入組件
Me.Controls.AddRange ( New System.Windows.Forms.Control ( )
{ Me.Label2 , Me.Label1 , Me.TrackBar1 } )
Me.MaximizeBox = False
Me.Name = "Form1"
Me.Text = "VB.NET定制透明窗體"
CType ( Me.TrackBar1 , System.ComponentModel.ISupportInitialize )
.EndInit ( )
Me.ResumeLayout ( False )
End Sub
#End Region
‘事件處理
Private Sub TrackBar1_Scroll ( ByVal sender As Object , ByVal e As
System.EventArgs ) Handles TrackBar1.Scroll
Dim temp As Double
temp = ( 100 - TrackBar1.Value ) / 100
Label1.Text = temp.ToString ( )
Me.Opacity = temp
End Sub
End Class
‘啟動(dòng)程序
Module Module1
Sub Main ( )
Application.Run ( new Form1 ( ) )
End sub
End Module |
Form.vb源程序文件經(jīng)過了下列命令編譯后,就可以得到執(zhí)行文件Form.exe,編譯命令如下:
Vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll Form.vb |
下面是Form.exe運(yùn)行界面:
圖01:VB.NET定制窗體透明度01
圖02:VB.NET定制窗體透明度02
通過上面的介紹,可以了解到如何創(chuàng)建窗體、組件,定義事件等,下面就在上面的這些知識(shí)之上,利用窗體的"Opacity"屬性和定時(shí)器組件來實(shí)現(xiàn)一個(gè)窗體的特效窗體——實(shí)現(xiàn)窗體的淡入淡出效果。
VB.NET實(shí)現(xiàn)窗體淡入淡出特效
根據(jù)上面的介紹,發(fā)現(xiàn)通過調(diào)整窗體的"Opacity"屬性值,就可以實(shí)現(xiàn)窗體的不同的透明程度。那么是否可以通過一個(gè)時(shí)間觸發(fā)器來不斷調(diào)整"Opacity"屬性值,從而實(shí)現(xiàn)窗體淡入淡出的特效。答案是肯定的。
在.Net FrameWork SDK中提供了一個(gè)時(shí)間觸發(fā)組件——Timer組件,當(dāng)Timer組件的"Enabled"屬性設(shè)定為"true",那么Timer組件就會(huì)每隔其"Interval"屬性中的數(shù)值(請注意"Interval"屬性為毫秒級(jí)),觸發(fā)一下其"Tick"。所以在Timer組件的"Tick"事件中,加入修改窗體"Opacity"屬性值的代碼,就可以在Timer組件啟動(dòng)的時(shí)候,實(shí)現(xiàn)窗體的淡入淡出的特效了。
整個(gè)程序構(gòu)造思路是:
首先要繼承一個(gè)Form對象,程序中為名稱為Form1,然后創(chuàng)建2個(gè)Button組件,名稱為Button1和Button2,其作用是實(shí)現(xiàn)啟動(dòng)和停止定時(shí)器;一個(gè)Label組件,名稱Label1,主要是顯示當(dāng)前窗體的透明度數(shù)值;和一個(gè)Timer組件,名稱為"Timer1",在程序中設(shè)定其"Interval"的屬性值為"100",也就是說,當(dāng)此定時(shí)器啟動(dòng)的時(shí)候,每隔01.秒,觸發(fā)一下其"Tick"事件。接著初始化組件和定義各組件的相關(guān)的事件。并在Form1中加入這些可視組件,這樣組件才能顯示出來,由于Timer1并非可視化組件,所以在Form1中也沒有必要加入了。最后提供VB.NET的程序入口函數(shù)"Main"來運(yùn)行這個(gè)程序。下面就是根據(jù)上述思路,通過VB.NET實(shí)現(xiàn)窗體淡入淡出特效的的完整程序代碼(Form2.vb):
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
‘繼承得到一個(gè)窗體
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗體設(shè)計(jì)器生成的代碼 "
Public Sub New ( )
MyBase.New ( )
‘該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
InitializeComponent ( )
‘在 InitializeComponent ( ) 調(diào)用之后添加任何初始化
End Sub
‘窗體重寫處置以清理組件列表。
Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
If disposing Then
If Not ( components Is Nothing ) Then
components.Dispose ( )
End If
End If
MyBase.Dispose ( disposing )
End Sub
‘在Windows 窗體創(chuàng)建組件和定義相關(guān)變量
Private components As System.ComponentModel.IContainer
‘注意:以下過程是 Windows 窗體設(shè)計(jì)器所必需的
‘可以使用 Windows 窗體設(shè)計(jì)器修改此過程。
‘不要使用代碼編輯器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents Label1 As System.Windows.Forms.Label
Dim temp As Integer = 100
Dim flag As Boolean = True
Private Sub InitializeComponent ( )
‘初始化各組件
Me.components = New System.ComponentModel.Container ( )
Me.Button1 = New System.Windows.Forms.Button ( )
Me.Button2 = New System.Windows.Forms.Button ( )
Me.Timer1 = New System.Windows.Forms.Timer ( Me.components )
Me.Label1 = New System.Windows.Forms.Label ( )
Me.SuspendLayout ( )
Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.Button1.Location = New System.Drawing.Point ( 86 , 64 )
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size ( 96 , 36 )
Me.Button1.TabIndex = 0
Me.Button1.Text = "啟動(dòng)"
Me.Button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.Button2.Location = New System.Drawing.Point ( 86 , 116 )
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size ( 96 , 36 )
Me.Button2.TabIndex = 1
Me.Button2.Text = "停止"
Me.Label1.Location = New System.Drawing.Point ( 168 , 180 )
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 2
Me.Label1.Text = "Label1"
Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 )
Me.ClientSize = New System.Drawing.Size ( 266 , 223 )
‘在窗體中加入可視化組件
Me.Controls.AddRange ( New System.Windows.Forms.Control ( )
{Me.Label1 , Me.Button2 , Me.Button1} )
Me.MaximizeBox = False
Me.Name = "Form1"
Me.Text = "VB.NET實(shí)現(xiàn)淡入淡出的窗體效果"
Me.ResumeLayout ( False )
End Sub
#End Region
‘啟動(dòng)定時(shí)器
Private Sub Button1_Click ( ByVal sender As Object , ByVal e As
System.EventArgs ) Handles Button1.Click
Timer1.Enabled = True
End Sub
‘關(guān)閉定時(shí)器,停止窗體淡入淡出
Private Sub Button2_Click ( ByVal sender As Object , ByVal e As
System.EventArgs ) Handles Button2.Click
Timer1.Enabled = False
End Sub
‘調(diào)整窗體屬性,實(shí)現(xiàn)窗體淡入淡出特效
Private Sub Timer1_Tick ( ByVal sender As Object , ByVal e As
System.EventArgs ) Handles Timer1.Tick
If flag = True Then
temp = temp - 1
Me.Opacity = temp / 100
Label1.Text = ( temp / 100 ).ToString ( )
If temp = 0 Then
flag = False
End If
Else
temp = temp + 1
Me.Opacity = temp / 100
Label1.Text = ( temp / 100 ).ToString ( )
If temp = 100 Then
flag = True
End If
End If
End Sub
End Class
Module Module1
Sub Main ( )
Application.Run ( new Form1 ( ) )
End sub
End Module |
Form2.vb源程序文件經(jīng)過了下列命令編譯后,就可以得到執(zhí)行文件Form2.exe,編譯命令如下:
Vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll Form2.vb |
下面是Form2.exe啟動(dòng)定時(shí)器后在不同時(shí)間段的運(yùn)行界面:
圖03:VB.NET實(shí)現(xiàn)窗體淡入淡出特效的運(yùn)行界面01
圖04:VB.NET實(shí)現(xiàn)窗體淡入淡出特效的運(yùn)行界面04
總結(jié)
上面的內(nèi)容主要是通過二個(gè)具體的、有代表性的例子來介紹在VB.NET中編寫和窗體(Form)相關(guān)程序的一般思路。通過上面的介紹,可以掌握VB.NET中如何創(chuàng)建組件,定義組件的相關(guān)事件。VB.NET的WinForm雖然相對比較簡單,但卻是最基礎(chǔ),在下面的一系列文章中,將通過大量的實(shí)例來幫助您精通WinFrom編程。
|