C#文字滚动 做项目的时候 我想用一个lable控件作出一个从左向右的滚动效果,当它到了最左边的时候又会自动的回到右边 然后就是一直的滚动 从右边到左边一直循环这滚动 请问一下 我该怎么样去做它 还忘各位不吝赐教.... 谢谢了 (最好能给点代码的提示) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void Form1_Load(object sender, EventArgs e) { this.label1.Location = new Point(0, 0); } int xPox = 0; bool ist = true; private void timer1_Tick(object sender, EventArgs e) { this.label1.Location = new Point(xPox,this.label1.Location.Y); if (xPox == this.Width-this.label1.Size.Width) ist = false; if(xPox==0) ist = true; if (ist) xPox++; else xPox--; } 这个小功能如何实现?[label上的文字从右往左滚动显示]http://topic.csdn.net/u/20090606/10/5fc58b85-d448-4449-a46e-feb3fd9b0bcc.html你稍微改一下就行了 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Threading;namespace WindowsApplication37{ public partial class Form1 : Form { Button B = new Button(); Button B2 = new Button(); Marquee M = null; public Form1() { InitializeComponent(); B.Parent = this; B.Text = "开始"; B.Click += new EventHandler(B_Click); B2.Parent = this; B2.Text = "停止"; B2.Click += new EventHandler(B2_Click); B2.Location = Point.Add(B.Location, new Size(B.Width, 0)); } void B_Click(object sender, EventArgs e) { M = new Marquee(); M.Start(this, "ABCDEFG", Point.Add(B.Location, new Size(0, B.Height)), new Size(100, 50)); } void B2_Click(object sender, EventArgs e) { M.Stop(); M = null; } } class Marquee { PictureBox PB = new PictureBox(); int X = 0; String DrawText = String.Empty; Bitmap OrgBmp = null; delegate void SetImage(Image NewImage); public void Start(Form Parent, String Text, Point Location, Size RectSize) { this.DrawText = Text; PB.Parent = Parent; PB.Location = Location; PB.Size = RectSize; X = RectSize.Width; PB.Visible = true; PB.Image = new Bitmap(PB.ClientRectangle.Width, PB.ClientRectangle.Height); Graphics G = Graphics.FromImage(PB.Image); G.FillRectangle(new SolidBrush(PB.BackColor), PB.ClientRectangle); G.Dispose(); OrgBmp = new Bitmap(PB.Image); Thread T = new Thread(new ThreadStart(DoDraw)); T.Start(); } public void Stop() { PB.Visible = false; } void DoDraw() { while (PB.Visible) { Bitmap CacheBmp = new Bitmap(OrgBmp); Graphics G = Graphics.FromImage(CacheBmp); G.DrawString(DrawText, new Font("宋体", 10), new SolidBrush(Color.Black), new PointF(X = X-- < -G.MeasureString(this.DrawText, new Font("宋体", 10)).Width ? PB.Size.Width : X, 0)); G.Dispose(); PB.Invoke(new SetImage(DoSetImage), new Object[] { CacheBmp }); Thread.Sleep(30); } } void DoSetImage(Image NewImage) { PB.Image = NewImage; } }} bool ist = true;//为true时字幕向右走,反之向左走 private void timer1_Tick(object sender, EventArgs e) { this.label1.Location = new Point(xPox,this.label1.Location.Y); if (xPox == this.Width-this.label1.Size.Width)//字幕到了最右边 ist = false; if(xPox==0)//字幕到了最左边 ist = true; if (ist) xPox++;//右走 else xPox--;//左走 } 一楼的似乎实现不了的……试试这个:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace 欢迎条{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.timer1.Interval = 10; this.timer1.Enabled = true; //this.WindowState = FormWindowState.Maximized; } private void timer1_Tick(object sender, EventArgs e) { label1.Left = label1.Left - 2; if (label1.Right<0) { label1.Left = this.Width; } } private void button1_Click(object sender, EventArgs e) { this.Close(); } }} 抱歉了,一楼的是可以的,实现的是字体向左向右移动的public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.timer1.Interval = 1; this.timer1.Enabled = true; //this.WindowState = FormWindowState.Maximized; } bool i = true; int xPox; private void timer1_Tick(object sender, EventArgs e) { this.label1.Location = new Point(xPox, this.label1.Location.Y); if (xPox > this.Width - this.label1.Size.Width+label1.Width)//字幕到了最右边 i = false; if (xPox < 0-label1.Width)//字幕到了最左边 i = true; if (i) xPox+=5;//右走 else xPox-=5;//左走 } private void label1_Click(object sender, EventArgs e) { this.Close(); } } LBG.Text = LBG.Text.Substring(1) + LBG.Text[0];这个是最好用的LBG是标签不明白的找我还有啊,别忘记TMG.Stop();启动记时器 label1.Left = label1.Left - 2; if (label1.Right < 0) { label1.Left = this.Width; } 新手求指教关于抽象工厂, Winform中怎么将一个子窗体镶入到父窗体中 100分问一些简单的问题:线程安全问题控件问题 datagrid没有数据时程序crash的问题 如何在C#windows应用程序中打开一个新的网页? 请问一下直接用“=”给数组(数组的类型是一个对象)赋值和用copyto的区别是什么? 对现有PDF操作.请教高人 【c#正则】有没有比较简单通用的正则写法提取网页内信息 在winform中怎么使用js脚本 求助:AxMapXlib缺少引用 关于对象序列化压缩后追加,再反序列化时丢失对象的问题 是这样的 %L'N')'P?
private void Form1_Load(object sender, EventArgs e)
{
this.label1.Location = new Point(0, 0);
}
int xPox = 0;
bool ist = true;
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Location = new Point(xPox,this.label1.Location.Y); if (xPox == this.Width-this.label1.Size.Width)
ist = false;
if(xPox==0)
ist = true;
if (ist)
xPox++;
else
xPox--;
}
http://topic.csdn.net/u/20090606/10/5fc58b85-d448-4449-a46e-feb3fd9b0bcc.html你稍微改一下就行了
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication37
{
public partial class Form1 : Form
{
Button B = new Button();
Button B2 = new Button();
Marquee M = null; public Form1()
{
InitializeComponent(); B.Parent = this;
B.Text = "开始";
B.Click += new EventHandler(B_Click);
B2.Parent = this;
B2.Text = "停止";
B2.Click += new EventHandler(B2_Click);
B2.Location = Point.Add(B.Location, new Size(B.Width, 0));
} void B_Click(object sender, EventArgs e)
{
M = new Marquee();
M.Start(this, "ABCDEFG", Point.Add(B.Location, new Size(0, B.Height)), new Size(100, 50));
} void B2_Click(object sender, EventArgs e)
{
M.Stop();
M = null;
}
} class Marquee
{
PictureBox PB = new PictureBox();
int X = 0;
String DrawText = String.Empty;
Bitmap OrgBmp = null;
delegate void SetImage(Image NewImage); public void Start(Form Parent, String Text, Point Location, Size RectSize)
{
this.DrawText = Text;
PB.Parent = Parent;
PB.Location = Location;
PB.Size = RectSize;
X = RectSize.Width;
PB.Visible = true;
PB.Image = new Bitmap(PB.ClientRectangle.Width, PB.ClientRectangle.Height);
Graphics G = Graphics.FromImage(PB.Image);
G.FillRectangle(new SolidBrush(PB.BackColor), PB.ClientRectangle);
G.Dispose();
OrgBmp = new Bitmap(PB.Image);
Thread T = new Thread(new ThreadStart(DoDraw));
T.Start();
} public void Stop()
{
PB.Visible = false;
} void DoDraw()
{
while (PB.Visible)
{
Bitmap CacheBmp = new Bitmap(OrgBmp);
Graphics G = Graphics.FromImage(CacheBmp);
G.DrawString(DrawText, new Font("宋体", 10), new SolidBrush(Color.Black),
new PointF(X = X-- < -G.MeasureString(this.DrawText, new Font("宋体", 10)).Width ? PB.Size.Width : X, 0));
G.Dispose();
PB.Invoke(new SetImage(DoSetImage), new Object[] { CacheBmp });
Thread.Sleep(30);
}
} void DoSetImage(Image NewImage)
{
PB.Image = NewImage;
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Location = new Point(xPox,this.label1.Location.Y); if (xPox == this.Width-this.label1.Size.Width)//字幕到了最右边
ist = false;
if(xPox==0)//字幕到了最左边
ist = true;
if (ist)
xPox++;//右走
else
xPox--;//左走
}
试试这个:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 欢迎条
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Interval = 10;
this.timer1.Enabled = true;
//this.WindowState = FormWindowState.Maximized;
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Left = label1.Left - 2;
if (label1.Right<0)
{
label1.Left = this.Width;
}
} private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
{ public Form1()
{ InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) { this.timer1.Interval = 1; this.timer1.Enabled = true; //this.WindowState = FormWindowState.Maximized; } bool i = true; int xPox; private void timer1_Tick(object sender, EventArgs e) { this.label1.Location = new Point(xPox, this.label1.Location.Y); if (xPox > this.Width - this.label1.Size.Width+label1.Width)//字幕到了最右边
i = false; if (xPox < 0-label1.Width)//字幕到了最左边 i = true; if (i) xPox+=5;//右走 else xPox-=5;//左走
} private void label1_Click(object sender, EventArgs e) { this.Close(); } }
这个是最好用的
LBG是标签
不明白的找我
还有啊,别忘记TMG.Stop();启动记时器
if (label1.Right < 0)
{
label1.Left = this.Width;
}