打開Visual Studio 2008在文件 (File) 菜單上,單擊新建項(xiàng)目 (New Project)。在新建項(xiàng)目 (New Project) 對(duì)話框的模板 (Templates) 窗格中,單擊 Windows 應(yīng)用程序(Windows Application)。 單擊確定 (OK)
窗體應(yīng)用技巧一,創(chuàng)建浮動(dòng)窗體。
創(chuàng)建新工程后,選擇Form1窗體,添加 Timer1和Timer2控件。為窗體選擇一個(gè)好看的背景,當(dāng)然你也可以使用系統(tǒng)默認(rèn)的背景。
進(jìn)入代 碼編輯器,輸入代碼:
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim pos As Point = New Point(100, 50) '設(shè)置窗體初始位置
Me.DesktopLocation = pos
Timer1.Interval = 10 '設(shè)置Timer的值
Timer1.Enabled = True
Timer2.Interval = 10
Timer2.Enabled = False
End Sub
進(jìn)入Timer1_Tick事件
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Timer1.Tick
Dim pos As Point = New Point(Me.DesktopLocation.X + 2, Me.DesktopLocation.Y + 1)
'窗體 左上方橫坐標(biāo)的timer1加
If pos.X < 600 Or pos.Y < 400 Then
Me.DesktopLocation = pos
Else
Timer1.Enabled = False
Timer2.Enabled = True
End If
End Sub
進(jìn)入Timer2_Tick事件
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Timer2.Tick
Dim pos As Point = New Point(Me.DesktopLocation.X - 2, Me.DesktopLocation.Y - 1)
'窗體的左上方橫坐標(biāo)隨著timer2減一
If pos.X > 100 Or pos.Y > 50 Then
Me.DesktopLocation = pos
Else
Timer1.Enabled = True
Timer2.Enabled = False
End If
End Sub
創(chuàng)建完成后我們來運(yùn)行程序測(cè)試一下,測(cè)試成功,程序在屏幕中不斷地來回走動(dòng)了。
更多信息請(qǐng)查看IT技術(shù)專欄