110,534
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
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);
}
}
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);
}
}
}