|
TOP
|
| 文章内容 |
VB.NET实现窗体淡入淡出特效
根据上面的介绍,发现通过调整窗体的"Opacity"属性值,就可以实现窗体的不同的透明程度。那么是否可以通过一个时间触发器来不断调整"Opacity"属性值,从而实现窗体淡入淡出的特效。答案是肯定的。
在.Net FrameWork SDK中提供了一个时间触发组件——Timer组件,当Timer组件的"Enabled"属性设定为"true",那么Timer组件就会每隔其"Interval"属性中的数值(请注意"Interval"属性为毫秒级),触发一下其"Tick"。所以在Timer组件的"Tick"事件中,加入修改窗体"Opacity"属性值的代码,就可以在Timer组件启动的时候,实现窗体的淡入淡出的特效了。
整个程序构造思路是:
首先要继承一个Form对象,程序中为名称为Form1,然后创建2个Button组件,名称为Button1和Button2,其作用是实现启动和停止定时器;一个Label组件,名称Label1,主要是显示当前窗体的透明度数值;和一个Timer组件,名称为"Timer1",在程序中设定其"Interval"的属性值为"100",也就是说,当此定时器启动的时候,每隔01.秒,触发一下其"Tick"事件。接着初始化组件和定义各组件的相关的事件。并在Form1中加入这些可视组件,这样组件才能显示出来,由于Timer1并非可视化组件,所以在Form1中也没有必要加入了。最后提供VB.NET的程序入口函数"Main"来运行这个程序。下面就是根据上述思路,通过VB.NET实现窗体淡入淡出特效的的完整程序代码(Form2.vb):
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
'继承得到一个窗体
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New ( )
MyBase.New ( )
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent ( )
'在 InitializeComponent ( ) 调用之后添加任何初始化
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 窗体创建组件和定义相关变量
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
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 = "启动"
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实现淡入淡出的窗体效果"
Me.ResumeLayout ( False )
End Sub
#End Region
'启动定时器
Private Sub Button1_Click ( ByVal sender As Object , ByVal e As
System.EventArgs ) Handles Button1.Click
Timer1.Enabled = True
End Sub
'关闭定时器,停止窗体淡入淡出
Private Sub Button2_Click ( ByVal sender As Object , ByVal e As
System.EventArgs ) Handles Button2.Click
Timer1.Enabled = False
End Sub
'调整窗体属性,实现窗体淡入淡出特效
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源程序文件经过了下列命令编译后,就可以得到执行文件Form2.exe,编译命令如下:
Vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll Form2.vb |
下面是Form2.exe启动定时器后在不同时间段的运行界面:

图03:VB.NET实现窗体淡入淡出特效的运行界面01

图04:VB.NET实现窗体淡入淡出特效的运行界面04
总结
上面的内容主要是通过二个具体的、有代表性的例子来介绍在VB.NET中编写和窗体(Form)相关程序的一般思路。通过上面的介绍,可以掌握VB.NET中如何创建组件,定义组件的相关事件。VB.NET的WinForm虽然相对比较简单,但却是最基础,在下面的一系列文章中,将通过大量的实例来帮助您精通WinFrom编程。
【 |
|
| 相关信息 |
![]() |
VB.NET编程之透明窗体篇 (2)
发布者:mmcbbs
浏览量:184
发布日期:2005-04-10 10:27:25
所属专题: |
|
|
|
|