闪烁选取标签(已用时间)

时间:2012-12-11 16:06:23

标签: c# timer

我正在制作一个正在运行的文本软件,其中有一些文本在marqueelabel控件中交替运行。我想在文本的每一个转折点,marqueelabel的背景都会闪烁。

我使用三个计时器控件,即两个System.Windows.Forms.Timer(Timer1和timer2)和一个System.Timers.Timer(elapsedTime)。 Timer1用于替换一定时间内的文本。闪烁所需的定时器2 由elapsedTime设置的2秒文本。一切顺利,除了文字不会像我预期的那样在文本的每个转弯处闪烁。

以下是代码:

using System; 
using System.Timers; 
using System.Threading; 
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace RunningText
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        private Dictionary<int,textData> dict = new Dictionary<int,textData>();
        private string[] textContent;
        private string[] textDuration;
        private int      textCount  = 0;
        private int      textIdx    = 0;
        private System.Timers.Timer elapsedTime;

    struct textData
    {
        public string content;
        public int duration;//in second
    }

    public MainForm()
    {
        InitializeComponent();

        fillData();

        timer1.Start();
        loadText();
    }

    public void fillData()
    {
        textData td;
        td.content = "Love means never having to say you're sorry";
        td.duration = 15;
        dict.Add(0,td);

        td.content = "A long time ago, in a galaxy far, far away...";
        td.duration = 20;
        dict.Add(1,td);

        td.content = "Just when you thought it was safe to go back in the water...";
        td.duration = 15;
        dict.Add(2,td);
    }

    public void loadText()
    {
        textCount = dict.Count;
        if(textCount > 0)
        {
            textContent     = new string[textCount];
            textDuration    = new string[textCount];

            for(int i=0;i<textCount;i++)
            {
                textContent[i] = dict[i].content;
                textDuration[i] = dict[i].duration.ToString();
            }
            textIdx = 0;
            showText(textContent[textIdx],textDuration[textIdx]);           

        }
    }

    private void showText(string content,string duration)
    {
        timer1.Stop();
        timer2.Stop();

        marqueeLabel1.Text = content;

        int second = Convert.ToInt32(duration) * 1000;
        timer1.Interval = second;

        timer1.Start();

        elapsedTime = new System.Timers.Timer(10000);
        elapsedTime.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
        elapsedTime.Interval = 2000;
        elapsedTime.AutoReset = true;
        elapsedTime.Start();

        timer2.Tick += blinkLabel; 
        timer2.Interval = 200; 
        timer2.Start();
    }

    void Timer1Tick(object sender, EventArgs e)
    {
        if(textCount > 0)
        {
            textIdx++;
            if(textCount == textIdx)
            {
                textIdx = 0;
            }
            showText(textContent[textIdx],textDuration[textIdx]);
        }
    }
    private void blinkLabel(object sender, EventArgs e) 
    {
        if (marqueeLabel1.BackColor == Color.White) 
            marqueeLabel1.BackColor = Color.Gold;
        else 
            marqueeLabel1.BackColor = Color.White;
    }

    private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
    {
        timer2.Stop();
        elapsedTime.Stop();
        marqueeLabel1.BackColor = Color.White;
    }
}

}

完整文件和marqueelabel控件可在此处找到:http://www.ziddu.com/download/21091002/RunningText.zip.html

0 个答案:

没有答案