文字走马灯效果

来源:互联网 发布:ie无法打开淘宝网 编辑:程序博客网 时间:2024/04/30 15:14

最近在学ado.net

喜欢界面的一些小细节比如走马灯的文字效果

最终效果就是在白色窗体上跑字

我的form的BackColor是White的,为什么是White,因为我后面设置的Label里面清除背景的颜色也是White的,所以用统一的颜色,你也可以使用别的颜色

在窗体中托一个Label,将Name改为HelpText,为什么是HelpText,因为我参考的一手资料也是的,然后将AutoSize设置为False,这样我们可以自己调整大小具体大小结合自己设定的字体大小

上面的做完了,基本操作就OK了,下面要做的就是复制下面的代码了,可以结合你自己的需求,修改,我也会稍微写点注释,但是核心代码都是经过沉淀下来了,不改为妙,改改边边角角的效果就行了。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            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 = "。。。。。。。》》》》》》》";
        PointF p;
        Font f = new Font("宋体", 21);
        Color c = Color.FromArgb(255,255,255);
        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);
        }
       
    }
}


原创粉丝点击