Label 控件实现跑马灯效果

来源:互联网 发布:数据大魔王txt离线下载 编辑:程序博客网 时间:2024/04/29 01:31
 public partial class LabelCt : System.Windows.Forms.Label
    {
        private PointF p;
        private Font f = new Font("宋体", 10);
        private Color c = Color.FromArgb(237, 232, 236);
        private string temp;


        public LabelCt()
        {
            Timer HelpTime = new Timer(); //实例化一个时间控件
            HelpTime.Enabled = true; //让时间控件可用
            HelpTime.Interval = 200; //时间间隔150毫秒
            p = new PointF(this.Size.Width, 0);
            HelpTime.Tick += new EventHandler(_Tick); //注册时间控件的Tick事件
        }


        private void _Tick(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            SizeF s = new SizeF();
            s = g.MeasureString(Text, f); //测量文字长度
            Brush brush = Brushes.Blue; //设置字体颜色


            g.Clear(c); //清除背景
            if (temp != Text) //文字改变时,重新显示
            {
                p = new PointF(this.Size.Width, 0);
                temp = Text;
            }
            else
                p = new PointF(p.X - 10, 0); //每次偏移10
            if (p.X <= -s.Width)
                p = new PointF(this.Size.Width, 0);
            g.DrawString(Text, f, brush, p);
        }
    }
0 0
原创粉丝点击