ProgressBar mit Animation durch GDI+ und Polygonen im AjaxStyle

    • VB.NET

    Es gibt 7 Antworten in diesem Thema. Der letzte Beitrag () ist von diylab.

      ProgressBar mit Animation durch GDI+ und Polygonen im AjaxStyle

      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 8-) ..

      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

      VB.NET-Quellcode

      1. Option Explicit On
      2. Option Strict On
      3. Imports System
      4. Imports System.Drawing
      5. Imports System.Drawing.Drawing2D
      6. Imports System.Drawing.Text
      7. Imports System.ComponentModel
      8. Imports System.Timers
      9. Namespace WhiteControlsEx
      10. #Region " Ajax ProgressBar "
      11. Public Class WhiteAjaxProgressBar
      12. Inherits Control
      13. ' timer
      14. Dim tmr As New System.Timers.Timer(25)
      15. Dim offset As Integer
      16. ' minimum value for progress range
      17. Private _min As Integer = 0
      18. ' maximum value for progress range
      19. Private _max As Integer = 100
      20. ' current progress
      21. Private _val As Integer = 0
      22. ' color of progress meter
      23. Private _barColor As Color = Color.Gray
      24. <Category("WhiteAjaxProgressbar")> Public Property Minimum() As Integer
      25. Get
      26. Return _min
      27. End Get
      28. Set(ByVal value As Integer)
      29. ' prevent a negative value
      30. If value < 0 Then
      31. _min = 0
      32. End If
      33. ' make sure that the minimum value is never set higher than the maximum value
      34. If value > _max Then
      35. Return
      36. End If
      37. _min = value
      38. ' invalidate the control to get a repaint
      39. Me.Invalidate()
      40. End Set
      41. End Property
      42. <Category("WhiteAjaxProgressbar")> Public Property Maximum() As Integer
      43. Get
      44. Return _max
      45. End Get
      46. Set(ByVal value As Integer)
      47. ' make sure that the maximum value is never set lower than the minimum value
      48. If value < _min Then
      49. _min = value
      50. End If
      51. _max = value
      52. ' make sure that value is still in range
      53. If _val > _max Then
      54. _val = _max
      55. End If
      56. ' invalidate the control to get a repaint
      57. Me.Invalidate()
      58. End Set
      59. End Property
      60. <Category("WhiteAjaxProgressbar")> Public Property Value() As Integer
      61. Get
      62. Return _val
      63. End Get
      64. Set(ByVal value As Integer)
      65. ' make sure that the value does not stray outside the valid range
      66. If value < _min Then
      67. _val = _min
      68. ElseIf value > _max Then
      69. _val = _max
      70. Else
      71. _val = value
      72. End If
      73. ' invalidate the control to get a repaint
      74. Me.Invalidate()
      75. End Set
      76. End Property
      77. <Category("WhiteAjaxProgressbar")> Public Property BarColor() As Color
      78. Get
      79. Return Me._barColor
      80. End Get
      81. Set(ByVal value As Color)
      82. Me._barColor = value
      83. ' invalidate the control to get a repaint
      84. Me.Invalidate()
      85. End Set
      86. End Property
      87. Public Sub New()
      88. Me.DoubleBuffered = True
      89. Me.BackColor = Color.White
      90. Me.ForeColor = Color.LightGray
      91. offset = -30
      92. AddHandler tmr.Elapsed, AddressOf OnTimedEvent
      93. tmr.Start()
      94. End Sub
      95. Protected Overrides Sub OnResize(ByVal e As EventArgs)
      96. ' invalidate the control to get a repaint
      97. Me.Invalidate()
      98. End Sub
      99. ' timer elapsed
      100. Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
      101. offset += 1
      102. If offset = 0 Then offset = -30
      103. ' invalidate the control to get a repaint
      104. Me.Invalidate()
      105. End Sub
      106. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
      107. ' system painting
      108. MyBase.OnPaint(e)
      109. ' graphics mode
      110. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
      111. e.Graphics.PixelOffsetMode = PixelOffsetMode.Half
      112. ' paint border
      113. e.Graphics.DrawRectangle(New Pen(Color.LightGray, 2), 2, 2, Me.Width - 4, Me.Height - 4)
      114. ' calculate area for drawing the progress
      115. Dim percent As Double = (_val - _min) / (_max - _min)
      116. Dim rect As Rectangle = New Rectangle(5, 5, Me.Width - 10, Me.Height - 10)
      117. rect.Width = CInt(rect.Width * percent)
      118. ' paint normal bargraph
      119. ' >~~~~~~~~~~~~~~~~~~~~~~
      120. 'e.Graphics.FillRectangle(New SolidBrush(Me.BarColor), rect)
      121. ' <~~~~~~~~~~~~~~~~~~~~~~
      122. ' -= OR =-
      123. ' paint gradient bargraph
      124. ' >~~~~~~~~~~~~~~~~~~~~~~
      125. Try
      126. Dim colors As Color() = {Color.White, Me.BarColor, Me.BarColor, Color.White}
      127. Dim positions As Single() = {0.0F, 0.15F, 0.85F, 1.0F}
      128. Dim blend As New ColorBlend : blend.Colors = colors : blend.Positions = positions
      129. Dim lgb As New LinearGradientBrush(New Point(5, 5), New Point(5, Me.Height - 5), Color.White, Color.White)
      130. lgb.InterpolationColors = blend
      131. e.Graphics.FillRectangle(lgb, rect)
      132. Catch
      133. End Try
      134. ' <~~~~~~~~~~~~~~~~~~~~~~
      135. ' paint polygons
      136. For i = offset To rect.Width Step 30
      137. 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)}
      138. ' set the clip property
      139. e.Graphics.Clip = New Region(rect)
      140. ' fill the region
      141. e.Graphics.FillPolygon(New SolidBrush(Color.FromArgb(40, 255, 255, 255)), point_feld)
      142. Next
      143. ' stringformat -> h:center, v:center
      144. Dim stringFormat As New StringFormat()
      145. stringFormat.Alignment = StringAlignment.Center
      146. stringFormat.LineAlignment = StringAlignment.Center
      147. ' set the clip property
      148. e.Graphics.Clip = New Region(Me.ClientRectangle)
      149. ' paint text and align center in rectangle
      150. e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.SystemDefault 'quality
      151. e.Graphics.DrawString(Me.Value & "%", Me.Font, New SolidBrush(Me.ForeColor), Me.ClientRectangle, stringFormat)
      152. End Sub
      153. End Class
      154. #End Region
      155. End Namespace



      Und falls Ihr zum Testen das Form auch so aufbaut wie ich, dann hier der Code dazu:
      Spoiler anzeigen

      VB.NET-Quellcode

      1. Option Explicit On
      2. Option Strict On
      3. Public Class frmMain
      4. Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
      5. WhiteAjaxProgressBar1.Value = TrackBar1.Value
      6. End Sub
      7. Private Sub TrackBar2_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar2.ValueChanged
      8. WhiteAjaxProgressBar2.Value = TrackBar2.Value
      9. End Sub
      10. Private Sub TrackBar3_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar3.ValueChanged
      11. WhiteAjaxProgressBar3.Value = TrackBar3.Value
      12. End Sub
      13. Private Sub TrackBar4_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar4.ValueChanged
      14. WhiteAjaxProgressBar4.Value = TrackBar4.Value
      15. End Sub
      16. End Class




      Ein ScreenShot des Controls:




      Download Demo:
      AjaxProgressBarFinal.exe

      Viel Spaß damit und ich freue mich auf Eure Vorschläge zur Verbesserung,
      Bruno :P
      habe ein paar probleme...

      Es kommen 23 Fehler wie "Der Typ Control ist nicht definiert" oder Der Typ "System.Windows.Forms.PaintEventArgs" ist nicht definiert.

      pls help...

      mfg

      Myrax schrieb:

      Gefällt mir wirklich. Kurze Frage, ohne jetzt den Sourcecode durchzuwühlen: wie haste das gemacht, dass die Quer-Balken nicht überlaufen

      Danke für Dein Interesse!

      An dieser Stelle passiert das:

      VB.NET-Quellcode

      1. e.Graphics.Clip = New Region(rect)


      Das Rechteck gibt die Region an, in der gezeichnet wird, der Rest wird weggeclipt.
      Diese Anweisung kann mehrfach in der Paint-Routine vorkommen und ist sehr praktisch.

      LG,
      Bruno

      Myrax schrieb:

      Dann noch ne Frage: wie haste das mit diesem Glasseffekt von den Querbalken?

      Die Polygone werden mit der Farbe "weiß" gezeichnet.
      Da ich aber den Alphawert angebe, sind sie Transparent.
      Das ist in diesem Fall die Zahl 40. Damit legt man die Transparenz fest.
      Geht mit jeder Farbe.

      VB.NET-Quellcode

      1. Color.FromArgb(40, 255, 255, 255)


      LG,
      Bruno