我将如何使按钮闪烁?

时间:2020-07-28 13:52:55

标签: c# winforms events

我正在制作一个用于玩Connect4的应用程序。我想通过让可点击的按钮闪烁播放器颜色的旋转来指示播放器更清晰地转向。我想知道我将如何去做。抱歉,如果以前已经回答过,请给我链接。

bgwMain = new BackgroundWorker();
bgwMain.WorkerSupportsCancellation = true;

int Red;
int Green;
int Blue;

bgwMain.DoWork += ;

while (!mre.WaitOne())
{
    for (int i = 0; i != 255; i++)
    {
        Red = i;
        Green = i;

        for (int z = 42; z <= btnLongArray.Length - 1; z++)
        {
            btnLongArray[z].BackColor = Color.FromArgb(Red, Green, 0);
        }

        if (i == 255)
        {
            for (int x = 255; x != 0; x--)
            {
                Red = x;
                Green = x;

                for (int z = 42; z <= btnLongArray.Length - 1; z++)
                {
                    btnLongArray[z].BackColor = Color.FromArgb(Red, Green, 0);
                }
            }
        }
    }
}

我对事件驱动的编程有很好的了解,我很确定这将需要某种事件检查器或后台工作程序。我不确定如何进行这项工作。我当前的错误是在while循环上“!mre.waitone”和“ bgwMain.DoWork + =;。”。由于我上次打开该项目已经有一段时间了,所以我不确定我在试图做什么。我相信我尝试调用手动重置事件类,无论哪种方式,我都不确定。请让我知道如何实现这一目标,谢谢!

1 个答案:

答案 0 :(得分:0)

// Keeps track of the number of blinks
private int m_nBlinkCount = 0;

// ...

// tmrTimer is a component added to the form.
tmrTimer.Tick += new EventHandler(OnTimerTick);

m_nBlinkCount = 0;
tmrTimer.Interval = 1000; // 1 second interval
tmrTimer.Start();

// In Tick event, keep changing the background color of your button

private void OnTimerTick ( Object sender, EventArgs eventargs)
{
    if (btnLongArray.BackColor == Color.Red)
        btnLongArray.BackColor = Color.Transparent;
    else
        btnLongArray.BackColor = Color.Red;

    m_nBlinkCount++;

    if ( m_nBlinkCount >= 10 )
        tmrTimer.Stop ();
}
相关问题