C# 如何移动标签';s文本从右到左,当字符隐藏在左侧时,它显示在右侧?
这是我的关键代码:C# 如何移动标签';s文本从右到左,当字符隐藏在左侧时,它显示在右侧?,c#,label,controls,C#,Label,Controls,这是我的关键代码: using System; using System.Windows.Forms; namespace Scroller { public partial class Form1 : Form { int i, j; bool k = false; public Form1() { InitializeComponent();
using System;
using System.Windows.Forms;
namespace Scroller
{
public partial class Form1 : Form
{
int i, j;
bool k = false;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = "Time:"+ System.DateTime.Now.ToString();
i--;
j = i + this.Width;
if (i < this.Width && i > 0)
{
label1.Left = i;
}
else
if (i < 0 && k == false)
{
label1.Left = i;
k = true;
}
else
if (i < 0 && k == true)
{
label1.Left = j;
k = false;
}
if (i < 0 - label1.Width)
{
i = this.Width - label1.Width;
}
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Time:"+ System.DateTime.Now.ToString();
i = this.Width - label1.Width;
label1.Left = i;
}
}
}
使用系统;
使用System.Windows.Forms;
名称空间滚动器
{
公共部分类Form1:Form
{
int i,j;
boolk=false;
公共表格1()
{
初始化组件();
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
label1.Text=“Time:+System.DateTime.Now.ToString();
我--;
j=i+该宽度;
如果(i0)
{
标签1.左=i;
}
其他的
如果(i<0&&k==false)
{
标签1.左=i;
k=真;
}
其他的
如果(i<0&&k==true)
{
标签1.左=j;
k=假;
}
如果(i<0-标签1.宽度)
{
i=此.Width-标签1.Width;
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
label1.Text=“Time:+System.DateTime.Now.ToString();
i=此.Width-标签1.Width;
标签1.左=i;
}
}
}
我想让整个时间字符串从右向左移动。当文本的某个像素在左侧消失时(因为它超出表单的左边框),该像素将显示在右侧
换句话说,不能通过删除字符串的第一个字符并将其附加到最后一个字符来产生效果
我知道使用两个标签会更容易做到这一点。在表单中设置一个人的位置,并在表单右侧隐藏另一个人。同时移动它们
当第一个标签碰到窗体的左边框时,第二个标签碰到窗体的右边框。第一个搬出去,第二个搬进来。在第二个完全移入之前,重置其x位置
但我只想用一个标签。所以我选择快速切换标签的位置,并试图“欺骗”用户的眼睛问题是当标签在左和右之间切换时,闪烁非常明显。即使我将计时器的间隔设置为20以下,问题仍然存在
你能帮我解决闪光灯的问题吗?或者告诉我其他的方法,只要用一个标签和一个定时器就能达到我想要的效果
谢谢。如果我没有足够清楚地描述我的问题或需要更多代码,请告诉我。我认为您无法解决在windows窗体中更改标签位置时出现的闪烁问题 另一个解决方案是将标签宽度设置为与表单宽度相同的大小,使标签文本使用空格填充所有宽度,并使计时器始终获取最后一个字符并将其放在字符串的开头 下面是示例代码
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = label1.Text.Substring(label1.Text.Length - 1) + label1.Text.Remove(label1.Text.Length - 1);
}
private void Form1_Load(object sender, EventArgs e)
{
// The total spaces required to fill the form vary from form.width and the initial label.text.width
// Width | Spaces
// 177 | 13
// 228 | 30
// 297 | 53
// 318 | 60
// The spacesEnd = 60 work for a form with width 319
int spacesBegin = 0, spacesEnd = 60;
label1.Text = "Time:" + System.DateTime.Now.ToString();
label1.AutoSize = false;
label1.Left = -3;
label1.Width = this.Width - 1;
label1.Height = 15;
label1.BorderStyle = BorderStyle.FixedSingle;
for (int i = 0; i < spacesBegin; i++)
label1.Text = " " + label1.Text;
for (int i = 0; i < spacesEnd; i++)
label1.Text += " ";
Timer timer = new Timer();
timer.Tick += timer1_Tick;
timer.Interval = 50;
timer.Start();
}
private void timer1\u勾选(对象发送方,事件参数e)
{
label1.Text=label1.Text.Substring(label1.Text.Length-1)+label1.Text.Remove(label1.Text.Length-1);
}
私有void Form1\u加载(对象发送方、事件参数e)
{
//填写表单所需的总空间因form.width和初始label.text.width而异
//宽度|间距
// 177 | 13
// 228 | 30
// 297 | 53
// 318 | 60
//对于宽度为319的窗体,spacesEnd=60工作
int spacesBegin=0,spacesEnd=60;
label1.Text=“Time:+System.DateTime.Now.ToString();
label1.AutoSize=false;
标签1.左=-3;
label1.Width=this.Width-1;
标签1.高度=15;
label1.BorderStyle=BorderStyle.FixedSingle;
对于(int i=0;i
我运行了你的代码。并将标签的宽度设置为319。以下是一些问题:1.当时间文本与右侧相交时,它将粘住几毫秒。2.因为文本的最小移动量基于每个空间,并且一个空间的宽度等于几个像素。这将使文本的移动不是很平滑。3.当右侧的xx/xx:xx:xx消失时,左侧将不会显示相同的内容。有没有更好的解决办法?也许使用填充而不是空间会更好?