今天给大家分享一个实现滚动字幕的小程序,也是作为我学习的一种见证吧。(hh)
先来看看效果图吧。(我对界面要求不高,只要看着不吐就行)
以下是源码(需要就贴吧)
using System;
using System.Drawing;
using System.Windows.Forms;
namespace 窗体滚动字幕
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Label label_show = new Label() ;
public PointF point { get; set; }
public string text_Contents = "窗体滚动字幕";
Color BackGround_Color = Color.White;
Font text_font = new Font("黑体",50);
private void Form1_Load(object sender, EventArgs e)
{
this.label_show.Location = new Point(200, 150);
this.label_show.Size = new Size(200, 80);
this.label_show.Text = "";
this.Controls.Add(label_show);
this.timer1.Enabled = true;
this.timer1.Interval = 500;
point = new PointF(this.label_show .Width , 0);//初始化位置
}
private void timer1_Tick(object sender, EventArgs e)
{
Graphics graphics = this.label_show.CreateGraphics();//为标签创建绘图界面
graphics.Clear(BackGround_Color);//清除背景色
SizeF sizeF = new SizeF();
sizeF = graphics.MeasureString(text_Contents, text_font);//获取字符串长度
Brush brush = Brushes.Red;//字体颜色
point = new PointF(point.X -10, 0);//单次偏移量10
if (point .X <=-sizeF.Width ) //判断条件重新循环
{
point = new PointF(this.label_show .Width , 0);
}
graphics.DrawString(text_Contents ,text_font ,brush, point);//绘制字符串
}
}
}
最先把显示顺序写反了(从左往右),别扭死了。。。。。
这个小程序个人觉得新手还是值得一试的,不难。
对了这个小程序用到了Timer控件,你建个窗体程序,拖这个控件,然后直接贴就行了。