标签超出面板宽度后如何使其重新出现 [英] How to make label reappear as soon as it's going out of panel width

查看:31
本文介绍了标签超出面板宽度后如何使其重新出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个移动的标签看起来更好、更流畅,而不是在它全部超出面板宽度后重新出现在左边.例如标签 'Hello' ,只要 'lo' 越界在右边我希望它重新出现在左边.有什么可能的解决方案吗?

I want to make a moving label seem nicer and smoother than just reappearing the whole thing to the left after it has all gone out of panel width .For example label 'Hello' , as soon as 'lo' goes out of bounds in the right I want it to reappear on the left. Is there any possible solution to this ?

这是我现在的标签代码.

Here's the code I have for the label now .

private void timer2_Tick(object sender, EventArgs e)
{          
    label5.Location = new Point(label5.Location.X + 3, label5.Location.Y);
    if (label5.Location.X > this.Width)
    {
        label5.Location = new Point(0 - label5.Width, label5.Location.Y);
    }
}

推荐答案

试试这个,使用一个标签(这里,命名为 lblMarquee 和一个 System.Windows.Forms.Timer>).

Try this, using a Label (here, named lblMarquee and a System.Windows.Forms.Timer).

滚动时间由 Timer.Interval 和浮点字段 (marqueeStep) 调节.
Timer.Tick 事件仅调用 lblMarquee.Invalidate(),导致 Label 控件重新绘制自身.

The scrolling time is regulated by both the Timer.Interval and a float Field (marqueeStep).
The Timer.Tick event just calls lblMarquee.Invalidate(), causing the Label control to repaint itself.

当滚动文本相对于其当前位置超出了 Label.ClientRectangle 的限制时,不再可见的文本部分将被绘制在 的开头>Label.ClientArea:

When the scrolling text, in relation to its current position, goes beyond the limits of the Label.ClientRectangle, the section of the text which is not visible anymore is painted at start of the Label.ClientArea:

System.Windows.Forms.Timer marqueeTimer = new System.Windows.Forms.Timer();
string marqueeText = string.Empty;
float marqueePosition = 0f;
float marqueeStep = 4f;

private void form1_Load(object sender, EventArgs e)
{
    marqueeText = lblMarquee.Text;
    lblMarquee.Text = string.Empty;
    marqueeTimer.Tick += (s, ev) => { this.lblMarquee.Invalidate(); };
    marqueeTimer.Interval = 100;
    marqueeTimer.Start();
}

private void lblMarquee_Paint(object sender, PaintEventArgs e)
{
    var marquee = sender as Label;
    SizeF stringSize = e.Graphics.MeasureString(marqueeText, marquee.Font, -1, marqueeFormat);
    PointF stringLocation = new PointF(marqueePosition, (marquee.Height - stringSize.Height) / 2);
    stringLength = marquee.ClientRectangle.Width - stringLocation.X;

    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
    e.Graphics.DrawString(marqueeText, marquee.Font, Brushes.Black, stringLocation, marqueeFormat);
    if (marqueePosition >= marquee.ClientRectangle.Width) marqueePosition = 0f;

    if (stringSize.Width + stringLocation.X > marquee.ClientRectangle.Width) {
        PointF partialStringPos = new PointF(-stringLength, (marquee.Height - stringSize.Height) / 2);
        e.Graphics.DrawString(marqueeText, marquee.Font, Brushes.Black, partialStringPos, marqueeFormat);
    }
    marqueePosition += marqueeStep;
}

您可能会发现其他一些有用的实现:

A couple of other implementations you might find useful:

如何在没有 NoWrap 的 TextBox 中跟踪文本的结尾
如何在两个不相邻的区域绘制字符串

这篇关于标签超出面板宽度后如何使其重新出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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