winform 横向与垂直的跑马灯效果的Label标签

因为群里的一个朋友在问跑马灯的弄法,看到了很多人仅仅只是用移动label的坐标的这种不太实用的方式,于是专门写了个跑马灯效果的Label,暂时只写了跑马灯文字走向4个方向。

public class MarqueeLabel : Label
{
    private float CurrentPosition { get; set; }
    private Timer Timer;
    private MarqueeDirection md = MarqueeDirection.LeftToRight;

        
    public MarqueeDirection MarqueeDirection
    {
        get { return md; }
        set {
            md = value;
            if (md == MarqueeDirection.LeftToRight)
            {
                CurrentPosition = 0;
            }
            else if (md == MarqueeDirection.RightToLeft)
            {
                CurrentPosition = Width;
            }
            else if (md == MarqueeDirection.TopToBottom)
            {
                CurrentPosition = 0;
            }
            else
            {
                CurrentPosition = Height;
            }
        }
    }

    public MarqueeLabel()
    {
        UseCompatibleTextRendering = true;
        Timer = new Timer();
        Timer.Interval = 50;
        Timer.Tick += Timer_Tick;
        Timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        Graphics gh = this.CreateGraphics();
        SizeF sf = gh.MeasureString(this.Text, this.Font);

        if (md == MarqueeDirection.LeftToRight)
        {
            if (CurrentPosition > Width)
                CurrentPosition = -sf.Width;
            else
                CurrentPosition += 2;
        }
        else if (md == MarqueeDirection.RightToLeft)
        {
            if (CurrentPosition > -sf.Width)
                CurrentPosition -= 2;
            else
                CurrentPosition = Width;
        }
        else if (md == MarqueeDirection.TopToBottom)
        {
            if (CurrentPosition > Height)
                CurrentPosition = -sf.Height;
            else
                CurrentPosition += 2;
        }
        else
        {
            if (CurrentPosition > -sf.Height)
                CurrentPosition -= 2;
            else
                CurrentPosition = Height;
        }
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (md == MarqueeDirection.LeftToRight || md == MarqueeDirection.RightToLeft)
        {
            e.Graphics.TranslateTransform((float)CurrentPosition, 0);
        }
        else
        {
            e.Graphics.TranslateTransform(0, (float)CurrentPosition);
        }
        base.OnPaint(e);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (Timer != null)
                Timer.Dispose();
        }
        Timer = null;
    }
}

public enum MarqueeDirection : int
{
    LeftToRight = 0,
    RightToLeft = 1,
    TopToBottom = 2,
    BottomToTop = 3
}

运行基本OK,可能存在一点小瑕疵,若文字太多而Label高度太小会导致显示不全。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值