C#中的滚动标签 [英] Scrolling Label in C#

查看:113
本文介绍了C#中的滚动标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种有效的方式来滚动文本,例如网络术语中的选取框.

I am looking for an efficient way of scrolling text like a marquee in web terminology.

我设法通过使用在网上找到的一段代码来实现这一目标:

I managed to achieve this using a piece of code I found online:

private int xPos = 0, YPos = 0;

private void Form1_Load(object sender, EventArgs e)
{
    //link label
    lblText.Text = "Hello this is marquee text";
    xPos = lblText.Location.X;
    YPos = lblText.Location.Y;
    timer1.Start();
}


private void timer1_Tick(object sender, EventArgs e)
{
    if (xPos == 0)
    {

        this.lblText.Location = new System.Drawing.Point(this.Width, YPos);
        xPos = this.Width;
    }
    else
    {
        this.lblText.Location = new System.Drawing.Point(xPos, YPos);
        xPos -= 2;
    }
}

该代码非常简单,它使用了一个计时器滴答事件.

The code is very simple and it uses, a timer tick event.

最初效果不错,但滚动3或4次后,它不再出现.

It works great initially, but after scrolling 3 or 4 times, it does not reappear.

有什么我可以调整以使滚动无限的吗?

Is there anything I can adjust to make the scrolling infinite?

推荐答案

尝试:

private void timer1_Tick(object sender, EventArgs e)
{
    if (xPos <= 0) xPos = this.Width;
    this.lblText.Location = new System.Drawing.Point(xPos, YPos);
    xPos -= 2;
}

您提到如果字符串长于表单宽度,它将切断".我认为您的意思是,标签一旦碰到左侧,便会跳回到表格的右侧,这意味着您无法阅读全文?

You mentioned that it 'cuts off' if the string is longer than the form width. I assume you mean that the label jumps back to the right side of the form as soon as it hits the left side, which means you can't read the full text?

如果是这样,则可以将Label的最小左侧"设置为其宽度的负数.这将允许标签在重置之前完全滚动离开表单:

If so, you could set the 'minimum Left' of the Label to be the negative of it's width. This would allow the label to scroll fully off the form before resetting it:

private void timer1_Tick(object sender, EventArgs e)
{
    // Let the label scroll all the way off the form
    int minLeft = this.lblText.Width * -1;

    if (xPos <= minLeft) xPos = this.Width;
    this.lblText.Location = new Point(xPos, yPos);
    xPos -= 2;
}

或者,您可以将'minimum Left'设置为标签宽度和表格宽度之间的差的负数,以便在显示最右边的字符之前不会重置它:

Or, you can set the 'minimum Left' to be the negative of the difference between the label width and the form width, so that it would not reset until the rightmost characters have been shown:

private void timer1_Tick(object sender, EventArgs e)
{
    // Ensure that the label doesn't reset until you can read the whole thing:
    int minLeft = (lblText.Width > this.Width) ? this.Width - lblText.Width : 0;

    if (xPos <= minLeft) xPos = this.Width;
    this.lblText.Location = new Point(xPos, yPos);
    xPos -= 2;
}

许多其他选项.就像有多个标签轮流背靠背旋转一样,所以永远不会有任何空白文本!您可以找出要动态生成多少个标签(基于标签宽度和表单宽度之间的差异)并处理它们在Timer事件中的位置.

Many other options, too. Like having multiple labels running back to back in rotation, so there is never any blank text!! You can figure out how many labels to generate dynamically (based on the difference between their width and the form's width) and handle their positions in the Timer event.

这篇关于C#中的滚动标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆