Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / Windows Forms

RssToolbar - Desktop RSS Ticker

Rate me:
Please Sign up or sign in to vote.
3.78/5 (7 votes)
29 Jun 2009CPL2 min read 84.4K   2.4K   27  
Streams RSS headlines to the desktop
/*
 Name: RSSToolbar 
 Author: Neal T Bailey (nealbailey@hotmail.com)
 Purpose: Fetch Rss feeds
 License: Creative Commons (must reference original author)
 
 Notes: This was a .NET v1.1 project that was upgraded to .NET v2.0 
 
*/

using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Rss;

namespace RSSToolbar
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
        //Threaded Ui
        private delegate void UpdateUiCallback(string news);
        private IniDataReader _iniReader;
        private string _htmlFile;
        private int _maxHeadlines = 0;
        private int _maxStories = 0;
        
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Panel panel1;
        private Button button1;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem refreshToolStripMenuItem;
        private ToolStripMenuItem closeToolStripMenuItem;
        private ToolStripMenuItem toHTMLToolStripMenuItem;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			InitializeComponent();
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.refreshToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toHTMLToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.closeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.button1 = new System.Windows.Forms.Button();
            this.contextMenuStrip1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.White;
            this.label1.ContextMenuStrip = this.contextMenuStrip1;
            this.label1.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(40, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(0, 19);
            this.label1.TabIndex = 0;
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.refreshToolStripMenuItem,
            this.toHTMLToolStripMenuItem,
            this.closeToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(114, 70);
            // 
            // refreshToolStripMenuItem
            // 
            this.refreshToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("refreshToolStripMenuItem.Image")));
            this.refreshToolStripMenuItem.Name = "refreshToolStripMenuItem";
            this.refreshToolStripMenuItem.Size = new System.Drawing.Size(113, 22);
            this.refreshToolStripMenuItem.Text = "Refresh";
            this.refreshToolStripMenuItem.Click += new System.EventHandler(this.refreshToolStripMenuItem_Click);
            // 
            // toHTMLToolStripMenuItem
            // 
            this.toHTMLToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("toHTMLToolStripMenuItem.Image")));
            this.toHTMLToolStripMenuItem.Name = "toHTMLToolStripMenuItem";
            this.toHTMLToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.toHTMLToolStripMenuItem.Text = "Launch";
            this.toHTMLToolStripMenuItem.Click += new System.EventHandler(this.toHTMLToolStripMenuItem_Click);
            // 
            // closeToolStripMenuItem
            // 
            this.closeToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("closeToolStripMenuItem.Image")));
            this.closeToolStripMenuItem.Name = "closeToolStripMenuItem";
            this.closeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.closeToolStripMenuItem.Text = "Exit";
            this.closeToolStripMenuItem.Click += new System.EventHandler(this.closeToolStripMenuItem_Click);
            // 
            // textBox1
            // 
            this.textBox1.BackColor = System.Drawing.Color.White;
            this.textBox1.ForeColor = System.Drawing.Color.White;
            this.textBox1.Location = new System.Drawing.Point(8, 64);
            this.textBox1.MaxLength = 0;
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(296, 20);
            this.textBox1.TabIndex = 1;
            this.textBox1.Text = "textBox1";
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.White;
            this.panel1.Controls.Add(this.button1);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(656, 26);
            this.panel1.TabIndex = 2;
            // 
            // button1
            // 
            this.button1.Dock = System.Windows.Forms.DockStyle.Left;
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
            this.button1.Location = new System.Drawing.Point(0, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(25, 26);
            this.button1.TabIndex = 2;
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(656, 26);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.textBox1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "Form1";
            this.TopMost = true;
            this.TransparencyKey = System.Drawing.Color.Transparent;
            this.Load += new System.EventHandler(this.Form1_Load);
            this.contextMenuStrip1.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
            this.FormBorderStyle = FormBorderStyle.None;
            this.TransparencyKey = Color.FromArgb(0, 255, 0);
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.button1.Height = panel1.Height;
            this.label1.Height = panel1.Height;
            			
			_iniReader = new IniDataReader(Application.StartupPath + "\\settings.ini");
            _maxHeadlines = _iniReader.GetIntValue("maxheadlines");
            _maxStories = _iniReader.GetIntValue("maxstories");
            _htmlFile = String.Format("{0}\\{1}.htm", 
                Application.StartupPath, Path.GetFileName(Path.GetTempFileName()));

            foreach (string fil in Directory.GetFiles(Application.StartupPath, "*.htm", SearchOption.TopDirectoryOnly))
            {
                File.Delete(fil);
            }

            label1.Font = new Font(_iniReader.GetStringValue("fontface"), _iniReader.GetIntValue("fontsize"), FontStyle.Regular);
			this.timer1.Enabled = true;
            this.timer1.Interval = _iniReader.GetIntValue("speed");
            InitiateThread();			
		}

        private void InitiateThread()
        {
            this.textBox1.Text = "Desktop News Ticker - Now Loading Feeds....";
            Thread rssStart = new Thread(new ThreadStart(StartRssThread));
            rssStart.IsBackground = true;
            rssStart.Start();
        }

        private void UpdateUi(string news)
        {
            textBox1.Text = news;
            this.Text = "C# Rss Aggregator (Ticker) - BaileySoft 2006";
        }

		//Start Rss Parsing
		private void StartRssThread()
		{
            List<RssChannel> channels = new List<RssChannel>();
			StringBuilder mergedFeed =  new StringBuilder();
            int mh = 0;

			foreach (string rssUrl in PopulateUrls())
            {
                int ms = 0;
                if (mh < _maxHeadlines)
                {
                    RssFeed DaFeed = RssFeed.Read(rssUrl);
                    RssChannel DaChannel = (RssChannel)DaFeed.Channels[0];
                    channels.Add(DaChannel);
                    mergedFeed.AppendFormat(" {0}: ", DaChannel.Title);

                    foreach (RssItem sTrm in DaChannel.Items)
                    {
                        if (ms < _maxStories)
                        {
                            mergedFeed.AppendFormat(" {0} |", sTrm.Title);
                            ms++;
                            mh++;
                        }
                    }
                }
			}
            string dafeed = mergedFeed.ToString();
            mergedFeed = null;
            textBox1.Invoke(new UpdateUiCallback(this.UpdateUi), new string[] { dafeed });
            MakeHtml(channels);
		}

        private void MakeHtml(List<RssChannel> feeds)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<html><head></head><body style=\"margin-left: 150px;font-family: Arial, sans-serif;font-size: 10pt;\"><img src=\"baileyrss.png\" />");

            foreach (RssChannel feed in feeds)
            {
                sb.Append("<p></p>");
                sb.AppendFormat("<b>{0}:</b><p></p>", feed.Title);
                foreach (RssItem sTrm in feed.Items)
                {
                    sb.AppendFormat("<a style=\"text-decoration: none;\" href=\"{0}\">{1}</a><br />", sTrm.Link, sTrm.Title);
                }
            }
            sb.Append("</body></html>");

            StreamWriter sw = File.CreateText(_htmlFile);
            sw.Write(sb.ToString());
            sw.Flush();
            sw.Close();
        }

		private List<string> PopulateUrls()
		{
            List<string> alUrls = new List<string>();

			for (int i = 1; i <= 5; i++)
			{
				string pVal = _iniReader.GetStringValue(Convert.ToString(i));
				if (!String.IsNullOrEmpty(pVal))
				{
					alUrls.Add(pVal);
				}				
			}
			return alUrls;
		}

		//Regular Expression to Remove HTML/XML tags
		private string StripTags(string HTML)
		{
			return Regex.Replace(HTML, "<[^>]*>", "");
		}

		private void textBox1_TextChanged(object sender, System.EventArgs e)
		{
			//this.label1.
			this.label1.Text = this.textBox1.Text;
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
				this.label1.Left = this.label1.Left - 1;
				if (this.label1.Left + this.label1.Width < 0)
				{
				this.label1.Left = this.panel1.Width;
				} 
		}

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitiateThread();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(_htmlFile);
        }

        private void toHTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(_htmlFile);
        }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Software Developer
United States United States
I'm a professional .NET software developer and proud military veteran. I've been in the software business for 20+ years now and if there's one lesson I have learned over the years, its that in this industry you have to be prepared to be humbled from time to time and never stop learning!

Comments and Discussions