如何制作闪烁的像素?

时间:2014-01-08 04:48:41

标签: c# .net winforms

在form1中我有一个timer1 tick事件:

private void timer1_Tick(object sender, EventArgs e)
        {
            if (CloudEnteringAlert.cloudsdetected == true)
            {
                timer1.Enabled = true;
            }
            else
            {
                timer1.Enabled = false;
            }
        }

在顶级的CloudEnteringAlert类中,我做了:

public static bool cloudsdetected;

然后将其设置为false作为默认值:

static CloudEnteringAlert()
        {
            cloudsdetected = false;
        }

然后在一个方法中,我将其设置为true或false:

if (clouds.Count == 0)
            {
                cloudsdetected = false;
                clouds = null;
            }
            else
            {
                cloudsdetected = true;
            }

如果列表不为空,则云是一个列表,这意味着有云。 这意味着我希望paint事件中的像素闪烁。

在pictureBox1的paint事件中,我有:

foreach (PointF pt in clouds)
                {
                    e.FillEllipse(Brushes.Yellow, pt.X * (float)currentFactor, pt.Y * (float)currentFactor, 2f, 2f);
                }

现在这只是为黄色像素着色。 现在我想以某种方式使用Timer1,如果cloudsdetected = true;然后启用真正的计时器,并且每秒都会将绘画事件中像素的颜色从黄色变为透明色或红色,然后再变为黄色,这样看起来就像闪烁一样。

1 个答案:

答案 0 :(得分:0)

您需要在计时器中设置颜色值。一种方法:

拥有一系列云彩颜色:

// blinking colors: yellow, red, yellow, transparent, repeat...
var cloudColors = new [] { Brushes.Yellow, Brushes.Red, Brushes.Yellow, Brushes.Transparent }
// current color index
var cloudColorIndex = 0;

在计时器事件中设置颜色索引:

private void cloudTimer_Tick(object sender, EventArgs e)
{
    cloudColorIndex = (cloudColorIndex + 1) % cloudColors.Length;
}

在您的绘画事件中,您现在可以使用当前颜色而不是固定颜色:

foreach (PointF pt in clouds)
{
    e.FillEllipse(cloudColors[cloudColorIndex], pt.X * (float)currentFactor, pt.Y * (float)currentFactor, 2f, 2f);
}
相关问题