Rubrik: Grafik und Font | VB-Versionen: VB.NET | 11.12.06 |
Threadsichere Gif-Animation Darstellung einer Gif-Animation in einer PictureBox während bspw. eine Datenbankabfrage läuft | ||
Autor: Bacik Harald / Zero-G. | Bewertung: | Views: 16.658 |
www.intelli-soft.at | System: WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Manchmal hat man z.B. bei Datenbankabfragen eine längere Wartezeit. Um dem User aber zu zeigen, dass das Programm nicht abgesürzt ist, oder die Datenbankabfrage noch läuft, kann man in einer PictureBox eine immerwiederkehrende Gif-Animation laufen lassen. Bsp. ist das mySQL Control Center oder auch der mySQL Query Browser.
Der Code ist eine Klasse, die sehr einfach ist:
#Region " GIF Animation in einer Picturebox " Public Class Animation Private AniPictureBox As PictureBox Private AniImage As Bitmap ' Ani_Starten ist die Sub, die vom neuen Thread aufgerufen wird ' und in dem die Animation gesetzt wird Private Sub Ani_Starten() PBoxProperty.Image = ImageProperty End Sub ' Die Eigenschaft für die PictureBox Private Property PBoxProperty() As PictureBox Get Return AniPictureBox End Get Set(ByVal value As PictureBox) AniPictureBox = value End Set End Property ' Die Eigenschaft für das Bitmap / Gif-Animation Private Property ImageProperty() As Bitmap Get Return AniImage End Get Set(ByVal value As Bitmap) AniImage = value End Set End Property Public Sub Animation(ByVal PBox As PictureBox, ByVal AnimationImage As Bitmap) Dim AniProperty As New Animation AniProperty.PBoxProperty = PBox AniProperty.ImageProperty = AnimationImage ' Ausführung des Threads Dim _Thread As New Threading.Thread(AddressOf AniProperty.Ani_Starten) If _Thread.IsAlive = False Then _Thread.Start() Else _Thread.Abort() PBox.Image = AnimationImage End If End Sub End Class #End Region
So, innerhalb des Codes kann man dann mit folgendem Befehl die Klasse aufrufen:
Dim Animation As New Animation Animation.Animation(PictureBox1, _ New Bitmap(Application.StartupPath & "\myAniGif.gif")) ' Hier wird dann z.B. die Datenbankabfrage gestaltet ' ... ' Nochmal die Animation aufrufen, damit sie wider gestoppt wird Animation.Animation(PictureBox1, Nothing)
So, das war es auch schon!
Viel Spass beim Probieren