请教各位C#熟手,在C#中怎样实现文字滚动??

A241551551 2006-09-10 10:41:08
呵呵 !初学C#好多不懂,请你讲清楚点哈~~~是不是用一个label和一个timer可以实现啊??我不知道怎么写代码???请教各位 谢谢!!!!!
...全文
680 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxcayumi 2006-09-11
  • 打赏
  • 举报
回复
留个邮箱,我有例子
siugwan 2006-09-10
  • 打赏
  • 举报
回复
给你个例子
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Text;

namespace MovingText
{
public class TextMoverForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label;
//
// Make the worker thread a member so that it can be referred to
// after it has died
//
private Thread thread;
public TextMoverForm()
{
InitializeComponent();
thread = new Thread( new ThreadStart( DrawMovingText ) );
}
private void InitializeComponent()
{
this.label = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label
//
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(325, 16);
this.label.TabIndex = 0;
this.label.Text = "Click Anywhere Or Resize the Form to Restart The Moving Text";
this.label.Click += new System.EventHandler(this.label_Click);
//
// TextMoverForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(492, 466);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.label});
this.Name = "TextMoverForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Moving Text";
this.Resize += new System.EventHandler( this.Form_Resize );
this.Load += new System.EventHandler( this.Form_Load );
this.MouseUp += new System.Windows.Forms.MouseEventHandler( this.Form_MouseUp );
this.ResumeLayout( false );
}
static void Main()
{
Application.Run( new TextMoverForm() );
}
//
// All of these events will restart the thread that draws the text
//
private void Form_Load( object sender, System.EventArgs e )
{
StartThread();
}
private void Form_MouseUp( object sender, System.Windows.Forms.MouseEventArgs e )
{
StartThread();
}
private void Form_Resize( object sender, System.EventArgs e )
{
StartThread();
}
private void label_Click( object sender, System.EventArgs e )
{
StartThread();
}
private void StartThread()
{
thread.Abort();
// give the thread time to die
Thread.Sleep( 100 );
Invalidate();
thread = new Thread( new ThreadStart( DrawMovingText ) );
thread.Start();
}
/// <summary>
/// DrawMovingText is the thread function that will draw and update the text
/// </summary>
private void DrawMovingText()
{
Graphics grfx = CreateGraphics();
Font font = new Font( "Courier New", 20, FontStyle.Bold );
string spaces = " ";
//
// StringBuilder is used to allow for efficient manipulation of one string,
// rather than generating many separate strings
//
StringBuilder str = new StringBuilder( "Catch Me If You Can..." + spaces );

Rectangle rect = CreateRect( grfx, str, font );

int numCycles = str.Length * 3 + 1;
for( int i = 0; i < numCycles; ++i )
{
grfx.FillRectangle( Brushes.White, rect );
grfx.DrawString( str.ToString(), font, Brushes.Red, rect );
// relocate the first char to the end of the string
str.Append( str[0] );
str.Remove( 0, 1 );
// pause for visual effect
Thread.Sleep( 150 );
}
grfx.Dispose();
}
/// <summary>
/// CreateRect will create a rectangle just big enough to display the text, except the spaces,
/// that is centered in the client area
/// </summary>
/// <param name="grfx"></param>
/// <param name="str"></param>
/// <param name="font"></param>
/// <returns></returns>
private Rectangle CreateRect( Graphics grfx, StringBuilder str, Font font )
{
int w = (int)grfx.MeasureString( str.ToString(), font ).Width + 5; // +5 to allow last char to fit
int h = (int)grfx.MeasureString( str.ToString(), font ).Height;
int x = (int)this.Width / 2 - w / 2;
int y = (int)this.Height / 2 - h;
return new Rectangle( x, y, w, h );
}
}
}
lionelwy 2006-09-10
  • 打赏
  • 举报
回复
楼主的方法是可以实现的。
jrl5365 2006-09-10
  • 打赏
  • 举报
回复
http://www.bfsj.net.cn/Article/sywz/wlyy/200607/627.html
自己找你要的效果吧

110,549

社区成员

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

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

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