用VB编程实现文字按一定的方向来回滚动,类似于公交车的站台提示,一般的方法是使用标签,通过定时器改变标签的Left和Top属性值来实现文字的滚动,但这样存在的问题是:滚动的文字会出现闪动。请教各位:有什么更好的办法可以解决?

解决方案 »

  1.   

    一个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
      

  2.   

    麻烦,用个lable就算了。就是一个lb.left=,放在时间控件了,调用一下就OK了,
      

  3.   

    还是用LABEL吧,感觉效果还行吧?
      

  4.   

    开心海:
        根据你的方法,运行程序点击Command1,程序无反应,txtScroll无变化,是怎么回事呢?你调试通过了吗?我的环境是xp+vb6
      

  5.   

    简单的用timer来控制label的移动会闪的,否则楼主就不会有此一问了。
      

  6.   

    程序爱好者、孤夜寒心泪:
        用LABEL会出现文字闪动的情况,效果不好接受啊
      

  7.   

    忘了说明了,需要把iscroll的scalemode设置成3,另外在form_unload中别忘了加一个scrolling=0
      

  8.   

    原来没考虑中文字符的问题,请把
    DrawText .hDC, Char, 1, RText, &H0
    改成:
    DrawText .hDC, Char, -1, RText, &H0