using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace _秒黄灯闪烁的实现
{
    public partial class Form1 : Form
    {
        public int time = 30;
        public bool isGreen = true;
        public bool isRed = false;        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            timer1_Tick(sender, e);
        }        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen1 = new Pen(Color.Green);
            SolidBrush brush1 = new SolidBrush(Color.Green);
            if (time > 0 && time < 4 && isGreen)
            {//在0-3秒之间实现黄灯的闪烁效果                //哪位仁兄完成之? 要的就是这一段代码,像实际的交通灯一样闪烁效果,闪烁三次黄颜色以示预警。            }
            else
            {
                g.DrawEllipse(pen1, this.Width / 2, this.Height / 2, 20, 20);
                g.FillEllipse(brush1, this.Width / 2, this.Height / 2, 20, 20);
            }
        }        private void timer1_Tick(object sender, EventArgs e)
        {
            time--;
            if (time < 1)
            {
                time = 30;
                if (isGreen)
                {
                    isGreen = false;
                    isRed = true;
                }
                else
                {
                    isRed = false;
                    isGreen = true;
                }            }
            this.Refresh();
        }
    }

解决方案 »

  1.   

    从封装的角度,交通灯应该自己画自己。下面是一段交通灯控件的代码,通过设置它的状态来改变灯的颜色:trafficLight.SignalState = TrafficLight.State.Green;
    trafficLight.SignalState = TrafficLight.State.Blink;你也可以略微改变代码使得黄灯闪几下后自动进入红灯状态。
        public class TrafficLight : UserControl
        {
            public TrafficLight()
            {
                this.Size = new System.Drawing.Size(60, 60);
                timer.Interval = 500;
                timer.Tick += delegate {
                    isOnWhenBlinking = !isOnWhenBlinking;
                    lightColor = isOnWhenBlinking ? Color.Yellow : SystemColors.Control;
                    Invalidate();
                };
            }
            
            public State SignalState
            {
                get { return lightState; }
                set 
                {
                    lightState = value;
                    if (lightState == State.Blink)
                    {
                        timer.Start();
                        isOnWhenBlinking = true;
                        lightColor = Color.Yellow;
                    }
                    else
                    {
                        timer.Stop();
                        if (lightState == State.Off)
                        {
                            lightColor = SystemColors.Control;
                        }
                        else
                        {
                            lightColor = lightState == State.Green ? Color.Green : Color.Red;
                        }
                    }
                    Invalidate();
                }
            }
            
            protected override void OnPaint(PaintEventArgs e)
            {
                using (Graphics g = e.Graphics)
                {
                    e.Graphics.FillEllipse(new SolidBrush(lightColor), 0, 0, Width, Height);
                }
            }        public enum State
            {
                Off,
                Green,
                Blink,
                Red
            }        bool isOnWhenBlinking;
            State lightState = State.Green;
            Color lightColor = Color.Green;
            Timer timer = new Timer();
        }
      

  2.   

    其实我想过可不可以通过画出来的图形(圆)用透明度来控制,比如透明度由0变到1,在1秒钟之内实现一次这样的循环,循环三次。但是这样不知道行不行的通?我只知道Form是可以通过透明度来实现闪烁的,但图形不知道可不可以?有没有更好的方法?
      

  3.   

    图形能行不过就的看GDI了,个人认为