WinForm中實現跑馬燈的效果

 
在程序的構造函數中寫
{
            Timer HelpTime = new Timer();//實例化一個時間控件
            HelpTime.Enabled = true;//讓時間控件可用
            HelpTime.Interval = 150;//時間間隔150毫秒
            p = new PointF(this.HelpText.Size.Width, 0);
            HelpTime.Tick+=new EventHandler(HelpTime_Tick);//註冊時間控件的Tick事件
}
 public string text = "跑馬燈!csdn baihe_591,文字改變時,重新顯示,文字改變時,重新顯示,文字改變時,重新顯示,文字改變時,重新顯示!";
        PointF p;
        Font f = new Font("宋體", 10);
        Color c = Color.FromArgb(237,232,236);
        string temp;
        private void HelpTime_Tick(object sender, EventArgs e)
        {
            Graphics g = this.HelpText.CreateGraphics();
            SizeF s = new SizeF();
            s = g.MeasureString(text, f);//測量文字長度
            Brush brush = Brushes.Blue;
            g.Clear(c);//清除背景
            if (temp != text)//文字改變時,重新顯示
            {
                p = new PointF(this.HelpText.Size.Width, 0);
                temp = text;
            }
            else
                p = new PointF(p.X - 10, 0);//每次偏移10
            if (p.X <= -s.Width)
                p = new PointF(this.HelpText.Size.Width, 0);
            g.DrawString(text, f, brush, p);
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章