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); } }
上次回答过一个类似的问题,直接复制-粘贴了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(); } }
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); }
http://topic.csdn.net/u/20090519/16/a42be3c9-c65b-45f9-ad34-63953036ac9b.html
{
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
ThreadPool 、 Thread 这是什么?
{
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);
}
}
{
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();
}
}
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); }
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;
}
}