Market Ticker Forums
Detailed market commentary at The Market Ticker and Ticker Classics (The Year 2012 In Review)
Donations accepted; we offer GOLD ACCESS for enhanced privileges. T-Shirts, caps, coffee mugs? Click here.
BlogTalkRadio - Mondays at 3:30 Central - Yes, TickerGuy has a radio show (kinda)
Rss Icon RSS available You are not signed on; if you are a visitor please register for a free account!
Sponsored Advertising
To remove advertising from your display upgrade to Gold Donor status
MarketTicker Forums Single Post Display (Show in context)
User: Not logged on
Top Login Control Panel FAQ Register Logout
User Info TickerCon; entered at 2011-04-16 17:57:24
Drr
Posts: 11
Registered: 2009-09-30
Well here's a quick version of the tray icon that was requested a while back. Anyone with a recent version of Windows (including XP) and the .NET Framework v3.5 or v4.0 installed (including most users since it's pushed via Windows Update) should be able to compile/run this.

Instructions:
1. Copy/paste the below code into a plain text file (using Notepad or whatever) and save to your hard drive. By convention it should have a .cs file extension but that is not strictly required. Note that Notepad will append a .txt file extension unless you use quotation marks around the filename when saving.
2. Open a command prompt: hit [Windows]+[R] and type "cmd" without the quotation marks.
3. Copy/paste the below command text into the prompt window (using [Ctrl]+[V] will not work--you need to use the right-click menu to paste), substituting the path and filename where indicated.
4. You should get output that says "Microsoft (R) Visual C# Compiler..." and no error messages.
5. You should find a .exe file dropped into the current directory of the command prompt. Copy it wherever, maybe link it into your Startup menu, have fun waiting for the nuke.

Note that I did not include any error-handling logic, so if you lose network connectivity or KD changes the URL format, the program will just crash. Use at your own risk. :)

EDIT: I found out that the forum does not like backslashes. You will need to replace any dollar signs ("$") with backslashes after copy/pasting. There are several in the command text and two in the Regex in the code. I also added unnecessary string concatenations to avoid the URLs becoming hyperlinks.

Command prompt wrote..
%SystemRoot%$Microsoft.NET$Framework$v3.5$CSC.exe /reference:System.dll /reference:System.Drawing.dll /reference:System.Windows.Forms.dll /target:winexe "C:$Path_to_saved_code_file$Saved_file_name.cs"


Quote:

namespace DRR.TickerCon
{
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;

/// <summary>The only class.</summary>
/// <revision date="2011-04-16" author="DRR">Initial version.</revision>
internal static class Program
{
#region Configuration

/// <summary>The HTML page to scrape to determine the current status.</summary>
private readonly static Uri _page =
new Uri("http" + "://tickerforum.org/akcs-www");

/// <summary>The format of TickerCon on-image URLs.</summary>
private readonly static Regex _imageUriFormat =
new Regex("http" + "://tickerforum.org/Defcon/($$d)-(on|bang).png");

/// <summary>Refresh hourly.</summary>
private const int _refreshIntervalInMilliseconds = 60 * 60 * 1000;

#endregion
#region State

/// <summary>The tray icon refresh timer.</summary>
private readonly static Timer _refreshTimer =
new Timer() { Interval = _refreshIntervalInMilliseconds };

/// <summary>The tray icon.</summary>
private readonly static NotifyIcon _trayIcon = new NotifyIcon
{ ContextMenu = new ContextMenu(new[] { new MenuItem("Exit", Exit_Click) }) };

#endregion
#region Logic

/// <summary>The program entry point.</summary>
[STAThread]
private static void Main()
{
// Use a Windows message timer to periodically update the tray icon.
_refreshTimer.Tick += (sender, e) => { UpdateTrayIcon(); };
_refreshTimer.Start();

// Initialize the icon.
UpdateTrayIcon();

// Start the message pump.
Application.Run();
}

/// <summary>Updates the TickerCon tray icon.</summary>
private static void UpdateTrayIcon()
{
_trayIcon.Icon = DownloadIcon(FindCurrentTickerConUri());
_trayIcon.Visible = true;
}

/// <summary>Scrapes an HTML page to find the current on-image.</summary>
/// <returns>An image URL.</returns>
private static Uri FindCurrentTickerConUri()
{
using (var request = new WebClient())
{
var match = _imageUriFormat.Match(request.DownloadString(_page));
if (!match.Success)
throw new InvalidOperationException
("No TickerCon icon was found at the specified URL.");
return new Uri(match.Value);
}
}

/// <summary>Downloads an image and converts it to an icon.</summary>
/// <param name="uri">The image to download.</param>
/// <returns>An icon.</returns>
private static Icon DownloadIcon(Uri uri)
{
if (uri == null) throw new ArgumentNullException("uri");

/* TODO: Is there a more direct way to download an image
* and convert it to an icon?
*/
using (var request = new WebClient())
using (var buffer = new MemoryStream(request.DownloadData(uri)))
using (var image = Image.FromStream(buffer))
using (var bitmap = new Bitmap(image))
return Icon.FromHandle(bitmap.GetHicon());
}

/// <summary>Handles the Exit menu item to terminate the program.</summary>
/// <param name="sender">Ignored.</param>
/// <param name="e">Ignored.</param>
private static void Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}

#endregion
}
}


Last modified: 2011-04-16 18:04:43 by drr
Reason: Backslashes were removed and URLs were truncated.

2011-04-16 17:57:24