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

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