【问题标题】:How to implement the same effect of marquees in winform?如何在winform中实现同样的跑马灯效果?
【发布时间】:2019-01-19 21:35:32
【问题描述】:

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

在 html 中我们可以使用 Marquees "Cool Effects with Marquees!" , sample2 c# WebBrowser 控件无法识别 Marquees 的语法

c# 中的一种方法是使用列表框,然后使用计时器滚动列表框。

我想知道是否有一种简单的方法可以做到这一点。

【问题讨论】:

  • 我猜你最好的办法是使用标签并在循环或计时器内设置位置。
  • 如果你想在控件上绘制动画文本,你需要创建一个自定义控件,有一个计时器,然后移动计时器中的文本位置并使控件无效。覆盖其绘制并在新位置呈现文本。
  • something like this,控制(乘)文本字符串的度量。

标签: c# .net winforms custom-controls marquee


【解决方案1】:

如果要在控件上绘制动画文本,则需要创建一个自定义控件,具有一个计时器,然后移动计时器中的文本位置并使控件无效。覆盖其绘制并在新位置呈现文本。

您可以在我的其他答案中找到从左到右和从右到左选取框标签:Right to Left and Left to Right Marquee Label in Windows Forms

Windows 窗体选取框标签 - 垂直

在下面的示例中,我创建了一个 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);
    }
}

【讨论】:

    猜你喜欢
    • 2019-09-29
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多