如何实现Label文字从右至左不间断滚动,不间断!!!解决即给分!!

luoyong269 2010-09-23 02:49:44
小弟通过如下代码实现了Label文字从右至左滚动,但这种滚动是要等Label完全消失在左边时,再从右边出来,
现想实现不间断的滚动,就是说,Label第一个字进入最左边时,从最右边显示进入最左边的字,实现不间断,完整显示!

请高手指点,给小弟一个思路或代码,谢稿

int ScreenWidth = SystemInformation.PrimaryMonitorMaximizedWindowSize.Width;
int ScreenHeight = SystemInformation.PrimaryMonitorMaximizedWindowSize.Height;
Point MyPos = new Point(this.label1.Location.X, this.label1.Location.Y);
if (MyPos.X + Width + this.label1.Width >= ScreenWidth)
{
this.label1.Location = new Point(MyPos.X - 20, 0);
return;
}
else
{
this.label1.Location = new Point(this.Width, 0);
}
...全文
698 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxiju203 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 benbenrh 的回复:]
引用 9 楼 xxiju203 的回复:
我的方法不会卡 ,试试就知道啦,我也曾经尝试过用GDI画,画面闪烁不说,资源占用太高了.


GDI闪烁,那时因为你可以用缓冲阿,先画在内存的一块画布上,然后再 画到界面上来。那就不会闪烁了。
[/Quote]
双缓冲CPU资源占用太多了.同时显示几个字幕还好,多了就不爽了@
倒霉熊 2010-09-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wcl1988 的回复:]
做两个文本内容一样的Label1(左)、Label2(右),他们之间的X轴坐标相差 = 屏幕宽度;
当Label1(左)完全消失在左边时,this.Label1(左).Location = new Point(this.Width, 0);
当Label2(右)完全消失在左边时,this.Label2(右).Location = new Point(this.Width, 0);
[/Quote]

我做过上下滚动的,原理就是用两个label。楼上这个的方法不错。
dylike 2010-09-24
  • 打赏
  • 举报
回复
完整的吧.由于我不懂C#,用VB.NET写的.直接把代码翻译过来的.我可以直接运行,如果C#中提供少什么的话建议自行添加修改.
luoyong269 2010-09-24
  • 打赏
  • 举报
回复
楼上的大哥,你的代码不完整,在C#里运行时缺少很多定义如this.CreateGraphics, _tmpBP,
luoyong269 2010-09-24
  • 打赏
  • 举报
回复
太谢谢各位大哥了,我测试一下效果,马上结贴
dylike 2010-09-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xxiju203 的回复:]
我的方法不会卡 ,试试就知道啦,我也曾经尝试过用GDI画,画面闪烁不说,资源占用太高了.
[/Quote]

以上代码将会自动清理,不会占用太多资源,由于并非移动控件本身,所以也不会导致因界面变化而整个界面所有控件的更新而导致的拖慢界面响应速度.
dylike 2010-09-24
  • 打赏
  • 举报
回复
补充:
将以上代码中:
               BP = new Bitmap(_w + 100, _h);
//此处的100为两个首尾相接的字幕间距

改成:
               BP = new Bitmap(_w + (Lb.Width-100), _h);

这样可以自适应Label宽度,其中的100可适当调大,此时成为视觉感观的偏移适应量,可自行测试调整
dylike 2010-09-24
  • 打赏
  • 举报
回复
看了,感觉有点意思,给你写了一个

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
//code by dylike
private string ADText = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private int _ScrollLeft = 0;
private Bitmap _textbmp;
//本代码可以保证滚动流畅并且不闪画面,如需最佳效果,建议:Timer1.Interval=10
//界面上放一Label控件,Autosize=fase,手动调整需要的宽度,高度将由代码控制,无需关注
private void Timer1_Tick(System.Object sender, System.EventArgs e)
{
//Try
using (_tmpBP == new Bitmap(Label1.Width, Label1.Height)) {
using (Graphics G = Graphics.FromImage(_tmpBP)) {
if (_textbmp != null) {
_ScrollLeft = (_ScrollLeft <= -_textbmp.Width * 2 ? Label1.Width : _ScrollLeft - 1);
// _ScrollLeft - 1中的1为每次移动速度,此处为每次移动1个像素
using (System.Drawing.TextureBrush imgBrs = new System.Drawing.TextureBrush(_textbmp)) {
imgBrs.WrapMode = Drawing2D.WrapMode.Tile;
imgBrs.TranslateTransform(_ScrollLeft, 0);

G.FillRectangle(imgBrs, new Rectangle(0, 0, Label1.Width, Label1.Height));
}
}
}
if (_tmpBP != null) {
if (Label1.BackgroundImage != null)
Label1.BackgroundImage = null;
Label1.BackgroundImage = new Bitmap(_tmpBP);
}
}
//Catch
//End Try
}
private Bitmap _Gettextbitmap(Label Lb, string ScrollText)
{
try {
this.DoubleBuffered = true;
Bitmap BP = default(Bitmap);
SizeF _Sz = default(SizeF);
using (SolidBrush Brs = new SolidBrush(Lb.ForeColor)) {
using (StringFormat _sf = new StringFormat()) {
_sf.Alignment = StringAlignment.Center;
_sf.LineAlignment = StringAlignment.Near;
using (Graphics G = this.CreateGraphics) {
_Sz = G.MeasureString(ScrollText, Lb.Font, new Size(99999, 99999), _sf);
}
}
_Sz = new SizeF(_Sz.Width + 1, _Sz.Height + 1);
int _w = 0;
int _h = 0;
Panel1.Size = _Sz.ToSize;
_w = _Sz.Width;
_h = _Sz.Height;
BP = new Bitmap(_w + 100, _h);
//此处的100为两个首尾相接的字幕间距
using (Graphics G2 = Graphics.FromImage(BP)) {
G2.DrawString(ScrollText, Lb.Font, Brs, 0, 0);
}
}
return BP;
} catch {
return null;
}
}

private void Form1_Shown(object sender, System.EventArgs e)
{
//注意,写在此方法内是为了确保Label1控件已经创建
_textbmp = _Gettextbitmap(Label1, ADText);
Label1.BackgroundImageLayout = ImageLayout.None;
Label1.Text = "";
Label1.AutoSize = false;
Label1.Height = _textbmp.Height;
}
public Form1()
{
Shown += Form1_Shown;
}
}
ysz89757 2010-09-24
  • 打赏
  • 举报
回复
学习啦
Nick黄 2010-09-23
  • 打赏
  • 举报
回复
见识了、谢谢LZ的问题
wuyq11 2010-09-23
  • 打赏
  • 举报
回复
private void Form1_Load(object sender, EventArgs e)
{
this.label1.Location = new Point(0, 0);
}
int x= 0;
bool ist = true;
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Location = new Point(xPox,this.label1.Location.Y);
if (x== this.Width-this.label1.Size.Width)
ist = false;
if(x==0)
ist = true;
if (ist)
x++;
else
x--;
}
http://topic.csdn.net/u/20090606/10/5fc58b85-d448-4449-a46e-feb3fd9b0bcc.html
娃ha哈 2010-09-23
  • 打赏
  • 举报
回复
??
timer 不行吗?
benbenRH 2010-09-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xxiju203 的回复:]
我的方法不会卡 ,试试就知道啦,我也曾经尝试过用GDI画,画面闪烁不说,资源占用太高了.
[/Quote]

GDI闪烁,那时因为你可以用缓冲阿,先画在内存的一块画布上,然后再 画到界面上来。那就不会闪烁了。
xxiju203 2010-09-23
  • 打赏
  • 举报
回复
我的方法不会卡 ,试试就知道啦,我也曾经尝试过用GDI画,画面闪烁不说,资源占用太高了.
sz709 2010-09-23
  • 打赏
  • 举报
回复
修改你的代码,增加Label2,把它的Location放到与Label差ScreenWidth的地方,再一起向左滚动

this.label1.Text ="AA";

//增加如下
this.label2.Text ="AA";
this.label2.Location = new Point(this.label1.Location.X + Width, 0);

int ScreenWidth = SystemInformation.PrimaryMonitorMaximizedWindowSize.Width;
int ScreenHeight = SystemInformation.PrimaryMonitorMaximizedWindowSize.Height;
Point MyPos = new Point(this.label1.Location.X, this.label1.Location.Y);

//增加如下
Point MyPos2 = new Point(this.label2.Location.X, this.label2.Location.Y);

if (MyPos.X + Width + this.label1.Width >= ScreenWidth)
{

this.label1.Location = new Point(MyPos.X - 20, 0);
//增加如下
this.label2.Location = new Point(MyPos2.X - 20, 0);
return;
}
else
{
//修改如下
this.label1.Location = new Point(this.Width-this.label1.Width-40, 0);
//增加如下
this.label2.Location = new Point(this.label1.Location.X + Width, 0);
}
benbenRH 2010-09-23
  • 打赏
  • 举报
回复
很简单的,你BAIDU 一下吧!
benbenRH 2010-09-23
  • 打赏
  • 举报
回复

Graphic g=this.createGraphic();
g.drawString("Hello",this.font, new Point(50,50));

g.dispose();
luoyong269 2010-09-23
  • 打赏
  • 举报
回复
有没有详细的GDI写法,小弟不太会,还望大哥仔细指导,谢谢
xxiju203 2010-09-23
  • 打赏
  • 举报
回复
你要做字幕对吧,给你一个我做的.
        Thread th = null;
private void frm_Sub_Load(object sender, EventArgs e)
{
this.Width = Screen.PrimaryScreen.Bounds.Width;
Random r = new Random();
this.Location = new Point(0,r.Next(0,Screen.PrimaryScreen.Bounds.Height-this.Height));
this.label1.Width = this.Width;
label1.Height = this.Height-10;
label1.Location = new Point(this.Width, 5);
label1.Top = 0;
label1.Font = new Font(FontFamily.GenericSansSerif, (float)(((this.Height - 10) * 72) / 96));
label1.Text = "你好啊~~~~~~~~~~~abc ABC";
th = new Thread(new ThreadStart(runSub));
th.IsBackground = true;
th.Start();
}
private void runSub()
{
while (true)
{
if (this.InvokeRequired)
{
try
{
this.Invoke(new subTitle(sub));
}
catch { }
}
Thread.Sleep(10);
}
}
private delegate void subTitle();
private void sub()
{
if (label1.Left > -label1.Width)
{
label1.Left -=1;
}
else
{
label1.Left = this.Width;
}
}
wulala789 2010-09-23
  • 打赏
  • 举报
回复
不要去移location这样会一卡一卡,你应该用drawtext的GDI方法把文字画出来,非常的简单,试试就会
加载更多回复(2)

110,539

社区成员

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

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

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