在 C# 中闪烁标签 backColor

blinking labels backColor in C#

我有这个包含 4 个标签的表单。我希望这些标签以指定的频率闪烁,例如 12.5、10、8 和 4 HZ。我使用了定时器,但它无法正常工作,它们闪烁的频率要低得多,我知道这是因为下面的 freqMethod 中嵌套了 if。我该如何解决这个问题?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Timers.Timer mainTimer; 
        private int counter = 0;
        Color lColor = Color.FromArgb(255, 192, 128);
        bool buttonPressed = false;

        public Form1()
        {

            InitializeComponent();
            label1.BackColor = lColor;
            label2.BackColor = lColor;
            label3.BackColor = lColor;
            label4.BackColor = lColor;
            mainTimer = new System.Timers.Timer(1);
            mainTimer.Elapsed += new ElapsedEventHandler(timerElapsed);
        }


        private void button2_Click(object sender, EventArgs e)
        {   
            if (buttonPressed)
            {
                mainTimer.Enabled = false;
                buttonPressed = !buttonPressed;
                counter = 0;
            }
            else
            {

                mainTimer.Enabled = true;
                buttonPressed = !buttonPressed;
                counter = 0;
            }
        }

        //Frequency Method

        public void freqMethod()
        {
           if (counter % 80 == 0)
               if (label4.backColor == lColor)
                   label4.backColor = Color.black;
               else
                   label4.backColor = lColor;
           if (counter % 100 == 0)
               if (label3.backColor == lColor)
                   label3.backColor = Color.black;
               else
                   label3.backColor = lColor;
           if (counter % 125 == 0)
               if (label2.backColor == lColor)
                   label2.backColor = Color.black;
               else
                   label2.backColor = lColor;
           if (counter % 250 == 0)
               if (label1.backColor == lColor)
                   label1.backColor = Color.black;
               else
                   label1.backColor = lColor;



        }
        private void timerElapsed(object source, ElapsedEventArgs e) {
            counter++;
            freqMethod();
        }

    }
}

您不需要计时器每秒迭代一次,因为您会跳过每 n 次迭代,而它们会占用太多资源。您可以仅使用 TimerInterval 值进行操作,以获得具有足够性能的所需频率。

例如,对于 8 Hz 的频率,您只需要定时器每 125 毫秒(每秒 8 次)触发一个事件。
我将提供一个频率为 double 的示例,以使其在小于 1 的间隔内工作。例如,如果将频率设置为 0.5,颜色将每 2 秒更改一次。

示例:

public Form1()
{
    double frequencyInHz = 8.0; // here goes your frequency
    int interval = (int)Math.Round(1000.0 / frequencyInHz); // 125ms in this case
    mainTimer = new Timer(interval);
    mainTimer.Elapsed += new ElapsedEventHandler(timerElapsed);
}   

private void timerElapsed(object source, ElapsedEventArgs e) {
    if (label2.BackColor == lColor)
        label2.BackColor = Color.Black;
    else
        label2.BackColor = lColor;
}

如果您需要多个标签来改变它们不同的颜色,您将需要制作多个计时器以获得良好的性能。

试试这个(除了 Joel 或 Yeldar 提出的定时器间隔修改):

if (counter % 80 == 0)
{
  label4.backColor = label4.backColor == lColor ? Color.black : lColor;
  label4.Refresh() ;
}

给定以下值(如果您想使用一个计时器同步它们),它们可以具有的公共间隔是 5 毫秒。所以你需要每 5ms 勾选定时器并检查频率。但请注意使用计时器,如下所述:

12.5hz = 80ms
10hz   = 100ms
8hz    = 125ms
4hz    = 250ms

来自 MSDN 使用 System.Timers.Timer (https://msdn.microsoft.com/en-us/library/system.timers.timer.interval(v=vs.110).aspx) 的备注请参阅备注部分。

You use the Interval property to determine the frequency at which the Elapsed event is fired. Because the Timer class depends on the system clock, it has the same resolution as the system clock. This means that the Elapsed event will fire at an interval defined by the resolution of the system clock if the Interval property is less than the resolution of the system clock. The following example sets the Interval property to 5 milliseconds. When run on a Windows 7 system whose system clock has a resolution of approximately 15 milliseconds, the event fires approximately every 15 milliseconds rather than every 5 milliseconds

但是如果你可以为每个计时器使用多个计时器,那么你可以像 Yeldar 提到的那样设置计时器的每个间隔。