文字寫(xiě)在label的caption屬性中,調(diào)用timer()函數(shù)
創(chuàng)新互聯(lián)建站是少有的做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、營(yíng)銷型企業(yè)網(wǎng)站、小程序開(kāi)發(fā)、手機(jī)APP,開(kāi)發(fā)、制作、設(shè)計(jì)、外鏈、推廣優(yōu)化一站式服務(wù)網(wǎng)絡(luò)公司,從2013年創(chuàng)立,堅(jiān)持透明化,價(jià)格低,無(wú)套路經(jīng)營(yíng)理念。讓網(wǎng)頁(yè)驚喜每一位訪客多年來(lái)深受用戶好評(píng)
附上我寫(xiě)的左右往復(fù)運(yùn)動(dòng)代碼,實(shí)現(xiàn)image1在picture1中左右往復(fù)運(yùn)動(dòng)
Private
Sub
Timer1_Timer()
If
Image1.Left
=
Picture1.ScaleWidth
-
Image1.Width
Then
k
=
1
If
k
=
1
Then
Image1.Left
=
Image1.Left
-
50
Else
Image1.Left
=
Image1.Left
+
50
End
If
If
Image1.Left
=
Then
k
=
End
Sub
RichTextBox1.Focus() RichTextBox1.SelectionLength = 0 RichTextBox1.SelectionStart = RichTextBox1.Text.Length RichTextBox1.ScrollToCaret()這樣將保持RichTextBox中的文字焦點(diǎn)始終出現(xiàn)在最新的一行
示例:
PrivateSubForm_Load()
'//預(yù)先設(shè)定要顯示的內(nèi)容
content="滾動(dòng)字幕示例控制文本的循環(huán)滾動(dòng)"
'//獲取內(nèi)容長(zhǎng)度
length=Len(content)
'//label1控件,手動(dòng)調(diào)整
'//接著需要選擇字體大小
SetMe.Font=Label1.Font'//方便借用Form.TextHeight方法
Dimfont_heightAsLong,font_widthAsLong,sizeAsLong
size=Label1.Font.size
font_height=Me.TextHeight("循環(huán)滾動(dòng)")
font_width=Me.TextWidth("循環(huán)滾動(dòng)")
Whilefont_height=Label1.HeightAndfont_width=Label1.Width
size=size+1
Label1.Font.size=size
font_height=Me.TextHeight("循環(huán)滾動(dòng)")
font_width=Me.TextWidth("循環(huán)滾動(dòng)")
Wend
Label1.Font.size=size-1'//選擇最合適的字體大小
pos=1'//從第一個(gè)字符開(kāi)始讀取
EndSub
PrivateSubCommand1_Click()
Timer1.Interval=1000'1000毫秒執(zhí)行一次
EndSub
PrivateSubTimer1_Timer()
DimsizeAsLong,tempAsString
'//每次顯示5個(gè)長(zhǎng)度單位的內(nèi)容
size=length-pos'//得到截取的長(zhǎng)度大小
Ifsize4Then'//當(dāng)不足5個(gè)長(zhǎng)度單位時(shí)
temp=Mid(content,pos,size+1)
temp=tempMid(content,1,4-size)
pos=5-size
Else
temp=Mid(content,pos,5)
pos=pos+5
EndIf
Label1.Caption=temp'//把截取的文本內(nèi)容顯示出來(lái)
EndSub
擴(kuò)展資料
VB設(shè)計(jì)自動(dòng)滾動(dòng)字幕窗體
DimDireAsString
'窗體Load事件
PrivateSubForm_Load()
Dire="向左"
EndSub
'定時(shí)器事件
PrivateSubTimer1_Timer()
DimNewColorAsLong
Randomize
NewColor=RGB(Rnd()*256,Rnd()*256,Rnd()*256)
Label1.ForeColor=NewColor
IfDire="向左"Then
Label1.Left=Label1.Left-10
IfLabel1.Left0ThenDire="向右"
ElseIfDire="向右"Then
Label1.Left=Label1.Left+10
IfLabel1.Left+Label1.WidthMe.ScaleWidthThenDire="向左"
EndIf
EndSub
一個(gè)上下左右移動(dòng)的例子
花了一些時(shí)間來(lái)做了一個(gè)上下左右移動(dòng)的例子,做得好的話幫我加點(diǎn)分.
包括四個(gè)標(biāo)簽控件,和一個(gè)時(shí)間控件
'把以下的代碼另存為一個(gè)FORM1.FRM的文件
(先新建一個(gè)文本文檔,保存后改名字即可)
'代碼開(kāi)始
VERSION 5.00
Begin VB.Form Form1
BackColor = H00000000
BorderStyle = 1 'Fixed Single
Caption = "字體上下左右來(lái)回移動(dòng)的動(dòng)畫(huà)"
ClientHeight = 5490
ClientLeft = 45
ClientTop = 435
ClientWidth = 7605
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 5490
ScaleWidth = 7605
StartUpPosition = 3 '窗口缺省
Begin VB.Timer Timer1
Left = 7020
Top = 4980
End
Begin VB.Label Label4
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "字體左右來(lái)回移動(dòng)"
ForeColor = H00FFFF00
Height = 180
Left = 6180
TabIndex = 3
Top = 3720
Width = 1440
End
Begin VB.Label Label3
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "字體左右來(lái)回移動(dòng)"
ForeColor = H008080FF
Height = 180
Left = 0
TabIndex = 2
Top = 2100
Width = 1440
End
Begin VB.Label Label2
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "字體上下來(lái)回移動(dòng)"
ForeColor = H00FF80FF
Height = 180
Left = 4560
TabIndex = 1
Top = 3180
Width = 1440
End
Begin VB.Label Label1
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "字體上下來(lái)回移動(dòng)"
ForeColor = H0000FFFF
Height = 180
Left = 1860
TabIndex = 0
Top = 2760
Width = 1440
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim i As Integer '標(biāo)簽1上下移動(dòng)的位置
Dim j As Integer '標(biāo)簽2上下移動(dòng)的位置
Dim c As Integer '標(biāo)簽3左右移動(dòng)的位置
Dim z As Integer '標(biāo)簽4左右移動(dòng)的位置
Dim lab1move As Boolean '標(biāo)簽1移動(dòng)的狀態(tài)
Dim lab2move As Boolean '標(biāo)簽2移動(dòng)的狀態(tài)
Dim lab3move As Boolean '標(biāo)簽3移動(dòng)的狀態(tài)
Dim lab4move As Boolean '標(biāo)簽4移動(dòng)的狀態(tài)
Private Sub Form_Load()
Timer1.Interval = 10 '每過(guò)10豪秒執(zhí)行一次
i = 0 '初始化標(biāo)簽1的位置
j = Me.Height '初始化標(biāo)簽2的位置
z = Me.Width '初始化標(biāo)簽4的位置
c = 0 '初始化標(biāo)簽3的位置
End Sub
Private Sub Timer1_Timer()
If lab1move = False Then '狀態(tài)為向下
'標(biāo)簽1從頂部移動(dòng)到底部
If i Me.Height Then '小于窗體的高
i = i + 100 '每次移動(dòng)100
Label1.Top = i '移動(dòng)標(biāo)簽1
End If
If i Me.Height Then '大于窗體的高
lab1move = True '狀態(tài)為向上
i = Me.Height '=窗體的高
End If
Else '狀態(tài)為向上
'標(biāo)簽1從底部移動(dòng)到頂部
i = i - 100 '每次移動(dòng)100
Label1.Top = i '移動(dòng)標(biāo)簽
If i = 0 Then lab1move = False: i = 0 '標(biāo)簽1從底部移到頂部
End If
If lab2move = False Then '狀態(tài)為向上
j = j - 100 '每次移動(dòng)100
Label2.Top = j '移動(dòng)標(biāo)簽2
If j 0 Then lab2move = True: j = 0 '標(biāo)簽1從底部移到頂部
Else '狀態(tài)為向下
If j Me.Height Then j = j + 100: Label2.Top = j '移動(dòng)標(biāo)簽2
If j Me.Height Then lab2move = False: j = Me.Height '狀態(tài)為向上
End If
If lab3move = False Then '狀態(tài)為向右
If c Me.Width Then c = c + 100: Label3.Left = c '每次移動(dòng)100移動(dòng)標(biāo)簽
If c Me.Width Then lab3move = True: c = Me.Width
Else '狀態(tài)為向左
c = c - 100
Label3.Left = c '每次移動(dòng)100移動(dòng)標(biāo)簽
If c 0 Then lab3move = False: c = 0 '狀態(tài)為向右
End If
If lab4move = False Then '狀態(tài)為向左
z = z - 100
Label4.Left = z '每次移動(dòng)100移動(dòng)標(biāo)簽
If z 0 Then lab4move = True: z = 0
Else '狀態(tài)為向右
If z Me.Width Then z = z + 100: Label4.Left = z '每次移動(dòng)100移動(dòng)標(biāo)簽
If z Me.Width Then lab4move = False: z = Me.Width '狀態(tài)為向左
End If
End Sub
'代碼結(jié)束.