Hallo,
hier mal ein Code, den ich benutze, um ein Label ein- oder auszufaden:
Die beiden Public functions zum faden
Hier die beiden private subs (die threads)
Benutzungshinweis:
hier mal ein Code, den ich benutze, um ein Label ein- oder auszufaden:
Die beiden Public functions zum faden
VB.NET-Quellcode
- ''' <summary>
- ''' Fades in a label slowly
- ''' </summary>
- ''' <param name="obj">the label to fade in</param>
- ''' <param name="destColor">the color, the labeltext should have</param>
- ''' <param name="steps">the steps to use</param>
- ''' <param name="waitForComplete">if true, the Fadeout functions waits, till the thread is complete, then returns true.</param>
- ''' <returns>true, if was waiting for a thread, and its complete, false if it shouldn't wait</returns>
- ''' <remarks></remarks>
- 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
- Dim myT As New System.Threading.Thread(AddressOf FadeInThread)
- myT.Start(New Object() {obj, destColor, steps})
- If waitForComplete Then
- While myT.IsAlive
- Application.DoEvents()
- End While
- Else
- Return False
- End If
- Return True
- End Function
- ''' <summary>
- ''' Fades out a label
- ''' </summary>
- ''' <param name="obj">the label to fade out</param>
- ''' <param name="parent">the control, containing the label, musst have the .BackColor property (form, panel, ...)</param>
- ''' <param name="waitForComplete">if true, the Fadeout functions waits, till the thread is complete, then returns true.</param>
- ''' <param name="steps">The number of steps, the fadeout should take</param>
- ''' <returns>true, if was waiting for a thread, and its complete, false if it shouldn't wait</returns>
- ''' <remarks></remarks>
- Public Function FadeOut(ByVal obj As Label, ByVal parent As Object, ByVal steps As Integer, Optional ByVal waitForComplete As Boolean = False) As Boolean
- Dim myT As New System.Threading.Thread(AddressOf FadeOutThread)
- myT.Start(New Object() {obj, parent, steps})
- If waitForComplete Then
- While myT.IsAlive
- Application.DoEvents()
- End While
- Else
- Return False
- End If
- Return True
- End Function
Hier die beiden private subs (die threads)
VB.NET-Quellcode
- Private Sub FadeInThread(ByVal obj() As Object)
- Dim curLabel As Label = CType(obj(0), Label)
- Dim destColor As Color = CType(obj(1), Color)
- Dim steps As Integer = CType(obj(2), Integer)
- Dim curColor As Color = curLabel.ForeColor
- 'calculate the vars, X steps
- Dim Rs As Double = (Convert.ToDouble(destColor.R) - curColor.R) / steps
- Dim Gs As Double = (Convert.ToDouble(destColor.G) - curColor.G) / steps
- Dim Bs As Double = (Convert.ToDouble(destColor.B) - curColor.B) / steps
- 'doit
- For i As Integer = 1 To steps - 1
- curLabel.ForeColor = Color.FromArgb(curColor.R + i * Rs, _
- curColor.G + i * Gs, _
- curColor.B + i * Bs)
- System.Threading.Thread.Sleep(20)
- Next
- 'Final Value reached
- curLabel.ForeColor = destColor
- 'Fade in Complete
- End Sub
- Private Sub FadeOutThread(ByVal obj() As Object)
- Dim curLabel As Label = CType(obj(0), Label)
- Dim steps As Integer = CType(obj(2), Integer)
- Dim parObject As Object = obj(1)
- Dim curColor As Color = curLabel.ForeColor
- Dim backColor As Color = parObject.BackColor
- 'calculate the vars. X steps
- Dim Rs As Double = (Convert.ToDouble(backColor.R) - curColor.R) / steps
- Dim Gs As Double = (Convert.ToDouble(backColor.G) - curColor.G) / steps
- Dim Bs As Double = (Convert.ToDouble(backColor.B) - curColor.B) / steps
- 'doit
- For i As Integer = 1 To steps - 1
- curLabel.ForeColor = Color.FromArgb(curColor.R + i * Rs, _
- curColor.G + i * Gs, _
- curColor.B + i * Bs)
- System.Threading.Thread.Sleep(20)
- Next
- 'Final Value reached
- curLabel.ForeColor = backColor
- 'Fadout complete
- End Sub
Benutzungshinweis: