Label Ein- und Ausfaden lassen.

    • VB.NET

    Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von markushettmann.

      Label Ein- und Ausfaden lassen.

      Hallo,

      hier mal ein Code, den ich benutze, um ein Label ein- oder auszufaden:

      Die beiden Public functions zum faden

      VB.NET-Quellcode

      1. ''' <summary>
      2. ''' Fades in a label slowly
      3. ''' </summary>
      4. ''' <param name="obj">the label to fade in</param>
      5. ''' <param name="destColor">the color, the labeltext should have</param>
      6. ''' <param name="steps">the steps to use</param>
      7. ''' <param name="waitForComplete">if true, the Fadeout functions waits, till the thread is complete, then returns true.</param>
      8. ''' <returns>true, if was waiting for a thread, and its complete, false if it shouldn't wait</returns>
      9. ''' <remarks></remarks>
      10. Public Function FadeIn(ByVal obj As Label, ByVal destColor As System.Drawing.Color, ByVal steps As Integer, Optional ByVal waitForComplete As Boolean = False) As Boolean
      11. Dim myT As New System.Threading.Thread(AddressOf FadeInThread)
      12. myT.Start(New Object() {obj, destColor, steps})
      13. If waitForComplete Then
      14. While myT.IsAlive
      15. Application.DoEvents()
      16. End While
      17. Else
      18. Return False
      19. End If
      20. Return True
      21. End Function
      22. ''' <summary>
      23. ''' Fades out a label
      24. ''' </summary>
      25. ''' <param name="obj">the label to fade out</param>
      26. ''' <param name="parent">the control, containing the label, musst have the .BackColor property (form, panel, ...)</param>
      27. ''' <param name="waitForComplete">if true, the Fadeout functions waits, till the thread is complete, then returns true.</param>
      28. ''' <param name="steps">The number of steps, the fadeout should take</param>
      29. ''' <returns>true, if was waiting for a thread, and its complete, false if it shouldn't wait</returns>
      30. ''' <remarks></remarks>
      31. Public Function FadeOut(ByVal obj As Label, ByVal parent As Object, ByVal steps As Integer, Optional ByVal waitForComplete As Boolean = False) As Boolean
      32. Dim myT As New System.Threading.Thread(AddressOf FadeOutThread)
      33. myT.Start(New Object() {obj, parent, steps})
      34. If waitForComplete Then
      35. While myT.IsAlive
      36. Application.DoEvents()
      37. End While
      38. Else
      39. Return False
      40. End If
      41. Return True
      42. End Function


      Hier die beiden private subs (die threads)

      VB.NET-Quellcode

      1. Private Sub FadeInThread(ByVal obj() As Object)
      2. Dim curLabel As Label = CType(obj(0), Label)
      3. Dim destColor As Color = CType(obj(1), Color)
      4. Dim steps As Integer = CType(obj(2), Integer)
      5. Dim curColor As Color = curLabel.ForeColor
      6. 'calculate the vars, X steps
      7. Dim Rs As Double = (Convert.ToDouble(destColor.R) - curColor.R) / steps
      8. Dim Gs As Double = (Convert.ToDouble(destColor.G) - curColor.G) / steps
      9. Dim Bs As Double = (Convert.ToDouble(destColor.B) - curColor.B) / steps
      10. 'doit
      11. For i As Integer = 1 To steps - 1
      12. curLabel.ForeColor = Color.FromArgb(curColor.R + i * Rs, _
      13. curColor.G + i * Gs, _
      14. curColor.B + i * Bs)
      15. System.Threading.Thread.Sleep(20)
      16. Next
      17. 'Final Value reached
      18. curLabel.ForeColor = destColor
      19. 'Fade in Complete
      20. End Sub
      21. Private Sub FadeOutThread(ByVal obj() As Object)
      22. Dim curLabel As Label = CType(obj(0), Label)
      23. Dim steps As Integer = CType(obj(2), Integer)
      24. Dim parObject As Object = obj(1)
      25. Dim curColor As Color = curLabel.ForeColor
      26. Dim backColor As Color = parObject.BackColor
      27. 'calculate the vars. X steps
      28. Dim Rs As Double = (Convert.ToDouble(backColor.R) - curColor.R) / steps
      29. Dim Gs As Double = (Convert.ToDouble(backColor.G) - curColor.G) / steps
      30. Dim Bs As Double = (Convert.ToDouble(backColor.B) - curColor.B) / steps
      31. 'doit
      32. For i As Integer = 1 To steps - 1
      33. curLabel.ForeColor = Color.FromArgb(curColor.R + i * Rs, _
      34. curColor.G + i * Gs, _
      35. curColor.B + i * Bs)
      36. System.Threading.Thread.Sleep(20)
      37. Next
      38. 'Final Value reached
      39. curLabel.ForeColor = backColor
      40. 'Fadout complete
      41. End Sub


      Benutzungshinweis:

      VB.NET-Quellcode

      1. Me.Label1.Text = "Text to fade out"
      2. Me.FadeOut(Me.Label1, Me, 50, True)
      3. Me.Label1.Text = "Text to fade in"
      4. Me.FadeIn(Me.Label1, Color.Black, 50, True)

      singu schrieb:

      Kannst du kurz erklären was das Skript genau macht?


      FadeOut

      Übergeben werden: ParentControl, label, Anzahl der schritte und eine boolsche variable.

      Dann wird ein Thread gestartet, der die Hintergrundfarbe des ParentControls bestimmt, und dann
      mit der Anzahl gegebener schritte die Vordergrundfarbe des labels ändert, bis es gleich der hintergrundfarbe ist.

      Setzt man die "Waitfor" flag, macht der Hauptthread erst weiter, wenn der Fadeout abgeschlossen ist.

      Fadein
      Übergeben werden: Label, gewünschte Farbe, anzahl der Schritte und eine boolsche variable

      Dann wird ein Thread gestartet, der die Vordergrundfarbe des labels langsam an die gegebene Farbe annähert.
      (ebenfalls linear).

      Setzt man die "Waitfor" flag, macht der Hauptthread erst weiter, wenn der Fadein abgeschlossen ist.

      Tjo, das wars eig schon.

      Nach jeder "anpassung" wird 20 ms gewartet. Wenn du also 10 schritte angibst, dauert der vorgang ~ 200ms.
      mhh gute Idee aber ich verstehe den Aufwand nicht...
      All dein Code lässt sich mit label.location bewältigen...wenn ich deine Idee richtig interpretiert habe.

      Du setzt den Code in Timer..

      VB.NET-Quellcode

      1. Einfahren.Interval = 1
      2. if label1.location < 100 then
      3. Label1.Location.x = new point(label1.location.x + 1,label1.location.y)
      4. end if
      5. ausfahren.interval = 1
      6. if label1.location > 100 then
      7. Label1.Location.x = new point(label1.location.x - 1,label1.location.y)
      8. end if




      so was in der art...ich habe jetzt nur grob geantwortet...genaueres müsste man in der VB Console schreiben

      LG :)

      maxlcp schrieb:

      mhh gute Idee aber ich verstehe den Aufwand nicht...
      All dein Code lässt sich mit label.location bewältigen...wenn ich deine Idee richtig interpretiert habe.

      Du setzt den Code in Timer..

      VB.NET-Quellcode

      1. Einfahren.Interval = 1
      2. if label1.location < 100 then
      3. Label1.Location.x = new point(label1.location.x + 1,label1.location.y)
      4. end if
      5. ausfahren.interval = 1
      6. if label1.location > 100 then
      7. Label1.Location.x = new point(label1.location.x - 1,label1.location.y)
      8. end if




      so was in der art...ich habe jetzt nur grob geantwortet...genaueres müsste man in der VB Console schreiben

      LG :)



      Nein du hast es falsch verstanden.
      Mit seinem Code wird ein Label ein und ausgeblendet.
      Also er weißt einem Label sozusagen die Opacityeigenschaft zu