Private _x As Integer
Private _color As Color
Private _width As Integer
Private _border As Boolean
Private WithEvents _tmr As System.Windows.Forms.Timer
Public Property RectColor() As Color
Get
Return _color
End Get
Set(ByVal Value As Color)
_color = Value
End Set
End Property
Public Property RectWidth() As Integer
Get
Return _width
End Get
Set(ByVal Value As Integer)
_width = Value
End Set
End Property
Public Property HasBorder() As Boolean
Get
Return _border
End Get
Set(ByVal Value As Boolean)
_border = Value
DrawBorder()
End Set
End Property
Public Sub StartPB()
_tmr = New System.Windows.Forms.Timer
_tmr.Interval = 1
AddHandler _tmr.Tick, AddressOf DrawBar
_tmr.Start()
End Sub
Public Sub StopPB()
_tmr.Stop()
End Sub
Private Sub DrawBar(ByVal sender As Object, ByVal e As EventArgs)
_x += 1
'account for border if necessary
If _x = 0 Then
If _border Then _x = 1
End If
'set to negative size of rects so it draws smoothly
If _x > Me.Width Then _x = -(_width * 3 + 2)
'create graphics object for drawing
Dim gr As Graphics = Me.CreateGraphics
gr.Clear(Me.BackColor)
If _border Then gr.DrawRectangle(Pens.Black, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
gr.FillRectangle(New SolidBrush(_color), New Rectangle(_x, IIf(_border, 1, 0), _width, IIf(_border, Me.Height - 2, Me.Height)))
gr.FillRectangle(New SolidBrush(_color), New Rectangle(_x + _width + 1, IIf(_border, 1, 0), _width, IIf(_border, Me.Height - 2, Me.Height)))
gr.FillRectangle(New SolidBrush(_color), New Rectangle(_x + (_width * 2) + 2, IIf(_border, 1, 0), _width, IIf(_border, Me.Height - 2, Me.Height)))
End Sub
Private Sub DrawBorder()
Dim gr As Graphics = Me.CreateGraphics
gr.Clear(Me.BackColor)
If _border Then gr.DrawRectangle(Pens.Black, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
End Sub
Private Sub ProgressBarEx_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
DrawBorder()
End Sub
Private Sub ProgressBarEx_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
DrawBorder()
End Sub
PBM_SETMARQUEE Message
--------------------------------------------------------------------------------
Sets the progress bar to marquee mode. This causes the progress bar to move like a marquee.
Syntax
To send this message, call the SendMessage function as follows.
lResult = SendMessage( // returns LRESULT in lResult (HWND) hWndControl, // handle to destination control (UINT) PBM_SETMARQUEE, // message ID (WPARAM) wParam, // = (WPARAM) (BOOL) wParam; (LPARAM) lParam // = (LPARAM) (UINT) lParam; );
Parameters
wParam
Indicates whether to turn the marquee mode on or off.
lParam
Time in milliseconds between marquee animation updates.
Return Value
Returns whether marquee mode is set.
Remarks
Use this message when you do not know the amount of progress toward completion but wish to indicate that progress is being made.
Send the PBM_SETMARQUEE message to start or stop the animation.
If you app is going to be a fixed size or even if the progress bar is going to exist in a fixed area such as a panel pane this would definately be a quick and easy work around to get the effects you are looking for. Windows itself took this approach for a lot of different tasks. AAhh...the old days of Windows with the file paper flying out of the cabinet, across the dialog box, crumpling up, and going into the trash..Thank goodness those are gone (mostly).marble_eater said:It is worth noting that you could easily do this with an animated gif in a picturebox
Not a concern for most users but I have seen a few healthy debates on the subject in this forum that might suggest otherwise...marble_eater said:This won't be consistend with user's xp styles/color settings, though
Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class Win32Interop
Public Const WM_USER As Int32 = 1024
Public Const PBM_SETMARQUEE As Int32 = (WM_USER + 10)
Public Const GWL_STYLE As Int32 = -16
Public Const PBS_MARQUEE As Int32 = 8
<DllImport("user32.dll", EntryPoint:="GetWindowLong")> _
Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Int32) As Int32
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowLong")> _
Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Int32, ByVal dwNewLong As Int32) As Int32
End Function
<DllImport("User32", SetLastError:=True)> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
End Function
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Private _pb As Windows.Forms.ProgressBar
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Application.EnableVisualStyles()
Application.DoEvents()
_pb = New Windows.Forms.ProgressBar
_pb.Dock = Windows.Forms.DockStyle.Fill
Width = 500
Height = 50
Controls.Add(_pb)
Dim i As Int32 = Win32Interop.GetWindowLong(_pb.Handle, Win32Interop.GWL_STYLE)
Win32Interop.SetWindowLong(_pb.Handle, Win32Interop.GWL_STYLE, i Or Win32Interop.PBS_MARQUEE)
Win32Interop.SendMessage(_pb.Handle, Win32Interop.PBM_SETMARQUEE, 1, 100)
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = "Form1"
End Sub
#End Region
End Class