****************************************
* 電腦環境:Windows 7 64bit *
* Microsoft Visual Studio版本:2010 Professional *
****************************************
一、把layout拉好要4個Label
要1個Timer
二、Timer的屬性
Timer.Enabled = True '啟動計時器
Timer.Interval = 10 '每隔0.01秒自動執行Timer_Tick事件
三、按F5後看到的畫面
「來抓來抓」跑到左邊碰壁後,會往右跑
****************************************
* 程式語法:Visual Basic *
****************************************
Public Class Form
Dim x, y, k As Integer
Dim flag As Boolean = True '跑馬燈方向朝左
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
x = 150 : y = 150
lblMsg.Location = New Point(x, y)
lblMsg.BackColor = Color.Aqua
lblMsg.AutoSize = True
lblMsg.Font = New System.Drawing.Font("標楷體", 20, FontStyle.Bold)
lblMsg.Text = "來抓來抓"
Timer1.Enabled = True '啟動計時器
Timer1.Interval = 10 '每隔0.01秒自動執行Timer1_Tick
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
lblLocation.Text = "'跑馬燈'位置:" & lblMsg.Left
If flag = True Then '當flag = True, 左移
x -= 1 'x座標位置-1
lblMsg.Location = New Point(x, y) '重新指定lblMsg的位置
lblDirection.Text = "目前方向: 往左移"
If (Integer.Parse(lblMsg.Left) <= 0) Then '當左到底了就設flag = False
flag = False
End If
Else '當flag = False, 右移
x += 1 'x座標位置+1
lblMsg.Location = New Point(x, y) '重新指定lblMsg的位置
lblDirection.Text = "目前方向: 往右移"
'Integer.Parse(lblMsg.Left) + Integer.Parse(lblMsg.Width)←相當於lblMsg右邊x的位置
'Me.Width←是指Form的寬
If (Integer.Parse(lblMsg.Left) + Integer.Parse(lblMsg.Width) >= Integer.Parse(Me.Width)) Then
flag = True '當右到底了就設flag = False
End If
End If
End Sub
End Class
3




沒有留言:
張貼留言