如何在Winform中实现字幕的相同效果? [英] How to implement the same effect of marquees in winform?
问题描述
我想使文本向上滚动或下载。
I would like to make the text scrolling upwards or downloads.
在html中,我们可以使用字幕>带字幕的酷效果! ,样本2
c# WebBrowser
控件无法识别字幕的语法
In html we can use Marquees "Cool Effects with Marquees!" , sample2
The c# WebBrowser
control doesn't recognize the syntax of Marquees
c#中的一种方法是使用列表框,然后使用计时器滚动列表框。
One way in c# is to use listbox, then rolling the listbox using timer.
我想知道是否
推荐答案
如果要在控件上绘制动画文本,则需要创建一个具有计时器的自定义控件,然后在计时器中移动文本位置并使该控件无效。
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.
您可以在我的其他答案中找到一个从左到右和从右到左的字幕标签:Windows窗体中从右到左和从左到右的选取框标签。
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.
在下面的示例中,我创建了一个 MarqueeLabel
控件,该控件可以垂直显示文本动画:
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);
}
}
这篇关于如何在Winform中实现字幕的相同效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!