Hallo Forum!
Diese ProgressBar ist eine Machbarkeitsstudie und basiert auf einer Idee aus diesem Forum in dem Thread Ajax "Progressbar's" v1.1b .
Dort werden Bilder für die Bar benutzt und die Behauptung aufgestellt, mit 100% GDI+ wäre es nicht möglich, die Balken ruckelfrei zu animieren.
Der Sache wollte ich nachgehen ..
So entstand dieses kleine Control, das ich Euch gern überlassen möchte.
Ihr könnt lizenzfrei alles damit anstellen (privat & kommerziell).
Betrachtet diesen Code als Grundgerüst. Das Control ist natürlich in jeder Hinsicht erweiterbar und kann sicher noch optimiert werden.
Ich würde mich sehr freuen, wenn wir zusammen das Control noch ein bisschen besser machen!
Der Code der ProgressBar ist dokumentiert, recht einfach und selbsterklärend, darum spare ich mir hier viele Worte.
Die Entwicklungsumgebung war: Visual Studio 2008, compiliert wurde für das FrameWork 2.0
Hier das Control:
Spoiler anzeigen
Und falls Ihr zum Testen das Form auch so aufbaut wie ich, dann hier der Code dazu:
Spoiler anzeigen
Ein ScreenShot des Controls:
Download Demo:
AjaxProgressBarFinal.exe
Viel Spaß damit und ich freue mich auf Eure Vorschläge zur Verbesserung,
Bruno
Diese ProgressBar ist eine Machbarkeitsstudie und basiert auf einer Idee aus diesem Forum in dem Thread Ajax "Progressbar's" v1.1b .
Dort werden Bilder für die Bar benutzt und die Behauptung aufgestellt, mit 100% GDI+ wäre es nicht möglich, die Balken ruckelfrei zu animieren.
Der Sache wollte ich nachgehen ..
So entstand dieses kleine Control, das ich Euch gern überlassen möchte.
Ihr könnt lizenzfrei alles damit anstellen (privat & kommerziell).
Betrachtet diesen Code als Grundgerüst. Das Control ist natürlich in jeder Hinsicht erweiterbar und kann sicher noch optimiert werden.
Ich würde mich sehr freuen, wenn wir zusammen das Control noch ein bisschen besser machen!
Der Code der ProgressBar ist dokumentiert, recht einfach und selbsterklärend, darum spare ich mir hier viele Worte.
Die Entwicklungsumgebung war: Visual Studio 2008, compiliert wurde für das FrameWork 2.0
Hier das Control:
VB.NET-Quellcode
- Option Explicit On
- Option Strict On
- Imports System
- Imports System.Drawing
- Imports System.Drawing.Drawing2D
- Imports System.Drawing.Text
- Imports System.ComponentModel
- Imports System.Timers
- Namespace WhiteControlsEx
- #Region " Ajax ProgressBar "
- Public Class WhiteAjaxProgressBar
- Inherits Control
- ' timer
- Dim tmr As New System.Timers.Timer(25)
- Dim offset As Integer
- ' minimum value for progress range
- Private _min As Integer = 0
- ' maximum value for progress range
- Private _max As Integer = 100
- ' current progress
- Private _val As Integer = 0
- ' color of progress meter
- Private _barColor As Color = Color.Gray
- <Category("WhiteAjaxProgressbar")> Public Property Minimum() As Integer
- Get
- Return _min
- End Get
- Set(ByVal value As Integer)
- ' prevent a negative value
- If value < 0 Then
- _min = 0
- End If
- ' make sure that the minimum value is never set higher than the maximum value
- If value > _max Then
- Return
- End If
- _min = value
- ' invalidate the control to get a repaint
- Me.Invalidate()
- End Set
- End Property
- <Category("WhiteAjaxProgressbar")> Public Property Maximum() As Integer
- Get
- Return _max
- End Get
- Set(ByVal value As Integer)
- ' make sure that the maximum value is never set lower than the minimum value
- If value < _min Then
- _min = value
- End If
- _max = value
- ' make sure that value is still in range
- If _val > _max Then
- _val = _max
- End If
- ' invalidate the control to get a repaint
- Me.Invalidate()
- End Set
- End Property
- <Category("WhiteAjaxProgressbar")> Public Property Value() As Integer
- Get
- Return _val
- End Get
- Set(ByVal value As Integer)
- ' make sure that the value does not stray outside the valid range
- If value < _min Then
- _val = _min
- ElseIf value > _max Then
- _val = _max
- Else
- _val = value
- End If
- ' invalidate the control to get a repaint
- Me.Invalidate()
- End Set
- End Property
- <Category("WhiteAjaxProgressbar")> Public Property BarColor() As Color
- Get
- Return Me._barColor
- End Get
- Set(ByVal value As Color)
- Me._barColor = value
- ' invalidate the control to get a repaint
- Me.Invalidate()
- End Set
- End Property
- Public Sub New()
- Me.DoubleBuffered = True
- Me.BackColor = Color.White
- Me.ForeColor = Color.LightGray
- offset = -30
- AddHandler tmr.Elapsed, AddressOf OnTimedEvent
- tmr.Start()
- End Sub
- Protected Overrides Sub OnResize(ByVal e As EventArgs)
- ' invalidate the control to get a repaint
- Me.Invalidate()
- End Sub
- ' timer elapsed
- Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
- offset += 1
- If offset = 0 Then offset = -30
- ' invalidate the control to get a repaint
- Me.Invalidate()
- End Sub
- Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
- ' system painting
- MyBase.OnPaint(e)
- ' graphics mode
- e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
- e.Graphics.PixelOffsetMode = PixelOffsetMode.Half
- ' paint border
- e.Graphics.DrawRectangle(New Pen(Color.LightGray, 2), 2, 2, Me.Width - 4, Me.Height - 4)
- ' calculate area for drawing the progress
- Dim percent As Double = (_val - _min) / (_max - _min)
- Dim rect As Rectangle = New Rectangle(5, 5, Me.Width - 10, Me.Height - 10)
- rect.Width = CInt(rect.Width * percent)
- ' paint normal bargraph
- ' >~~~~~~~~~~~~~~~~~~~~~~
- 'e.Graphics.FillRectangle(New SolidBrush(Me.BarColor), rect)
- ' <~~~~~~~~~~~~~~~~~~~~~~
- ' -= OR =-
- ' paint gradient bargraph
- ' >~~~~~~~~~~~~~~~~~~~~~~
- Try
- Dim colors As Color() = {Color.White, Me.BarColor, Me.BarColor, Color.White}
- Dim positions As Single() = {0.0F, 0.15F, 0.85F, 1.0F}
- Dim blend As New ColorBlend : blend.Colors = colors : blend.Positions = positions
- Dim lgb As New LinearGradientBrush(New Point(5, 5), New Point(5, Me.Height - 5), Color.White, Color.White)
- lgb.InterpolationColors = blend
- e.Graphics.FillRectangle(lgb, rect)
- Catch
- End Try
- ' <~~~~~~~~~~~~~~~~~~~~~~
- ' paint polygons
- For i = offset To rect.Width Step 30
- Dim point_feld() As Point = {New Point(i, Me.Height - 5), New Point(i + 20, 5), New Point(i + 40, 5), New Point(i + 20, Me.Height - 5)}
- ' set the clip property
- e.Graphics.Clip = New Region(rect)
- ' fill the region
- e.Graphics.FillPolygon(New SolidBrush(Color.FromArgb(40, 255, 255, 255)), point_feld)
- Next
- ' stringformat -> h:center, v:center
- Dim stringFormat As New StringFormat()
- stringFormat.Alignment = StringAlignment.Center
- stringFormat.LineAlignment = StringAlignment.Center
- ' set the clip property
- e.Graphics.Clip = New Region(Me.ClientRectangle)
- ' paint text and align center in rectangle
- e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.SystemDefault 'quality
- e.Graphics.DrawString(Me.Value & "%", Me.Font, New SolidBrush(Me.ForeColor), Me.ClientRectangle, stringFormat)
- End Sub
- End Class
- #End Region
- End Namespace
Und falls Ihr zum Testen das Form auch so aufbaut wie ich, dann hier der Code dazu:
VB.NET-Quellcode
- Option Explicit On
- Option Strict On
- Public Class frmMain
- Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
- WhiteAjaxProgressBar1.Value = TrackBar1.Value
- End Sub
- Private Sub TrackBar2_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar2.ValueChanged
- WhiteAjaxProgressBar2.Value = TrackBar2.Value
- End Sub
- Private Sub TrackBar3_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar3.ValueChanged
- WhiteAjaxProgressBar3.Value = TrackBar3.Value
- End Sub
- Private Sub TrackBar4_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar4.ValueChanged
- WhiteAjaxProgressBar4.Value = TrackBar4.Value
- End Sub
- End Class
Ein ScreenShot des Controls:
Download Demo:
AjaxProgressBarFinal.exe
Viel Spaß damit und ich freue mich auf Eure Vorschläge zur Verbesserung,
Bruno