Makes a progress bar marquee. Only works with XP styles turned on


Code:
Private Const WM_USER = &H400
Private Const PBM_SETMARQUEE = WM_USER + 10
Private Const GWL_STYLE = (-16)
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Dim PrevStyle As Long
Private Function SetUpMarquee(ObjProgressBar As ProgressBar, Optional TurnOn As Boolean, Optional Speed As Integer = 50)
Select Case TurnOn
    Case True
        'Set the style to Marquee
        SetWindowLong ObjProgressBar.hwnd, GWL_STYLE, PrevStyle Or PBM_SETMARQUEE
        'Make it automatically scroll
        SetUpMarquee = SendMessage(ObjProgressBar.hwnd, PBM_SETMARQUEE, CLng(TurnOn), CLng(Speed))
    Case False
        'Set the Style Back
        SetWindowLong ObjProgressBar.hwnd, GWL_STYLE, PrevStyle
End Select
End Function

Private Sub Form_Load()
    'Get the current Style
    PrevStyle = GetWindowLong(ProgressBar1.hwnd, GWL_STYLE)
    
    'Turn it on or off and speed
    SetUpMarquee ProgressBar1, True, 50
    ProgressBar1.Value = 50         'Just to show that when its on, setting the value will not work
End Sub


References:
  • The need to set the style to Marquee from Link
  • Making it Automatic, research on MSDN and experimenting