C#滚动字幕的实现⽅法
本⽂实例讲述了C#滚动字幕的实现⽅法,分享给⼤家供⼤家参考。具体⽅法如下:
在c#中其实滚动屏幕的实现其实很简单,只需要⽤到Graphics.DrawString⽅法. Graphics.DrawString (String s, Font font, Brush brush, PointF point) 在指定位置并且⽤指定的 Brush 和 Font 对象绘制指定的⽂本字符串。
参数说明:
s 要绘制的字符串。
font 它定义字符串的⽂本格式。
brush 它确定所绘制⽂本的颜⾊和纹理。
point 结构,它指定所绘制⽂本的左上⾓。
其中,我们要⽤到的就是point函数,通过控制它的X或Y参数来控制⽂字的偏移量.下⾯以⽔平滚动字幕为例.
复制代码代码如下:
private  Label label = new Label();
public string text="csdn baihe_591";
private void FrmShow_Load(object sender, EventArgs e)
{
this.label.Location = new Point(149, 13);
this.label.Size = new Size(134, 16);
this.Controls.Add (label);
this.label.Text = "";
this.timer1.Enabled = true;
this.timer1.Interval = 500;
p = new PointF(this.label.Size.Width, 0);
}
PointF p;
Font f = new Font("宋体", 10);
Color c = Color.White;
string temp;
private void timer1_Tick(object sender, EventArgs e)
{
Graphics g = this.label.CreateGraphics();
SizeF s = new SizeF();
s = g.MeasureString(text, f);//测量⽂字长度
Brush brush = Brushes.Black;
g.Clear(c);//清除背景
if (temp != text)//⽂字改变时,重新显⽰
{
p = new PointF(this.label.Size.Width, 0);
temp = text;
}
else
p = new PointF(p.X - 10, 0);//每次偏移10
if (p.X <= -s.Width)
html滚动字幕p = new PointF(this.label.Size.Width, 0);
g.DrawString(text, f, brush, p);
}
希望本⽂所述对⼤家的C#程序设计有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。