小弟通过如下代码实现了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);
}
现想实现不间断的滚动,就是说,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);
}
当Label1(左)完全消失在左边时,this.Label1(左).Location = new Point(this.Width, 0);
当Label2(右)完全消失在左边时,this.Label2(右).Location = new Point(this.Width, 0);
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;
}
}
Graphic g=this.createGraphic();
g.drawString("Hello",this.font, new Point(50,50));g.dispose();
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);
}
GDI闪烁,那时因为你可以用缓冲阿,先画在内存的一块画布上,然后再 画到界面上来。那就不会闪烁了。
{
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
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;
}
}
将以上代码中:
BP = new Bitmap(_w + 100, _h);
//此处的100为两个首尾相接的字幕间距改成:
BP = new Bitmap(_w + (Lb.Width-100), _h);
这样可以自适应Label宽度,其中的100可适当调大,此时成为视觉感观的偏移适应量,可自行测试调整
我做过上下滚动的,原理就是用两个label。楼上这个的方法不错。