Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在winform中实现字幕的相同效果?_C#_.net_Winforms_Custom Controls_Marquee - Fatal编程技术网

C# 如何在winform中实现字幕的相同效果?

C# 如何在winform中实现字幕的相同效果?,c#,.net,winforms,custom-controls,marquee,C#,.net,Winforms,Custom Controls,Marquee,我想让文本向上滚动或下载 在html中,我们可以使用字幕, c#WebBrowser控件无法识别字幕的语法 c#中的一种方法是使用listbox,然后使用计时器滚动listbox 我想知道是否有一种简单的方法可以做到这一点。如果要在控件上绘制动画文本,需要创建一个自定义控件,带有计时器,然后移动计时器中的文本位置并使控件无效。替代其绘制并在新位置渲染文本 您可以在我的另一个答案中找到从左到右和从右到左的选框标签: Windows窗体选框标签-垂直 在下面的示例中,我创建了一个MarqueeLab

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

在html中,我们可以使用字幕, c#
WebBrowser
控件无法识别字幕的语法

c#中的一种方法是使用listbox,然后使用计时器滚动listbox


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

如果要在控件上绘制动画文本,需要创建一个自定义控件,带有计时器,然后移动计时器中的文本位置并使控件无效。替代其绘制并在新位置渲染文本

您可以在我的另一个答案中找到从左到右和从右到左的选框标签:

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);
    }
}
使用系统;
使用系统图;
使用System.Windows.Forms;
公共类MarqueeLabel:标签
{
定时器;
公共选框标签()
{
双缓冲=真;
定时器=新定时器();
计时器。间隔=100;
timer.Enabled=true;
timer.Tick+=定时器_Tick;
}
int?top;
int textHeight=0;
私有无效计时器(对象发送方、事件参数)
{
top-=3;
如果(顶部<-文本高度)
顶部=高度;
使无效();
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
e、 图形。清晰(背景色);
var s=TextRenderer.MeasureText(文本、字体、新大小(宽度,0),
TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak);
text高度=s.高度;
如果(!top.HasValue)top=高度;
TextRenderer.DrawText(例如图形、文本、字体、,
新矩形(0,顶部值,宽度,文本高度),
前景色、背景色、TextFormatFlags.TextBoxControl|
TextFormatFlags.WordBreak);
}
受保护的覆盖无效处置(布尔处置)
{
如果(处置)
timer.Dispose();
基地。处置(处置);
}
}

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