雖然VB.NET和VB在語(yǔ)法上有很多相同之處,但從根本上說(shuō),VB.NET不僅僅是VB的另外一個(gè)升級版本,而是一個(gè)全新的語(yǔ)言。VB.NET全面支持面向對象,在VB.NET中哪怕是一個(gè)字符串,你也都可以把它看成是一個(gè)對象,一個(gè)實(shí)例,也有自己的屬性和方法。同樣VB.NET中程序窗體(Form)也是一個(gè)實(shí)例,它是由命名空間“System.Windows.Forms”中的“Form”類(lèi)通過(guò)構造函數而成的一個(gè)實(shí)例。下面試著(zhù)用VB.NET來(lái)編寫(xiě)與窗體相關(guān)的程序。
利用VB.NET來(lái)定制窗體的透明度
在下面介紹的程序中,將通過(guò)調整TrackBar組件的數值來(lái)調整窗體的透明程度。定制透明的窗體,如果要用其他語(yǔ)言來(lái)實(shí)現,一定是件很復雜的工作,但對于VB.NET來(lái)說(shuō),則是一件非常簡(jiǎn)單的事情,這是因為在.Net FrameWork SDK的“Form”類(lèi)中,提供了一個(gè)可以設定窗體透明度的屬性“Opacity”,當“Opacity”屬性值為“1”,說(shuō)明窗體不透明,當“Opacity”為“0”,則窗體完全透明。
構造整個(gè)程序的主要思路就是:首先要繼承一個(gè)Form對象,程序中名稱(chēng)為Form1,然后創(chuàng )建一個(gè)TrackBar組件和一個(gè)Label組件,并進(jìn)行初始化,Label組件的名稱(chēng)為L(cháng)abel1,主要是顯示當前窗體的透明度數值。接著(zhù)定義各組件相關(guān)事件,程序中只有一個(gè)TrackBar1的“Scroll”事件,并在Form1中加入這些可視組件,這樣組件才能顯示出來(lái)。最后提供VB.NET的程序入口函數“Main”來(lái)運行這個(gè)程序。下面在VB.NET中通過(guò)TrackBar1來(lái)定制窗體透明度的完整程序代碼(Form.vb):
Imports System.DrawingImports System.Windows.FormsImports System.ComponentModel‘繼承得到一個(gè)窗體Public Class Form1 Inherits System.Windows.Forms.Form#Region " Windows 窗體設計器生成的代碼 " Public Sub New ( ) MyBase.New ( ) ‘該調用是 Windows 窗體設計器所必需的。 InitializeComponent ( ) ‘在 InitializeComponent ( ) 調用之后添加任何初始化 End Sub ‘窗體重寫(xiě)處置以清理組件列表。 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 ‘注意:以下過(guò)程是 Windows 窗體設計器所必需的 ‘可以使用 Windows 窗體設計器修改此過(guò)程。 ‘不要使用代碼編輯器修改它。 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 SubEnd Class‘啟動(dòng)程序Module Module1Sub Main ( ) Application.Run ( new Form1 ( ) )End subEnd Module
Form.vb源程序文件經(jīng)過(guò)了下列命令編譯后,就可以得到執行文件Form.exe,編譯命令如下:
Vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll Form.vb
下面是Form.exe運行界面:
圖01:VB.NET定制窗體透明度01
圖02:VB.NET定制窗體透明度02
通過(guò)上面的介紹,可以了解到如何創(chuàng )建窗體、組件,定義事件等,下面就在上面的這些知識之上,利用窗體的"Opacity"屬性和定時(shí)器組件來(lái)實(shí)現一個(gè)窗體的特效窗體——實(shí)現窗體的淡入淡出效果。
VB.NET實(shí)現窗體淡入淡出特效
根據上面的介紹,發(fā)現通過(guò)調整窗體的"Opacity"屬性值,就可以實(shí)現窗體的不同的透明程度。那么是否可以通過(guò)一個(gè)時(shí)間觸發(fā)器來(lái)不斷調整"Opacity"屬性值,從而實(shí)現窗體淡入淡出的特效。答案是肯定的。
在.Net FrameWork SDK中提供了一個(gè)時(shí)間觸發(fā)組件——Timer組件,當Timer組件的"Enabled"屬性設定為"true",那么Timer組件就會(huì )每隔其"Interval"屬性中的數值(請注意"Interval"屬性為毫秒級),觸發(fā)一下其"Tick"。所以在Timer組件的"Tick"事件中,加入修改窗體"Opacity"屬性值的代碼,就可以在Timer組件啟動(dòng)的時(shí)候,實(shí)現窗體的淡入淡出的特效了。
整個(gè)程序構造思路是:
首先要繼承一個(gè)Form對象,程序中為名稱(chēng)為Form1,然后創(chuàng )建2個(gè)Button組件,名稱(chēng)為Button1和Button2,其作用是實(shí)現啟動(dòng)和停止定時(shí)器;一個(gè)Label組件,名稱(chēng)Label1,主要是顯示當前窗體的透明度數值;和一個(gè)Timer組件,名稱(chēng)為"Timer1",在程序中設定其"Interval"的屬性值為"100",也就是說(shuō),當此定時(shí)器啟動(dòng)的時(shí)候,每隔01.秒,觸發(fā)一下其"Tick"事件。接著(zhù)初始化組件和定義各組件的相關(guān)的事件。并在Form1中加入這些可視組件,這樣組件才能顯示出來(lái),由于Timer1并非可視化組件,所以在Form1中也沒(méi)有必要加入了。最后提供VB.NET的程序入口函數"Main"來(lái)運行這個(gè)程序。下面就是根據上述思路,通過(guò)VB.NET實(shí)現窗體淡入淡出特效的的完整程序代碼(Form2.vb):
Imports System.Drawing Imports System.Windows.Forms Imports System.ComponentModel ‘繼承得到一個(gè)窗體 Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows 窗體設計器生成的代碼 " Public Sub New ( ) MyBase.New ( ) ‘該調用是 Windows 窗體設計器所必需的。 InitializeComponent ( ) ‘在 InitializeComponent ( ) 調用之后添加任何初始化 End Sub ‘窗體重寫(xiě)處置以清理組件列表。 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 ‘注意:以下過(guò)程是 Windows 窗體設計器所必需的 ‘可以使用 Windows 窗體設計器修改此過(guò)程。 ‘不要使用代碼編輯器修改它。 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í)現淡入淡出的窗體效果" 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 ‘調整窗體屬性,實(shí)現窗體淡入淡出特效 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)過(guò)了下列命令編譯后,就可以得到執行文件Form2.exe,編譯命令如下:
Vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll Form2.vb
下面是Form2.exe啟動(dòng)定時(shí)器后在不同時(shí)間段的運行界面:
圖03:VB.NET實(shí)現窗體淡入淡出特效的運行界面01
圖04:VB.NET實(shí)現窗體淡入淡出特效的運行界面04
總結
上面的內容主要是通過(guò)二個(gè)具體的、有代表性的例子來(lái)介紹在VB.NET中編寫(xiě)和窗體(Form)相關(guān)程序的一般思路。通過(guò)上面的介紹,可以掌握VB.NET中如何創(chuàng )建組件,定義組件的相關(guān)事件。VB.NET的WinForm雖然相對比較簡(jiǎn)單,但卻是最基礎,在下面的一系列文章中,將通過(guò)大量的實(shí)例來(lái)幫助您精通WinFrom編程。