将标签的颜色从1淡化到另一个

时间:2014-09-03 20:37:42

标签: vb.net

我制作了一个非常简单的倒数计时器。我希望标签(数字)的颜色随着计时器滴答而改变。我想从绿色开始,然后在计时器滴答00:00时淡入(转换)为红色。

计时器工作正常,一旦计时器到达00:00,我也可以将标签变为红色。我希望它会消失。这是代码的一部分。

'handles the label ticking down'
Private Sub tmrCountdown_Tick(sender As Object, e As EventArgs) Handles tmrCountdown.Tick
    Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
    If ts.Milliseconds > 0 Then
        lblTime.Text = ts.ToString("mm\:ss")
        lblTime.ForeColor = Color.FromArgb(0, 255, 0)
    Else
        lblTime.ForeColor = Color.FromArgb(255, 0, 0) 'changes label color to red when it hits 00:00'
        'stops the timer once the label reaches 00:00
        lblTime.Text = "00:00"


        'Plays sound when timer hits 00:00'
        My.Computer.Audio.Play(My.Resources.alarm, AudioPlayMode.BackgroundLoop)
        tmrCountdown.Stop()

        End If
End Sub

4 个答案:

答案 0 :(得分:3)

我知道这是一个老线程,但我正在寻找像这样的东西,标签发红光。为此,我创建了一个计时器,然后设置一些条件计数器来改变标签颜色的RGB值。更改率由timer.interval值设置。希望这有助于某人。

Public Class AlertWindow

Dim Red As Integer = 0
Dim Green As Integer = 0
Dim Blue As Integer = 0
Dim CountUp As Boolean = True

Private Sub AlertWindow_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


    Timer1.Enabled = True

End Sub

   Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

    If CountUp = True Then

        If Red < 253 Then
            Red = Red + 1
        Else
            CountUp = False
        End If

    End If

    If CountUp = False Then

        If Red > 0 Then
            Red = Red - 1
        Else
            CountUp = True
        End If

    End If

    Label1.ForeColor = Color.FromArgb(Red, Blue, Green)

End Sub

结束班

答案 1 :(得分:0)

对于淡入,您可以使用Alpha参数

例如:

Static Alpha As Integer

    lblTime.BackColor = Color.FromArgb(Alpha , 255, 0, 0)

    Alpha  += 5 'amount of opacity change for each timer tick
    If Alpha  > 255 Then lblTime.Enabled = False 'finished fade-in

答案 2 :(得分:0)

当您可以将颜色平均时,这会更好。

您必须根据计时器间隔计算出tickCount的值:

Private tickCount As Integer = 100
Private tickValue As Integer = 0

Private Sub tmrCountdown_Tick(sender As Object, e As EventArgs) _
                              Handles tmrCountdown.Tick
  If tickValue > tickCount Then
    tmrCountdown.Stop()
    tickValue = 0
  Else
    lblTime.ForeColor = AvgColor(Color.Green, Color.Red, tickValue, tickCount)
    tickValue += 1
  End If
End Sub

Private Function AvgColor(startColor As Color,
                          finalColor As Color,
                          colorValue As Integer,
                          colorCount As Integer) As Color
  Dim r1 As Integer = startColor.R
  Dim g1 As Integer = startColor.G
  Dim b1 As Integer = startColor.B

  Dim r2 As Integer = finalColor.R
  Dim g2 As Integer = finalColor.G
  Dim b2 As Integer = finalColor.B

  Dim avgR As Integer = r1 + ((r2 - r1) * colorValue) / colorCount
  Dim avgG As Integer = g1 + ((g2 - g1) * colorValue) / colorCount
  Dim avgB As Integer = b1 + ((b2 - b1) * colorValue) / colorCount

  Return Color.FromArgb(avgR, avgG, avgB)
End Function

答案 3 :(得分:0)

'RGB_1 = Timer;
'RGB_2 = Timer;
'RGB_3 = Timer;
'Label1 = Label;
'RGB1 = Progressbar;
'RGB2 = Progressbar;
'RGB3 = Progressbar.



Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    RGB1.Value = 255
    RGB_1.Interval = 10
    RGB_2.Interval = 10
    RGB_3.Interval = 10
RGB_1.Start()
End Sub

Private Sub RGB_1_Tick(sender As Object, e As EventArgs) Handles RGB_1.Tick
    If RGB2.Value = 255 Then
        If RGB1.Value = 0 Then
            If RGB3.Value = 255 Then
                RGB_1.Stop()
                RGB_2.Start()
            Else
                RGB3.Value += 1
            End If
        Else
            RGB1.Value -= 1
        End If
    Else
        RGB2.Value += 1
    End If
    SetColor()
End Sub

Private Sub RGB_2_Tick(sender As Object, e As EventArgs) Handles RGB_2.Tick
    If RGB3.Value = 255 Then
        If RGB2.Value = 0 Then
            If RGB1.Value = 255 Then
                RGB_2.Stop()
                RGB_3.Start()
            Else
                RGB1.Value += 1
            End If
        Else
            RGB2.Value -= 1
        End If
    Else
        RGB3.Value += 1
    End If
    SetColor()
End Sub

Private Sub RGB_3_Tick(sender As Object, e As EventArgs) Handles RGB_3.Tick
    If RGB3.Value = 0 Then
        RGB_3.Stop()
        RGB_1.Start()
    Else
        RGB3.Value -= 1
    End If
    SetColor()
End Sub

Sub SetColor()
    Label1.ForeColor = Color.FromArgb(RGB1.Value, RGB2.Value, RGB3.Value)
End Sub