Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for systemtraymessage.cs

(view source code of systemtraymessage.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9.  
  10.  
  11. namespace RobvanderWoude
  12. {
  13. 	class SystemTrayMessage
  14. 	{
  15. 		static string progver = "1.04";
  16.  
  17.  
  18. 		static int Main( string[] args )
  19. 		{
  20. 			#region Initialize Variables
  21.  
  22. 			bool escapemessage = true;
  23. 			bool wait = false;
  24. 			int timeout = 10000;
  25. 			int iconindex = 277;
  26. 			string[] searchpath = Environment.GetEnvironmentVariable( "PATH" ).Split( new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries );
  27. 			string iconfile = "shell32.dll";
  28. 			string iconext = ".dll";
  29. 			Icon systrayicon = IconExtractor.Extract( iconfile, iconindex, true );
  30. 			ToolTipIcon tooltipicon = ToolTipIcon.Info;
  31. 			string message = String.Empty;
  32. 			string title = String.Empty;
  33.  
  34. 			#endregion Initialize Variables
  35.  
  36.  
  37. 			#region Parse Command Line
  38.  
  39. 			if ( args.Length == 0 )
  40. 			{
  41. 				return ShowHelp( );
  42. 			}
  43. 			foreach ( string arg in args )
  44. 			{
  45. 				if ( arg == "/?" )
  46. 				{
  47. 					return ShowHelp( );
  48. 				}
  49. 				if ( arg.ToUpper( ) == "/W" )
  50. 				{
  51. 					if ( wait )
  52. 					{
  53. 						return ShowHelp( "Duplicate command line switch /W" );
  54. 					}
  55. 					wait = true;
  56. 				}
  57. 				else if ( arg.ToUpper( ) == "/L" )
  58. 				{
  59. 					if ( !escapemessage )
  60. 					{
  61. 						return ShowHelp( "Duplicate command line switch /NE" );
  62. 					}
  63. 					escapemessage = false;
  64. 				}
  65. 				else if ( arg[0] == '/' )
  66. 				{
  67. 					if ( arg.Length > 3 && arg[2] == ':' )
  68. 					{
  69. 						switch ( arg[1].ToString( ).ToUpper( ) )
  70. 						{
  71. 							case "I":
  72. 								if ( tooltipicon != ToolTipIcon.Info )
  73. 								{
  74. 									return ShowHelp( "Duplicate command line switch /I" );
  75. 								}
  76. 								switch ( arg.Substring( 3 ).ToUpper( ) )
  77. 								{
  78. 									case "ERROR":
  79. 										tooltipicon = ToolTipIcon.Error;
  80. 										break;
  81. 									case "INFO":
  82. 										tooltipicon = ToolTipIcon.Info;
  83. 										break;
  84. 									case "NONE":
  85. 										tooltipicon = ToolTipIcon.None;
  86. 										break;
  87. 									case "WARNING":
  88. 										tooltipicon = ToolTipIcon.Warning;
  89. 										break;
  90. 									default:
  91. 										return ShowHelp( "Invalid tooltip icon type \"{0}\"", arg.Substring( 3 ) );
  92. 								}
  93. 								break;
  94. 							case "P":
  95. 								if ( iconfile.ToUpper( ) != "SHELL32.DLL" )
  96. 								{
  97. 									return ShowHelp( "Duplicate command line switch /P" );
  98. 								}
  99. 								iconext = Path.GetExtension( arg.Substring( 3 ) ).ToLower( );
  100. 								if ( File.Exists( arg.Substring( 3 ) ) )
  101. 								{
  102. 									iconfile = Path.GetFullPath( arg.Substring( 3 ) );
  103. 								}
  104. 								else if ( iconext == ".dll" || iconext == ".exe" )
  105. 								{
  106. 									foreach ( string folder in searchpath )
  107. 									{
  108. 										if ( File.Exists( Path.Combine( folder, arg.Substring( 3 ) ) ) )
  109. 										{
  110. 											iconfile = Path.Combine( folder, arg.Substring( 3 ) );
  111. 											break;
  112. 										}
  113. 									}
  114. 									if ( !File.Exists( iconfile ) )
  115. 									{
  116. 										return ShowHelp( "Invalid path to icon file or library \"{0}\"", arg.Substring( 3 ) );
  117. 									}
  118. 								}
  119. 								break;
  120. 							case "S":
  121. 								if ( iconindex != 277 )
  122. 								{
  123. 									return ShowHelp( "Duplicate command line switch /S" );
  124. 								}
  125. 								try
  126. 								{
  127. 									iconindex = Convert.ToInt32( arg.Substring( 3 ) );
  128. 									if ( iconindex < 0 )
  129. 									{
  130. 										return ShowHelp( "Invalid system tray icon index \"{0}\"", arg.Substring( 3 ) );
  131. 									}
  132. 								}
  133. 								catch ( Exception )
  134. 								{
  135. 									return ShowHelp( "Invalid system tray icon argument \"{0}\"", arg );
  136. 								}
  137. 								break;
  138. 							case "T":
  139. 								if ( String.IsNullOrWhiteSpace( title ) )
  140. 								{
  141. 									title = arg.Substring( 3 ).Trim( "\" \t".ToCharArray( ) );
  142. 								}
  143. 								else
  144. 								{
  145. 									return ShowHelp( "Duplicate command line switch /T" );
  146. 								}
  147. 								break;
  148. 							case "V":
  149. 								if ( timeout != 10000 )
  150. 								{
  151. 									return ShowHelp( "Duplicate command line switch /V" );
  152. 								}
  153. 								try
  154. 								{
  155. 									timeout = 1000 * Convert.ToInt32( arg.Substring( 3 ) );
  156. 									if ( timeout < 1000 )
  157. 									{
  158. 										return ShowHelp( "Invalid time (\"{0}\"), must be greater than zero", arg.Substring( 3 ) );
  159. 									}
  160. 								}
  161. 								catch ( Exception )
  162. 								{
  163. 									return ShowHelp( "Invalid time \"{0}\"", arg.Substring( 3 ) );
  164. 								}
  165. 								break;
  166. 							default:
  167. 								return ShowHelp( "Invalid command line switch \"{0}\"", arg );
  168. 						}
  169. 					}
  170. 					else
  171. 					{
  172. 						return ShowHelp( "Invalid command line switch \"{0}\"", arg );
  173. 					}
  174. 				}
  175. 				else
  176. 				{
  177. 					if ( String.IsNullOrWhiteSpace( message ) )
  178. 					{
  179. 						message = arg;
  180. 					}
  181. 					else
  182. 					{
  183. 						return ShowHelp( "Duplicate message on command line" );
  184. 					}
  185. 				}
  186. 			}
  187.  
  188. 			if ( String.IsNullOrWhiteSpace( message ) )
  189. 			{
  190. 				return ShowHelp( "Please specify a message text" );
  191. 			}
  192.  
  193. 			if ( escapemessage )
  194. 			{
  195. 				message = Regex.Replace( message, @"(?<!\\)\\n", "\n" );
  196. 				message = Regex.Replace( message, @"(?<!\\)\\t", "\t" );
  197. 				message = Regex.Replace( message, @"\\", @"\" );
  198. 			}
  199.  
  200. 			if ( iconext == ".dll" )
  201. 			{
  202. 				systrayicon = IconExtractor.Extract( iconfile, iconindex, true );
  203. 			}
  204. 			else
  205. 			{
  206. 				systrayicon = new Icon( iconfile );
  207. 			}
  208.  
  209. 			#endregion Parse Command Line
  210.  
  211.  
  212. 			NotifyIcon notifyicon = new NotifyIcon( );
  213. 			notifyicon.Icon = systrayicon;
  214. 			notifyicon.Visible = true;
  215. 			notifyicon.ShowBalloonTip( timeout, title, message, tooltipicon );
  216.  
  217. 			if ( wait )
  218. 			{
  219. 				// Wait till timeout elapses
  220. 				Thread.Sleep( timeout );
  221. 				// Hide icon
  222. 				notifyicon.Visible = false;
  223. 				notifyicon.Dispose( );
  224. 			}
  225.  
  226. 			return 0;
  227. 		}
  228.  
  229.  
  230. 		static int ShowHelp( params string[] errmsg )
  231. 		{
  232. 			#region Error Message
  233.  
  234. 			if ( errmsg.Length > 0 )
  235. 			{
  236. 				List<string> errargs = new List<string>( errmsg );
  237. 				errargs.RemoveAt( 0 );
  238. 				Console.Error.WriteLine( );
  239. 				Console.ForegroundColor = ConsoleColor.Red;
  240. 				Console.Error.Write( "ERROR:\t" );
  241. 				Console.ForegroundColor = ConsoleColor.White;
  242. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  243. 				Console.ResetColor( );
  244. 			}
  245.  
  246. 			#endregion Error Message
  247.  
  248.  
  249. 			#region Help Text
  250.  
  251. 			/*
  252. 			SystemTrayMessage.exe,  Version 1.04
  253. 			Display a tooltip message in the system tray's notification area
  254.  
  255. 			Usage:    SystemTrayMessage.exe  message  [ options ]
  256.  
  257. 			Where:    message     is the message text in the tooltip balloon
  258.  
  259. 			Options:  /I:icon     tooltip Icon (Error, Info, None, Warning; default: Info)
  260. 			          /L          treat message as Literal text without interpreting
  261. 			                      escaped characters, e.g. show "\n" as literal "\n"
  262. 			                      instead of interpreting it as a newline character
  263. 			          /P:path     Path to an icon file or library (default: Shell32.dll)
  264. 			          /S:index    System tray icon index in icon library (default: 277)
  265. 			          /T:title    specifies the optional Title in the tooltip balloon
  266. 			          /V:seconds  specifies the number of seconds the tooltip balloon
  267. 			                      will remain Visible (default: 10)
  268. 			          /W          Wait for the timeout to elapse and remove the icon from
  269. 			                      the notification area (default: exit without waiting)
  270.  
  271. 			Notes:    By default, \n is interpreted as newline and \t as tab in message;
  272. 			          in some cases this may lead to misinterpretations, e.g. when showing
  273. 			          a path like "c:\temp"; either escape backslashes in paths or use /L
  274. 			          to treat all message text as literal text.
  275. 			          Command line switch /S will be ignored if switch /P specifies
  276. 			          anything but an icon library.
  277. 			          Use my Shell32Icons.exe to select a Shell32 icon and get its index.
  278. 			          Return code ("ErrorLevel") is -1 in case of errors, or 0 otherwise.
  279.  
  280. 			Credits:  Code to extract icons from Shell32.dll by Thomas Levesque
  281. 			          http://stackoverflow.com/questions/6873026
  282.  
  283. 			Written by Rob van der Woude
  284. 			http://www.robvanderwoude.com
  285. 			*/
  286.  
  287. 			Console.Error.WriteLine( );
  288.  
  289. 			Console.Error.WriteLine( "SystemTrayMessage.exe,  Version {0}", progver );
  290.  
  291. 			Console.Error.WriteLine( "Display a tooltip message in the system tray's notification area" );
  292.  
  293. 			Console.Error.WriteLine( );
  294.  
  295. 			Console.Error.Write( "Usage:    " );
  296. 			Console.ForegroundColor = ConsoleColor.White;
  297. 			Console.Error.WriteLine( "SystemTrayMessage.exe  message  [ options ]" );
  298. 			Console.ResetColor( );
  299.  
  300. 			Console.Error.WriteLine( );
  301.  
  302. 			Console.Error.Write( "Where:    " );
  303. 			Console.ForegroundColor = ConsoleColor.White;
  304. 			Console.Error.Write( "message" );
  305. 			Console.ResetColor( );
  306. 			Console.Error.Write( "     is the " );
  307. 			Console.ForegroundColor = ConsoleColor.White;
  308. 			Console.Error.Write( "message" );
  309. 			Console.ResetColor( );
  310. 			Console.Error.WriteLine( " text in the tooltip balloon" );
  311.  
  312. 			Console.Error.WriteLine( );
  313.  
  314. 			Console.Error.Write( "Options:  " );
  315. 			Console.ForegroundColor = ConsoleColor.White;
  316. 			Console.Error.Write( "/I:icon" );
  317. 			Console.ResetColor( );
  318. 			Console.Error.Write( "     tooltip " );
  319. 			Console.ForegroundColor = ConsoleColor.White;
  320. 			Console.Error.Write( "I" );
  321. 			Console.ResetColor( );
  322. 			Console.Error.WriteLine( "con (Error, Info, None, Warning; default: Info)" );
  323.  
  324. 			Console.ForegroundColor = ConsoleColor.White;
  325. 			Console.Error.Write( "          /L" );
  326. 			Console.ResetColor( );
  327. 			Console.Error.Write( "          treat " );
  328. 			Console.ForegroundColor = ConsoleColor.White;
  329. 			Console.Error.Write( "message" );
  330. 			Console.ResetColor( );
  331. 			Console.Error.Write( " as " );
  332. 			Console.ForegroundColor = ConsoleColor.White;
  333. 			Console.Error.Write( "L" );
  334. 			Console.ResetColor( );
  335. 			Console.Error.WriteLine( "iteral text without interpreting" );
  336.  
  337. 			Console.Error.WriteLine( "                      escaped characters, e.g. show \"\\n\" as literal \"\\n\"" );
  338.  
  339. 			Console.Error.WriteLine( "                      instead of interpreting it as a newline character" );
  340.  
  341. 			Console.ForegroundColor = ConsoleColor.White;
  342. 			Console.Error.Write( "          /P:path     P" );
  343. 			Console.ResetColor( );
  344. 			Console.Error.WriteLine( "ath to an icon file or library (default: Shell32.dll)" );
  345.  
  346. 			Console.ForegroundColor = ConsoleColor.White;
  347. 			Console.Error.Write( "          /S:index    S" );
  348. 			Console.ResetColor( );
  349. 			Console.Error.Write( "ystem tray icon " );
  350. 			Console.ForegroundColor = ConsoleColor.White;
  351. 			Console.Error.Write( "index" );
  352. 			Console.ResetColor( );
  353. 			Console.Error.WriteLine( "  in icon library (default: 277)" );
  354.  
  355. 			Console.ForegroundColor = ConsoleColor.White;
  356. 			Console.Error.Write( "          /T:title" );
  357. 			Console.ResetColor( );
  358. 			Console.Error.Write( "    optional " );
  359. 			Console.ForegroundColor = ConsoleColor.White;
  360. 			Console.Error.Write( "T" );
  361. 			Console.ResetColor( );
  362. 			Console.Error.WriteLine( "itle in the tooltip balloon" );
  363.  
  364. 			Console.ForegroundColor = ConsoleColor.White;
  365. 			Console.Error.Write( "          /V:seconds" );
  366. 			Console.ResetColor( );
  367. 			Console.Error.Write( "  number of " );
  368. 			Console.ForegroundColor = ConsoleColor.White;
  369. 			Console.Error.Write( "seconds" );
  370. 			Console.ResetColor( );
  371. 			Console.Error.WriteLine( " the tooltip balloon will remain" );
  372.  
  373. 			Console.Error.Write( "" );
  374. 			Console.ForegroundColor = ConsoleColor.White;
  375. 			Console.Error.Write( "                      V" );
  376. 			Console.ResetColor( );
  377. 			Console.Error.WriteLine( "isible (default: 10)" );
  378.  
  379. 			Console.ForegroundColor = ConsoleColor.White;
  380. 			Console.Error.Write( "          /W          W" );
  381. 			Console.ResetColor( );
  382. 			Console.Error.WriteLine( "ait for the timeout to elapse and remove the icon from" );
  383.  
  384. 			Console.Error.WriteLine( "                      the notification area (default: exit without waiting)" );
  385.  
  386. 			Console.Error.WriteLine( );
  387.  
  388. 			Console.Error.Write( "Notes:    By default, " );
  389. 			Console.ForegroundColor = ConsoleColor.White;
  390. 			Console.Error.Write( "\\n" );
  391. 			Console.ResetColor( );
  392. 			Console.Error.Write( " is interpreted as newline and " );
  393. 			Console.ForegroundColor = ConsoleColor.White;
  394. 			Console.Error.Write( "\\t" );
  395. 			Console.ResetColor( );
  396. 			Console.Error.Write( " as tab in " );
  397. 			Console.ForegroundColor = ConsoleColor.White;
  398. 			Console.Error.Write( "message" );
  399. 			Console.ResetColor( );
  400. 			Console.Error.WriteLine( ";" );
  401.  
  402. 			Console.Error.WriteLine( "          in some cases this may lead to misinterpretations, e.g. when showing" );
  403.  
  404. 			Console.Error.Write( "          a path like \"c:\\temp\"; either escape backslashes in paths or use " );
  405. 			Console.ForegroundColor = ConsoleColor.White;
  406. 			Console.Error.WriteLine( "/L" );
  407. 			Console.ResetColor( );
  408.  
  409. 			Console.Error.WriteLine( "          to treat all message text as literal text." );
  410.  
  411. 			Console.Error.Write( "          Command line switch " );
  412. 			Console.ForegroundColor = ConsoleColor.White;
  413. 			Console.Error.Write( "/S" );
  414. 			Console.ResetColor( );
  415. 			Console.Error.Write( " will be ignored if switch " );
  416. 			Console.ForegroundColor = ConsoleColor.White;
  417. 			Console.Error.Write( "/P" );
  418. 			Console.ResetColor( );
  419. 			Console.Error.WriteLine( " specifies" );
  420.  
  421. 			Console.Error.WriteLine( "          anything but an icon library." );
  422.  
  423. 			Console.Error.WriteLine( "          Use my Shell32Icons.exe to select a Shell32 icon and get its index." );
  424.  
  425. 			Console.Error.WriteLine( "          Return code (\"ErrorLevel\") is -1 in case of errors, or 0 otherwise." );
  426.  
  427. 			Console.Error.WriteLine( );
  428.  
  429. 			Console.Error.WriteLine( "Credits:  Code to extract icons from Shell32.dll by Thomas Levesque" );
  430.  
  431. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  432. 			Console.Error.WriteLine( "          http://stackoverflow.com/questions/6873026" );
  433. 			Console.ResetColor( );
  434.  
  435. 			Console.Error.WriteLine( );
  436.  
  437. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  438.  
  439. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  440.  
  441. 			#endregion Help Text
  442.  
  443.  
  444. 			return -1;
  445. 		}
  446.  
  447.  
  448. 		// Code to extract icons from Shell32.dll by Thomas Levesque
  449. 		// http://stackoverflow.com/questions/6873026
  450. 		public class IconExtractor
  451. 		{
  452. 			public static Icon Extract( string file, int number, bool largeIcon )
  453. 			{
  454. 				IntPtr large;
  455. 				IntPtr small;
  456. 				ExtractIconEx( file, number, out large, out small, 1 );
  457. 				try
  458. 				{
  459. 					return Icon.FromHandle( largeIcon ? large : small );
  460. 				}
  461. 				catch
  462. 				{
  463. 					return null;
  464. 				}
  465. 			}
  466.  
  467. 			[DllImport( "Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall )]
  468. 			private static extern int ExtractIconEx( string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons );
  469. 		}
  470. 	}
  471. }
  472.  

page last uploaded: 2017-08-21, 14:26