riz*_*riz 1 .net c# marquee winforms
我正在尝试制作一个 RSS 新闻收报机,它将显示文本,文本需要从左到右移动
我制作了代码,文本从左向右移动,但在特定时间后它不显示全文,我将从管理面板添加更多新闻,每次我添加新闻时,文本在第一个新闻之后不会显示滚动
使用的代码
int x = -800,y=1;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
private void timer1_Tick(object sender, System.EventArgs e)
{
label1.SetBounds(x, y, 1, 1);
x++;
if(x>=800)
{
x = 4;
}
}
Run Code Online (Sandbox Code Playgroud)
读取xml的代码
private void StartRssThread()
{
List<RssChannel> channels = new List<RssChannel>();
StringBuilder mergedFeed = new StringBuilder();
int mh = 0;
int ms = 0;
if (mh < 7)
{
RssFeed DaFeed = RssFeed.Read("http://shjc.ae/rss/fileName.xml");
RssChannel DaChannel = (RssChannel)DaFeed.Channels[0];
channels.Add(DaChannel);
mergedFeed.AppendFormat(" {0}: ", DaChannel.Title);
foreach (RssItem sTrm in DaChannel.Items)
{
if (ms < 10)
{
mergedFeed.AppendFormat(" {0} |", sTrm.Title);
ms++;
mh++;
}
}
}
string dafeed = mergedFeed.ToString();
mergedFeed = null;
textBox1.Invoke(new UpdateUiCallback(this.UpdateUi), new string[] { dafeed });
}
Run Code Online (Sandbox Code Playgroud)
我已经在这篇文章中发布了一个示例,说明如何创建选取框标签以从上到下对文本进行动画处理,方法是使用自定义绘制控件的Timer重写方法: Windows 窗体从上到下选取框标签。OnPaint
在下面的代码中,我更改了该示例,以从右到左或从左到右水平设置文本动画。设置RightToLeft控件的属性足以改变方向。另外不要忘记设置AutoSize为false。
示例 - Windows 窗体中从右到左和从左到右选取框标签
using System;
using System.Drawing;
using System.Windows.Forms;
public class MarqueeLabel : Label
{
Timer timer;
public MarqueeLabel()
{
DoubleBuffered = true;
timer = new Timer() { Interval = 100 };
timer.Enabled = true;
timer.Tick += Timer_Tick;
}
int? left;
int textWidth = 0;
private void Timer_Tick(object sender, EventArgs e)
{
if (RightToLeft == RightToLeft.Yes)
{
left += 3;
if (left > Width)
left = -textWidth;
}
else
{
left -= 3;
if (left < -textWidth)
left = Width;
}
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(BackColor);
var s = TextRenderer.MeasureText(Text, Font, new Size(0, 0),
TextFormatFlags.TextBoxControl | TextFormatFlags.SingleLine);
textWidth = s.Width;
if (!left.HasValue) left = Width;
var format = TextFormatFlags.TextBoxControl | TextFormatFlags.SingleLine |
TextFormatFlags.VerticalCenter;
if (RightToLeft == RightToLeft.Yes)
{
format |= TextFormatFlags.RightToLeft;
if (!left.HasValue) left = -textWidth;
}
TextRenderer.DrawText(e.Graphics, Text, Font,
new Rectangle(left.Value, 0, textWidth, Height),
ForeColor, BackColor, format);
}
protected override void Dispose(bool disposing)
{
if (disposing)
timer.Dispose();
base.Dispose(disposing);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4398 次 |
| 最近记录: |