How to implement the same effect of marquees in winform?

后端 未结 1 960

I would like to make the text scrolling upwards or downloads.

In html we can use Marquees \"Cool Effects with Marquees!\" , sample2 The c# WebBrowser contro

1条回答
  •  鱼传尺愫
    2021-01-25 18:10

    If you want to draw animated text on a control, you need to create a custom control, having a timer, then move the text location in the timer and invalidate the control. Override its paint and render the text in new location.

    You can find a Left to Right and Right to Left Marquee Label in my other answer here: Right to Left and Left to Right Marquee Label in Windows Forms.

    Windows Forms Marquee Label - Vertical

    In the following example, I've created a MarqueeLabel control which animates the text vertically:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    public class MarqueeLabel : Label
    {
        Timer timer;
        public MarqueeLabel()
        {
            DoubleBuffered = true;
            timer = new Timer();
            timer.Interval = 100;
            timer.Enabled = true;
            timer.Tick += Timer_Tick;
        }
        int? top;
        int textHeight = 0;
        private void Timer_Tick(object sender, EventArgs e)
        {
            top -= 3;
            if (top < -textHeight)
                top = Height;
            Invalidate();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.Clear(BackColor);
            var s = TextRenderer.MeasureText(Text, Font, new Size(Width, 0),
                TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak);
            textHeight = s.Height;
            if (!top.HasValue) top = Height;
            TextRenderer.DrawText(e.Graphics, Text, Font,
                new Rectangle(0, top.Value, Width, textHeight),
                ForeColor, BackColor, TextFormatFlags.TextBoxControl |
                TextFormatFlags.WordBreak);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
                timer.Dispose();
            base.Dispose(disposing);
        }
    }
    

    0 讨论(0)
提交回复
热议问题