如何实现在winform跑马灯同样的效果?

问题描述 投票:1回答:1

我想使文本滚动向上或下载。

在HTML中,我们可以用帐篷"Cool Effects with Marquees!"sample2 C#的WebBrowser控制不承认选取框的语法

在C#中的一种方式是使用列表框,然后滚动使用定时器列表框。

我想知道如果有一个简单的方法来做到这一点。

c# .net winforms custom-controls marquee
1个回答
0
投票

如果你想画上一个控制动画文字,你需要创建一个自定义的控制,有一个定时器,然后在定时器移动文本位置和无效的控制。覆盖其paint并呈现在新位置的文本。

在下面的例子中,我创建了垂直动画中的文本MarqueeLabel控制:

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.