【问题标题】:News ticker with moving text from left to right带有从左到右移动文本的新闻自动收报机
【发布时间】:2019-11-25 10:24:36
【问题描述】:

我正在尝试制作一个显示文本的 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;
            }

        }

读取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 });

        }

【问题讨论】:

  • 问题标有“asp.net”和“winforms”标签,没有多大意义。您是在开发 Web 应用程序 (asp.net) 还是桌面应用程序 (winforms)?

标签: c# .net winforms marquee


【解决方案1】:

Windows 窗体选取框标签 - 水平

我已经发布了一个示例,说明如何通过使用Timer 并在这篇文章中覆盖自定义绘制控件的OnPaint 方法来创建一个选取框标签以从上到下为文本设置动画:Windows Forms Top to Bottom Marquee Label

在下面的代码中,我已将该示例更改为从右到左或从左到右水平设置文本动画。设置控件的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);
    }
}

【讨论】:

  • 可以开始了。稍后您可以通过添加一些属性来扩展控件,以设置移动步长(而不是 +=3 或 -=3)或移动间隔(而不是 100 毫秒)或重置文本更改时的动画和其他一些属性。
  • 以防万一你想扩展控件,你可以强制Label 设置AutoSize = false 当你在窗体上放置一个控件的实例。为此,请查看this post
【解决方案2】:

您正在为 x 的起始值和最大值使用硬编码值。根据您的问题,我认为标签中的文本具有动态长度(正确吗?)。如果文本是动态长度的,那么 x 的值也应该是动态的。

此外,x 从 -800 开始。然后慢慢增长到 800,然后设置为 4。 这对我来说似乎很奇怪,如果第一次运行从 -800 开始,那么第二次运行可能也需要从 -800 开始。

希望这对您有所帮助。如果不是,请提供更多详细信息(例如您选择 -800、800 和 4 的原因)。

【讨论】:

  • 你是对的,我怎样才能得到我从后端添加的文本的长度
  • @riz 我认为TextRenderer.MeasureText( text, textBox1.Font ); 应该这样做。不确定,但您可以查看该函数以获取更多信息
  • 可以添加不同的图片吗?我该怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
相关资源
最近更新 更多