一个PictureBox叫iScroll,一个TextBox叫txtScroll,一个CommandButton叫Command1,点击Command1后在iScroll中滚动显示txtScroll中的内容。模块中: Public Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Public Const EM_FMTLINES As Long = &HC8 Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Declare Function timeGetTime Lib "winmm.dll" () As Long Public Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Public Declare Function OffsetRect Lib "user32" (lpRect As RECT, ByVal X As Long, ByVal y As Long) As Long Public Declare Function ScrollDC Lib "user32" (ByVal hDC As Long, ByVal Dx As Long, ByVal dY As Long, lprcScroll As RECT, lprcClip As RECT, ByVal hrgnUpdate As Long, lprcUpdate As RECT) As Long Public Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long窗体中: Private TextLine As String '文字信息 Private Index As Long '字符索引 Private Scrolling As Boolean '滚动标志 Private t As Long '帧延时 Private RText As RECT Private RClip As RECT Private RUpdate As RECTPrivate Sub Scroll(sScroll As String) Scrolling = -1 Index = 1 Dim Char As String With iScroll SetRect RClip, 1, 2, .ScaleWidth, .ScaleHeight SetRect RText, .ScaleWidth, 2, .ScaleWidth + .TextWidth(Left$(sScroll, 1)), .ScaleHeight End With Char = Left$(sScroll, 1) With iScroll Do If (timeGetTime - t >= 30) Then t = timeGetTime If (RText.Right <= .ScaleWidth) Then Index = Index + 1 Char = Mid$(sScroll, Index, 1) SetRect RText, .ScaleWidth, 2, .ScaleWidth + .TextWidth(Mid$(sScroll, Index, 1)), .ScaleHeight End If DrawText .hDC, Char, 1, RText, &H0 OffsetRect RText, -1, 0 ScrollDC .hDC, -1, 0, RClip, RClip, 0, RUpdate iScroll.Line (.ScaleWidth - 1, 0)-(.ScaleWidth - 1, .ScaleHeight - 1), .BackColor End If If (Index > Len(sScroll)) Then Index = 0 DoEvents Loop Until Scrolling = 0 End With End SubPrivate Sub Command1_Click() Call Scroll(txtScroll.Text) End Sub
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Const EM_FMTLINES As Long = &HC8
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function timeGetTime Lib "winmm.dll" () As Long
Public Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Public Declare Function OffsetRect Lib "user32" (lpRect As RECT, ByVal X As Long, ByVal y As Long) As Long
Public Declare Function ScrollDC Lib "user32" (ByVal hDC As Long, ByVal Dx As Long, ByVal dY As Long, lprcScroll As RECT, lprcClip As RECT, ByVal hrgnUpdate As Long, lprcUpdate As RECT) As Long
Public Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long窗体中:
Private TextLine As String '文字信息
Private Index As Long '字符索引
Private Scrolling As Boolean '滚动标志
Private t As Long '帧延时
Private RText As RECT
Private RClip As RECT
Private RUpdate As RECTPrivate Sub Scroll(sScroll As String)
Scrolling = -1
Index = 1
Dim Char As String
With iScroll
SetRect RClip, 1, 2, .ScaleWidth, .ScaleHeight
SetRect RText, .ScaleWidth, 2, .ScaleWidth + .TextWidth(Left$(sScroll, 1)), .ScaleHeight
End With
Char = Left$(sScroll, 1)
With iScroll
Do
If (timeGetTime - t >= 30) Then
t = timeGetTime
If (RText.Right <= .ScaleWidth) Then
Index = Index + 1
Char = Mid$(sScroll, Index, 1)
SetRect RText, .ScaleWidth, 2, .ScaleWidth + .TextWidth(Mid$(sScroll, Index, 1)), .ScaleHeight
End If
DrawText .hDC, Char, 1, RText, &H0
OffsetRect RText, -1, 0
ScrollDC .hDC, -1, 0, RClip, RClip, 0, RUpdate
iScroll.Line (.ScaleWidth - 1, 0)-(.ScaleWidth - 1, .ScaleHeight - 1), .BackColor
End If
If (Index > Len(sScroll)) Then Index = 0
DoEvents
Loop Until Scrolling = 0
End With
End SubPrivate Sub Command1_Click()
Call Scroll(txtScroll.Text)
End Sub
根据你的方法,运行程序点击Command1,程序无反应,txtScroll无变化,是怎么回事呢?你调试通过了吗?我的环境是xp+vb6
用LABEL会出现文字闪动的情况,效果不好接受啊
DrawText .hDC, Char, 1, RText, &H0
改成:
DrawText .hDC, Char, -1, RText, &H0