Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for messagebox.cs

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

  1. using System;
  2. using System.Globalization;
  3. using System.Runtime.InteropServices;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Forms;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class MessageBox
  11. 	{
  12. 		static string progver = "1.30";
  13.  
  14. 		static bool timeoutelapsed = false;
  15. 		static string defaultmessage = DefaultMessage( );
  16. 		static string defaulttitle = String.Format( "MessageBox {0}", progver );
  17. 		static int timeout = 0;
  18.  
  19.  
  20. 		static int Main( string[] args )
  21. 		{
  22. 			#region Initialize Variables
  23.  
  24. 			string message = defaultmessage;
  25. 			string title = defaulttitle;
  26. 			MessageBoxButtons buttons = MessageBoxButtons.OK;
  27. 			MessageBoxIcon icon = MessageBoxIcon.Information;
  28. 			MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button1;
  29. 			MessageBoxOptions option = MessageBoxOptions.DefaultDesktopOnly;
  30. 			bool buttonsset = false;
  31. 			bool defaultset = false;
  32. 			bool escapemessage = true;
  33. 			bool iconset = false;
  34. 			bool optionsset = false;
  35. 			bool useswitches = false;
  36. 			DialogResult result;
  37. 			int rc = 0;
  38.  
  39. 			#endregion Initialize Variables
  40.  
  41.  
  42. 			#region Command Line Parsing
  43.  
  44. 			foreach ( string arg in args )
  45. 			{
  46. 				if ( arg[0] == '/' )
  47. 				{
  48. 					useswitches = true;
  49. 					if ( arg.Length > 3 && arg[2] == ':' )
  50. 					{
  51. 						string key = arg[1].ToString( ).ToUpper( );
  52. 						string val = arg.Substring( 3 ).ToUpper( );
  53. 						switch ( key )
  54. 						{
  55. 							case "?":
  56. 								return DisplayHelp( );
  57. 							case "B":
  58. 								switch ( val )
  59. 								{
  60. 									case "A":
  61. 									case "ABORTRETRYIGNORE":
  62. 										buttons = MessageBoxButtons.AbortRetryIgnore;
  63. 										break;
  64. 									case "C":
  65. 									case "OKCANCEL":
  66. 										buttons = MessageBoxButtons.OKCancel;
  67. 										break;
  68. 									case "N":
  69. 									case "YESNOCANCEL":
  70. 										buttons = MessageBoxButtons.YesNoCancel;
  71. 										break;
  72. 									case "O":
  73. 									case "OK":
  74. 										buttons = MessageBoxButtons.OK;
  75. 										break;
  76. 									case "R":
  77. 									case "RETRYCANCEL":
  78. 										buttons = MessageBoxButtons.RetryCancel;
  79. 										break;
  80. 									case "Y":
  81. 									case "YESNO":
  82. 										buttons = MessageBoxButtons.YesNo;
  83. 										break;
  84. 									default:
  85. 										rc = 1;
  86. 										break;
  87. 								}
  88. 								buttonsset = true;
  89. 								break;
  90. 							case "D":
  91. 								if ( !buttonsset )
  92. 								{
  93. 									rc = 1;
  94. 								}
  95. 								switch ( val )
  96. 								{
  97. 									case "1":
  98. 									case "BUTTON1":
  99. 									case "ABORT":
  100. 									case "OK":
  101. 									case "YES":
  102. 										defaultbutton = MessageBoxDefaultButton.Button1;
  103. 										break;
  104. 									case "2":
  105. 									case "BUTTON2":
  106. 									case "NO":
  107. 										defaultbutton = MessageBoxDefaultButton.Button2;
  108. 										break;
  109. 									case "3":
  110. 									case "BUTTON3":
  111. 									case "IGNORE":
  112. 										defaultbutton = MessageBoxDefaultButton.Button3;
  113. 										break;
  114. 									case "CANCEL":
  115. 										if ( buttons == MessageBoxButtons.YesNoCancel )
  116. 										{
  117. 											defaultbutton = MessageBoxDefaultButton.Button3;
  118. 										}
  119. 										else
  120. 										{
  121. 											defaultbutton = MessageBoxDefaultButton.Button2;
  122. 										}
  123. 										break;
  124. 									case "RETRY":
  125. 										if ( buttons == MessageBoxButtons.RetryCancel )
  126. 										{
  127. 											defaultbutton = MessageBoxDefaultButton.Button1;
  128. 										}
  129. 										else
  130. 										{
  131. 											defaultbutton = MessageBoxDefaultButton.Button2;
  132. 										}
  133. 										break;
  134. 									default:
  135. 										rc = 1;
  136. 										break;
  137. 								}
  138. 								defaultset = true;
  139. 								break;
  140. 							case "I":
  141. 								switch ( val )
  142. 								{
  143. 									case "A":
  144. 									case "ASTERISK":
  145. 										icon = MessageBoxIcon.Asterisk;
  146. 										break;
  147. 									case "E":
  148. 									case "ERROR":
  149. 										icon = MessageBoxIcon.Error;
  150. 										break;
  151. 									case "H":
  152. 									case "HAND":
  153. 										icon = MessageBoxIcon.Hand;
  154. 										break;
  155. 									case "I":
  156. 									case "INFORMATION":
  157. 										icon = MessageBoxIcon.Information;
  158. 										break;
  159. 									case "N":
  160. 									case "NONE":
  161. 										icon = MessageBoxIcon.None;
  162. 										break;
  163. 									case "Q":
  164. 									case "QUESTION":
  165. 										icon = MessageBoxIcon.Question;
  166. 										break;
  167. 									case "S":
  168. 									case "STOP":
  169. 										icon = MessageBoxIcon.Stop;
  170. 										break;
  171. 									case "W":
  172. 									case "WARNING":
  173. 										icon = MessageBoxIcon.Warning;
  174. 										break;
  175. 									case "X":
  176. 									case "EXCLAMATION":
  177. 										icon = MessageBoxIcon.Exclamation;
  178. 										break;
  179. 									default:
  180. 										rc = 1;
  181. 										break;
  182. 								}
  183. 								iconset = true;
  184. 								break;
  185. 							case "O":
  186. 								switch ( val )
  187. 								{
  188. 									case "H":
  189. 									case "HIDECONSOLE":
  190. 										HideConsoleWindow( );
  191. 										break;
  192. 									case "L":
  193. 									case "RTLREADING":
  194. 										option |= MessageBoxOptions.RtlReading;
  195. 										break;
  196. 									case "N":
  197. 									case "NOESCAPE":
  198. 										escapemessage = false;
  199. 										break;
  200. 									case "R":
  201. 									case "RIGHTALIGN":
  202. 										option |= MessageBoxOptions.RightAlign;
  203. 										break;
  204. 									default:
  205. 										rc = 1;
  206. 										break;
  207. 								}
  208. 								optionsset = true;
  209. 								break;
  210. 							case "T":
  211. 								rc = CheckTimeout( val );
  212. 								break;
  213. 							default:
  214. 								rc = 1;
  215. 								break;
  216. 						}
  217. 					}
  218. 					else
  219. 					{
  220. 						rc = 1;
  221. 					}
  222. 				}
  223. 				else
  224. 				{
  225. 					if ( message == defaultmessage )
  226. 					{
  227. 						message = arg;
  228. 					}
  229. 					else if ( title == defaulttitle )
  230. 					{
  231. 						title = arg;
  232. 					}
  233. 					else if ( useswitches ) // If switches are used, only 2 "unnamed" arguments are allowed
  234. 					{
  235. 						rc = 1;
  236. 					}
  237. 					else if ( !buttonsset )
  238. 					{
  239. 						switch ( arg.ToLower( ) )
  240. 						{
  241. 							case "abortretryignore":
  242. 								buttons = MessageBoxButtons.AbortRetryIgnore;
  243. 								break;
  244. 							case "ok":
  245. 								buttons = MessageBoxButtons.OK;
  246. 								break;
  247. 							case "okcancel":
  248. 								buttons = MessageBoxButtons.OKCancel;
  249. 								break;
  250. 							case "retrycancel":
  251. 								buttons = MessageBoxButtons.RetryCancel;
  252. 								break;
  253. 							case "yesno":
  254. 								buttons = MessageBoxButtons.YesNo;
  255. 								break;
  256. 							case "yesnocancel":
  257. 								buttons = MessageBoxButtons.YesNoCancel;
  258. 								break;
  259. 							default:
  260. 								buttons = MessageBoxButtons.OK;
  261. 								rc = 1;
  262. 								break;
  263. 						}
  264. 						buttonsset = true;
  265. 					}
  266. 					else if ( !iconset )
  267. 					{
  268. 						switch ( arg.ToLower( ) )
  269. 						{
  270. 							case "asterisk":
  271. 								icon = MessageBoxIcon.Asterisk;
  272. 								break;
  273. 							case "error":
  274. 								icon = MessageBoxIcon.Error;
  275. 								break;
  276. 							case "exclamation":
  277. 								icon = MessageBoxIcon.Exclamation;
  278. 								break;
  279. 							case "hand":
  280. 								icon = MessageBoxIcon.Hand;
  281. 								break;
  282. 							case "information":
  283. 								icon = MessageBoxIcon.Information;
  284. 								break;
  285. 							case "none":
  286. 								icon = MessageBoxIcon.None;
  287. 								break;
  288. 							case "question":
  289. 								icon = MessageBoxIcon.Question;
  290. 								break;
  291. 							case "stop":
  292. 								icon = MessageBoxIcon.Stop;
  293. 								break;
  294. 							case "warning":
  295. 								icon = MessageBoxIcon.Warning;
  296. 								break;
  297. 							default:
  298. 								icon = MessageBoxIcon.Warning;
  299. 								rc = 1;
  300. 								break;
  301. 						}
  302. 						iconset = true;
  303. 					}
  304. 					else if ( !defaultset )
  305. 					{
  306. 						switch ( arg.ToLower( ) )
  307. 						{
  308. 							case "":
  309. 							case "abort":
  310. 							case "button1":
  311. 							case "ok":
  312. 							case "yes":
  313. 								defaultbutton = MessageBoxDefaultButton.Button1;
  314. 								break;
  315. 							case "button2":
  316. 							case "no":
  317. 								defaultbutton = MessageBoxDefaultButton.Button2;
  318. 								break;
  319. 							case "button3":
  320. 							case "ignore":
  321. 								defaultbutton = MessageBoxDefaultButton.Button3;
  322. 								break;
  323. 							case "cancel":
  324. 								if ( args[2].ToLower( ) == "okcancel" || args[2].ToLower( ) == "retrycancel" )
  325. 								{
  326. 									defaultbutton = MessageBoxDefaultButton.Button2;
  327. 								}
  328. 								else // yesnocancel
  329. 								{
  330. 									defaultbutton = MessageBoxDefaultButton.Button3;
  331. 								}
  332. 								break;
  333. 							case "retry":
  334. 								if ( args[2].ToLower( ) == "abortretryignore" )
  335. 								{
  336. 									defaultbutton = MessageBoxDefaultButton.Button2;
  337. 								}
  338. 								else // retrycancel
  339. 								{
  340. 									defaultbutton = MessageBoxDefaultButton.Button1;
  341. 								}
  342. 								break;
  343. 							default:
  344. 								defaultbutton = MessageBoxDefaultButton.Button1;
  345. 								rc = 1;
  346. 								break;
  347. 						}
  348. 						defaultset = true;
  349. 					}
  350. 					else if ( !optionsset )
  351. 					{
  352. 						switch ( arg.ToLower( ) )
  353. 						{
  354. 							case "":
  355. 							case "none":
  356. 								optionsset = true;
  357. 								break;
  358. 							case "hideconsole":
  359. 								HideConsoleWindow( );
  360. 								optionsset = true;
  361. 								break;
  362. 							case "noescape":
  363. 								escapemessage = false;
  364. 								optionsset = true;
  365. 								break;
  366. 							case "rightalign":
  367. 								option = MessageBoxOptions.RightAlign;
  368. 								optionsset = true;
  369. 								break;
  370. 							case "rtlreading":
  371. 								option = MessageBoxOptions.RtlReading;
  372. 								optionsset = true;
  373. 								break;
  374. 							default: // try if option is unspecified and argument is timeout
  375. 								rc = CheckTimeout( arg );
  376. 								break;
  377. 						}
  378. 					}
  379. 					else if ( timeout == 0 )
  380. 					{
  381. 						rc = CheckTimeout( arg );
  382. 					}
  383. 					else
  384. 					{
  385. 						rc = 1;
  386. 					}
  387. 				}
  388. 			}
  389.  
  390. 			if ( !escapemessage && message != defaultmessage )
  391. 			{
  392. 				message = UnEscapeString( message );
  393. 				title = UnEscapeString( title );
  394. 			}
  395.  
  396. 			// MessageBoxOptions.ServiceNotification allows interactive use by SYSTEM account (or any other account not currently logged in)
  397. 			option |= MessageBoxOptions.ServiceNotification;
  398.  
  399. 			#endregion Command Line Parsing
  400.  
  401.  
  402. 			if ( rc == 1 ) // command line error
  403. 			{
  404. 				ShowConsoleWindow( );
  405. 				message = defaultmessage;
  406. 				title = defaulttitle;
  407. 				buttons = MessageBoxButtons.OK;
  408. 				icon = MessageBoxIcon.Warning;
  409. 				defaultbutton = MessageBoxDefaultButton.Button1;
  410. 				return DisplayHelp( );
  411. 			}
  412.  
  413. 			if ( rc == 0 && timeout > 0 )
  414. 			{
  415. 				result = AutoClosingMessageBox.Show( message, title, timeout, buttons, icon, defaultbutton, option );
  416. 				if ( timeoutelapsed )
  417. 				{
  418. 					Console.WriteLine( "timeout" );
  419. 					return 3;
  420. 				}
  421. 			}
  422. 			else
  423. 			{
  424. 				if ( message == defaultmessage )
  425. 				{
  426. 					message = defaultmessage.Substring( 0, defaultmessage.IndexOf( "\n\nNotes:" ) ) + "\n\nWritten by Rob van der Woude\nhttp://www.robvanderwoude.com";
  427. 					result = System.Windows.Forms.MessageBox.Show( message, title, buttons, icon, defaultbutton, option );
  428. 					Console.WriteLine( result.ToString( ).ToLower( ) );
  429. 					message = defaultmessage.Substring( defaultmessage.IndexOf( "Notes:" ) );
  430. 				}
  431. 				result = System.Windows.Forms.MessageBox.Show( message, title, buttons, icon, defaultbutton, option );
  432. 			}
  433. 			Console.WriteLine( result.ToString( ).ToLower( ) );
  434. 			return rc;
  435. 		}
  436.  
  437.  
  438. 		static int CheckTimeout( string val )
  439. 		{
  440. 			int rc = 0;
  441. 			try
  442. 			{
  443. 				timeout = Convert.ToInt32( val ) * 1000;
  444. 				if ( timeout < 1000 )
  445. 				{
  446. 					rc = 1;
  447. 				}
  448. 			}
  449. 			catch ( FormatException )
  450. 			{
  451. 				rc = 1;
  452. 			}
  453. 			return rc;
  454. 		}
  455.  
  456.  
  457. 		static string DefaultMessage( )
  458. 		{
  459. 			string message = "MessageBox.exe,  Version " + progver + "\n";
  460. 			message += "Batch tool to show a message in a MessageBox and return the caption\nof the button that is clicked\n\n";
  461. 			message += "Usage:\nMessageBox \"message\" [ \"title\" ]  [ switches ]\n\n";
  462. 			message += "Or:\nMessageBox \"message\" \"title\" buttons icon default [option] timeout\n\n";
  463. 			message += "Where:\tbuttons\t\"AbortRetryIgnore\", \"OK\", \"OKCancel\",\n";
  464. 			message += "\t\t\"RetryCancel\", \"YesNo\" or \"YesNoCancel\"\n";
  465. 			message += "\ticon\t\"Asterisk\", \"Error\", \"Exclamation\", \"Hand\",\n";
  466. 			message += "\t\t\"Information\", \"None\", \"Question\", \"Stop\"\n";
  467. 			message += "\t\tor \"Warning\"\n";
  468. 			message += "\tdefault\t\"Button1\", \"Button2\" or \"Button3\" or the\n\t\tdefault button's (English) caption\n";
  469. 			message += "\toption\t\"HideConsole\", \"NoEscape\", \"RightAlign\",\n\t\t\"RtlReading\", \"None\" or \"\"\n";
  470. 			message += "\ttimeout\ttimeout interval in seconds\n\n";
  471. 			message += "Switches:\t/B:buttons\tA = AbortRetryIgnore, O = OK,\n";
  472. 			message += "        \t\t\tC = OKCancel, R = RetryCancel,\n";
  473. 			message += "        \t\t\tY = YesNo, N = YesNoCancel\n";
  474. 			message += "            \t/I:icon        \tA = Asterisk, E = Error,\n";
  475. 			message += "        \t\t\tX = Exclamation, H = Hand,\n";
  476. 			message += "        \t\t\tI = Information, N = None,\n";
  477. 			message += "        \t\t\tQ = Question, S = Stop\n";
  478. 			message += "        \t\t\tW = Warning\n";
  479. 			message += "            \t/D:default    \t1 = Button1, 2 = Button2,\n";
  480. 			message += "        \t\t\t3 = Button3 or use the default\n";
  481. 			message += "        \t\t\tbutton's (English) caption\n";
  482. 			message += "            \t/O:option     \tH = HideConsole, N = NoEscape,\n";
  483. 			message += "        \t\t\tR = RightAlign, L = RtlReading\n";
  484. 			message += "            \t/T:timeout\ttimeout interval in seconds\n\n";
  485. 			message += "Notes:\tUse switches if you want to skip arguments.\n\n";
  486. 			message += "\tAlways specify buttons BEFORE specifying default.\n\n";
  487. 			message += "\tUsing the \"HideConsole\" option will hide the console\n\twindow permanently, thereby disabling all console based\n\tuser interaction (e.g. \"ECHO\" and \"PAUSE\").\n\tIt is meant to be used in scripts that run \"hidden\"\n\tthemselves, e.g. VBScript with the WScript.exe interpreter.\n\tDo not use this option in a batch file unless hiding\n\tthe console window permanently is intended.\n\n";
  488. 			message += "\tLinefeeds (\\n or \\012 and/or \\r or \\015), tabs (\\t or \\007),\n\tsinglequotes (' or \\047) and doublequotes (\\\" or \\042)\n\tare allowed in the message string.\n";
  489. 			message += "\tEscaped Unicode characters (e.g. \"\\u5173\" for \"\u5173\")\n\tare allowed in the message string and in the title.\n";
  490. 			message += "\tUse option \"NoEscape\" to disable all character escaping\n\texcept doublequotes (useful when displaying a path).\n\n";
  491. 			message += "\tThe (English) caption of the button that was clicked\n\tis returned as text to Standard Output (in lower case),\n\tor \"timeout\" if the timeout interval expired.\n\n";
  492. 			message += "\tCode to hide console by Anthony on:\n\thttp://stackoverflow.com/a/15079092\n\n";
  493. 			message += "\tMessageBox timeout based on code by DmitryG on:\n\thttp://stackoverflow.com/a/14522952\n\n";
  494. 			message += "\tNote that when using the timeout feature, A window\n\twith the current MessageBox's TITLE will be closed,\n\tnot necessarily the current MessageBox. To prevent\n\tclosing the wrong MessageBox, use unique titles.\n\n";
  495. 			message += "\tThe return code of the program is 0 if a button was clicked,\n\t1 in case of (command line) errors, 3 if the timeout expired.\n\n";
  496. 			message += "Written by Rob van der Woude\nhttp://www.robvanderwoude.com\n";
  497. 			return message;
  498. 		}
  499.  
  500.  
  501. 		static int DisplayHelp()
  502. 		{
  503. 			// Display the help text in 2 message boxes AND in the console
  504. 			DisplayHelpText( );
  505. 			if ( DisplayHelpWindow( 1 ) != DialogResult.Cancel )
  506. 			{
  507. 				DisplayHelpWindow( 2 );
  508. 			}
  509. 			return 1;
  510. 		}
  511.  
  512.  
  513. 		static void DisplayHelpText( )
  514. 		{
  515. 			string message = DefaultMessage( );
  516. 			message = message.Replace( "\n\n\t", "\n\t" );
  517. 			message = message.Replace( "Usage:\n", "Usage:\t" );
  518. 			message = message.Replace( "Or:\n", "   or:\t" );
  519. 			Console.Error.Write( message );
  520. 		}
  521.  
  522.  
  523. 		static DialogResult DisplayHelpWindow( int part )
  524. 		{
  525. 			// part is 1 or 2; 1 for first half of message, 2 for second half
  526. 			string message = DefaultMessage( );
  527. 			if ( part == 1 )
  528. 			{
  529. 				message = message.Substring( 0, message.IndexOf( "\n\nNotes:" ) ) + "\n\nWritten by Rob van der Woude\nhttp://www.robvanderwoude.com";
  530. 				return System.Windows.Forms.MessageBox.Show( message, defaulttitle, MessageBoxButtons.OKCancel, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification );
  531. 			}
  532. 			else
  533. 			{
  534. 				message = message.Substring( message.IndexOf( "Notes:" ) );
  535. 				return System.Windows.Forms.MessageBox.Show( message, defaulttitle, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification );
  536. 			}
  537. 		}
  538.  
  539.  
  540. 		static string UnEscapeString( string message )
  541. 		{
  542. 			// Unescaping tabs, linefeeds and quotes
  543. 			message = message.Replace( "\\n", "\n" );
  544. 			message = message.Replace( "\\r", "\r" );
  545. 			message = message.Replace( "\\t", "\t" );
  546. 			message = message.Replace( "\\007", "\t" );
  547. 			message = message.Replace( "\\012", "\n" );
  548. 			message = message.Replace( "\\015", "\r" );
  549. 			message = message.Replace( "\\042", "\"" );
  550. 			message = message.Replace( "\\047", "'" );
  551. 			// Unescaping Unicode, technique by "dtb" on StackOverflow.com: http://stackoverflow.com/a/8558748
  552. 			message = Regex.Replace( message, @"\\[Uu]([0-9A-Fa-f]{4})", m => char.ToString( (char) ushort.Parse( m.Groups[1].Value, NumberStyles.AllowHexSpecifier ) ) );
  553. 			return message;
  554. 		}
  555.  
  556.  
  557. 		#region Hide or Show Console
  558. 		// Source: http://stackoverflow.com/a/15079092
  559.  
  560. 		public static void ShowConsoleWindow( )
  561. 		{
  562. 			var handle = GetConsoleWindow( );
  563.  
  564. 			if ( handle == IntPtr.Zero )
  565. 			{
  566. 				AllocConsole( );
  567. 			}
  568. 			else
  569. 			{
  570. 				ShowWindow( handle, SW_SHOW );
  571. 			}
  572. 		}
  573.  
  574.  
  575. 		public static void HideConsoleWindow( )
  576. 		{
  577. 			var handle = GetConsoleWindow( );
  578.  
  579. 			ShowWindow( handle, SW_HIDE );
  580. 		}
  581.  
  582.  
  583. 		[DllImport( "kernel32.dll", SetLastError = true )]
  584. 		static extern bool AllocConsole( );
  585.  
  586.  
  587. 		[DllImport( "kernel32.dll" )]
  588. 		static extern IntPtr GetConsoleWindow( );
  589.  
  590.  
  591. 		[DllImport( "user32.dll" )]
  592. 		static extern bool ShowWindow( IntPtr hWnd, int nCmdShow );
  593.  
  594.  
  595. 		const int SW_HIDE = 0;
  596. 		const int SW_SHOW = 5;
  597.  
  598. 		#endregion Hide or Show Console
  599.  
  600.  
  601. 		#region Timed MessageBox
  602.  
  603. 		// Timed MessageBox based on code by DmitryG on StackOverflow.com
  604. 		// http://stackoverflow.com/a/14522952
  605. 		public class AutoClosingMessageBox
  606. 		{
  607. 			System.Threading.Timer _timeouttimer;
  608. 			string _caption;
  609. 			DialogResult _result;
  610.  
  611.  
  612. 			AutoClosingMessageBox( string message, string title, int timeout, MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.None, MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button1, MessageBoxOptions option = MessageBoxOptions.DefaultDesktopOnly )
  613. 			{
  614. 				_caption = title;
  615. 				_timeouttimer = new System.Threading.Timer( OnTimerElapsed, null, timeout, System.Threading.Timeout.Infinite );
  616.  
  617. 				using ( _timeouttimer )
  618. 				{
  619. 					_result = System.Windows.Forms.MessageBox.Show( message, title, buttons, icon, defaultbutton, option | MessageBoxOptions.ServiceNotification );
  620. 				}
  621. 			}
  622.  
  623.  
  624. 			public static DialogResult Show( string message, string title, int timeout, MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.None, MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button1, MessageBoxOptions option = MessageBoxOptions.DefaultDesktopOnly )
  625. 			{
  626. 				return new AutoClosingMessageBox( message, title, timeout, buttons, icon, defaultbutton, option | MessageBoxOptions.ServiceNotification )._result;
  627. 			}
  628.  
  629.  
  630. 			void OnTimerElapsed( object state )
  631. 			{
  632. 				IntPtr mbWnd = FindWindow( "#32770", _caption ); // lpClassName is #32770 for MessageBox
  633. 				if ( mbWnd != IntPtr.Zero )
  634. 				{
  635. 					SendMessage( mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero );
  636. 				}
  637. 				_timeouttimer.Dispose( );
  638. 				timeoutelapsed = true;
  639. 			}
  640.  
  641.  
  642. 			const int WM_CLOSE = 0x0010;
  643.  
  644.  
  645. 			[System.Runtime.InteropServices.DllImport( "user32.dll", SetLastError = true )]
  646. 			static extern IntPtr FindWindow( string lpClassName, string lpWindowName );
  647.  
  648.  
  649. 			[System.Runtime.InteropServices.DllImport( "user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto )]
  650. 			static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam );
  651. 		}
  652.  
  653. 		#endregion Timed MessageBox
  654. 	}
  655. }
  656.  

page last uploaded: 2018-12-04, 10:47