winform如何实现流动字幕

melody_zhong 2012-02-21 08:07:29
在窗体上我想让字体流动起来,但是不知道如何写代码,求大虾们帮忙呀!
...全文
240 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycproc 2012-02-21
  • 打赏
  • 举报
回复
放个timer记时器,如果你的文字是写在label里,有2种实现方法,第一种也就是将你的label动态的移动.
timer1_tick(...)
{
label1.left=label1.left-20;
if (label1.left <=-label1.width)
{
label1.left=this.width;
}
}

第二种方法,就是label不移动,而文字动态的添加和减少,不过这种稍微难以控制,不推荐,首先将label的textalign设为middlecenter。
private string str= "asdgasdgsdgadsgdgdddsdas ";
private int index=0;
timer1_tick(...)
{

if (index <str.length)
{
label1.Text=str.substring(0,index);
index=index+1;
}
else
{
index=0;
}
}
  • 打赏
  • 举报
回复
其实 实现 跑马灯效果 也只是。。。Time控件 。 还可以 做出 垂直的 跑马灯 效果。有兴趣的话 你自己琢磨 琢磨看。
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 perfect_zqm 的回复:]

引用 10 楼 zhizhuochangeing 的回复:
LZ,你所谓的流动字幕是 我们通常说的 跑马灯效果吧?

是呀,你有木有代码也拿出来让我好好学习呀
[/Quote]

LZ如果我没猜错的话,你应该是 北大青鸟的学生吧? 正在做第一学期 的毕业项目吧,也就是KTV,因为我最近 遇到很多。。。做KTV项目的,我就纳闷了,怎么近段时间 KTV 这么受欢迎啊? 一问,原来是。。。。。

好了,代码给你,你看看可行吗? 我试了下,貌似可以

在程序的构造函数中写
{ 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); }
老毕 2012-02-21
  • 打赏
  • 举报
回复
上次回答过一个类似的问题,直接复制-粘贴了

ublic partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
InitializeScrollTextWorker();
}

private BackgroundWorker _workerScrollText = new BackgroundWorker() { WorkerSupportsCancellation = true, WorkerReportsProgress = true };
private string _stringScrollText = @"老毕出品,版权所有。http://www.cnblogs.com/abbey";

private void InitializeScrollTextWorker()
{
_workerScrollText.DoWork += new DoWorkEventHandler(
(s, t) =>
{
int LENGTHOFSCROLLTEXT = _stringScrollText.Length;
int shift = 0;

while (true)
{
if (_workerScrollText.CancellationPending)
{
t.Cancel = true;
return;
}

Thread.Sleep(500);
_workerScrollText.ReportProgress(shift);

shift++;
if (shift > LENGTHOFSCROLLTEXT)
shift = 0;
}
});

_workerScrollText.ProgressChanged += new ProgressChangedEventHandler(
(s, t) =>
{
StringBuilder temp = new StringBuilder();
temp.Append(_stringScrollText.Substring(t.ProgressPercentage));
temp.Append(" ");
temp.Append(_stringScrollText.Substring(0, t.ProgressPercentage));

this.labelCopyright.Text = temp.ToString();
});
}

private void MainForm_Load(object sender, EventArgs e)
{
// start thread to scroll text of labelCopyright.
this.labelCopyright.Text = _stringScrollText;
_workerScrollText.RunWorkerAsync();
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_workerScrollText.CancelAsync();
}
}
melody_zhong 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhizhuochangeing 的回复:]
LZ,你所谓的流动字幕是 我们通常说的 跑马灯效果吧?
[/Quote]
是呀,你有木有代码也拿出来让我好好学习呀
  • 打赏
  • 举报
回复
LZ,你所谓的流动字幕是 我们通常说的 跑马灯效果吧?
zzzzv0 2012-02-21
  • 打赏
  • 举报
回复
public Form1()
{
InitializeComponent();
ThreadPool.QueueUserWorkItem(DrawText);
}

private string GetName()
{
return "歌曲名1";
}

private string GetNextName()
{
return "歌曲名2";
}

private void DrawText(object state)
{
float x = 200;
while (true)
{
string s = string.Format("正在播放的歌曲: {0} 下一首歌曲: {1}", GetName(), GetNextName());
Bitmap bmp = new Bitmap(200, 20);
Graphics g = Graphics.FromImage(bmp);
g.DrawString(s, this.Font, new SolidBrush(Color.Red), x--, 0);
x = x < -200 ? 200 : x;
this.pictureBox1.Image = bmp;
Thread.Sleep(20);
}
}
melody_zhong 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zzzzv0 的回复:]
using System.Threading;
[/Quote]
再问一下,如果我做KTV的点歌系统上面显示正在播放的歌曲和下一首歌曲的流动字幕要怎样修改这些代码呢?
melody_zhong 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zzzzv0 的回复:]
using System.Threading;
[/Quote]
非常谢谢你~~
zzzzv0 2012-02-21
  • 打赏
  • 举报
回复
using System.Threading;
melody_zhong 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zzzzv0 的回复:]
C# code

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ThreadPool.QueueUserWorkItem(DrawText);
}

private void DrawText(object s……
[/Quote]

ThreadPool 、 Thread 这是什么?
melody_zhong 2012-02-21
  • 打赏
  • 举报
回复
求代码呀~~~
zzzzv0 2012-02-21
  • 打赏
  • 举报
回复
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ThreadPool.QueueUserWorkItem(DrawText);
}

private void DrawText(object state)
{
string s = "123456789012345678901234567890";
float x = 200;
while (true)
{
Bitmap bmp = new Bitmap(200, 20);
Graphics g = Graphics.FromImage(bmp);
g.DrawString(s, this.Font, new SolidBrush(Color.Red), x--, 0);
x = x < -200 ? 200 : x;
this.pictureBox1.Image = bmp;
Thread.Sleep(20);
}
}
}

窗体上画个pictureBox1
licai1210 2012-02-21
  • 打赏
  • 举报
回复
没做过winform,但是我想应该可以通过定时器时刻改变字体的坐标达到流动的效果
孟子E章 2012-02-21
  • 打赏
  • 举报
回复
参见
http://topic.csdn.net/u/20090519/16/a42be3c9-c65b-45f9-ad34-63953036ac9b.html

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧