Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for richtextmessagebox.cs

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

  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Timers;
  6. using System.Windows.Forms;
  7.  
  8.  
  9. namespace RobvanderWoude
  10. {
  11. 	class RichTextMessageBox
  12. 	{
  13. 		static readonly string progver = "1.00";
  14.  
  15.  
  16. 		#region Global Default Values
  17.  
  18. 		static readonly int defaultbuttonheight = 25;
  19. 		static readonly int defaultbuttonwidth = 100;
  20. 		static readonly string defaultfontfamily = "Sans-Serif";
  21. 		static readonly float defaultfontsize = 12;
  22. 		static readonly FontStyle defaultfontstyle = FontStyle.Regular;
  23. 		static readonly Color defaulttextcolor = Color.Black;
  24. 		static readonly string defaulttitle = string.Format( "RichTextMessageBox,  Version {0}", progver );
  25. 		static readonly int defaultwindowheight = 480;
  26. 		static readonly int defaultwindowwidth = 640;
  27. 		static readonly int screenheight = Screen.PrimaryScreen.Bounds.Height;
  28. 		static readonly int screenwidth = Screen.PrimaryScreen.Bounds.Width;
  29.  
  30. 		#endregion Global Default Values
  31.  
  32.  
  33. 		#region Global Variables
  34.  
  35. 		static Form rtmbform;
  36. 		static RichTextBox rtmbox;
  37. 		static string button1text = "OK";
  38. 		static string button2text = string.Empty;
  39. 		static string button3text = string.Empty;
  40. 		static string buttonclickedtext = "Cancel";
  41. 		static int buttonclickednumber = -1;
  42. 		static int buttoncount = 1;
  43. 		static int buttonheight = defaultbuttonheight;
  44. 		static int buttonwidth = defaultbuttonwidth;
  45. 		static int defaultbutton = -1;
  46. 		static string message = string.Empty;
  47. 		static double timeout = 0;
  48. 		static bool timeoutelapsed = false;
  49. 		static System.Timers.Timer timer;
  50. 		static int windowheight = defaultwindowheight;
  51. 		static int windowwidth = defaultwindowwidth;
  52.  
  53. 		#endregion Global Variables
  54.  
  55.  
  56. 		[STAThread]
  57. 		static int Main( string[] args )
  58. 		{
  59. 			#region Initial Values
  60.  
  61. 			string title = defaulttitle;
  62. 			int windowx = -1;
  63. 			int windowy = -1;
  64. 			string fontfamily = defaultfontfamily;
  65. 			float fontsize = defaultfontsize;
  66. 			FontStyle fontstyle = defaultfontstyle;
  67. 			bool showhelp = false;
  68. 			bool showintaskbar = false;
  69. 			Color textcolor = defaulttextcolor;
  70. 			bool topmost = false;
  71.  
  72. 			#endregion Initial Values
  73.  
  74.  
  75. 			#region Parse Command Line
  76.  
  77. 			if ( args.Length == 0 || args.Contains( "/?" ) )
  78. 			{
  79. 				showhelp = true;
  80. 			}
  81.  
  82. 			foreach ( string arg in args )
  83. 			{
  84. 				if ( arg.Length > 2 && ( arg[0] == '/' || arg[0] == '-' ) )
  85. 				{
  86. 					if ( arg.IndexOf( ':' ) > 1 )
  87. 					{
  88. 						string key = arg.ToUpper( ).Substring( 1, arg.IndexOf( ':' ) - 1 );
  89. 						string val = arg.Substring( arg.IndexOf( ':' ) + 1 );
  90. 						switch ( key )
  91. 						{
  92. 							case "B1": // /B1:"text for button 1"
  93. 								button1text = val;
  94. 								break;
  95. 							case "B2": // /B2:"text for button 2"
  96. 								button2text = val;
  97. 								buttoncount = 2;
  98. 								break;
  99. 							case "B3": // /B3:"text for button 3"
  100. 								button3text = val;
  101. 								buttoncount = 3;
  102. 								break;
  103. 							case "BH": // /BH:button_height
  104. 								if ( !int.TryParse( val, out buttonheight ) )
  105. 								{
  106. 									showhelp = true;
  107. 								}
  108. 								break;
  109. 							case "BW": // /BW:button_width
  110. 								if ( !int.TryParse( val, out buttonwidth ) )
  111. 								{
  112. 									showhelp = true;
  113. 								}
  114. 								break;
  115. 							case "C": // /C"text_color"
  116. 								try
  117. 								{
  118. 									textcolor = Color.FromName( val ); // if FromName does not recognize the value of val as color it will return black
  119. 								}
  120. 								catch ( Exception )
  121. 								{
  122. 									showhelp = true;
  123. 								}
  124. 								break;
  125. 							case "DB": // /DB:default_button (1 is always valid, 2 or 3 only if there are that many buttons)
  126. 								if ( !int.TryParse( val, out defaultbutton ) )
  127. 								{
  128. 									if ( val.ToUpper( ) == button1text.ToUpper( ) )
  129. 									{
  130. 										defaultbutton = 1;
  131. 									}
  132. 									else if ( val.ToUpper( ) == button2text.ToUpper( ) )
  133. 									{
  134. 										defaultbutton = 2;
  135. 									}
  136. 									else if ( val.ToUpper( ) == button3text.ToUpper( ) )
  137. 									{
  138. 										defaultbutton = 3;
  139. 									}
  140. 									else
  141. 									{
  142. 										showhelp = true;
  143. 									}
  144. 								}
  145. 								break;
  146. 							case "FONT":
  147. 								fontfamily = val;
  148. 								try
  149. 								{
  150. 									FontFamily testfont = new FontFamily( fontfamily );
  151. 								}
  152. 								catch
  153. 								{
  154. 									showhelp = true;
  155. 								}
  156. 								break;
  157. 							case "FS": // /FS:font_size
  158. 								if ( !float.TryParse( val, out fontsize ) )
  159. 								{
  160. 									showhelp = true;
  161. 								}
  162. 								break;
  163. 							case "T": // /T:timeout_seconds
  164. 								if ( !double.TryParse( val, out timeout ) )
  165. 								{
  166. 									showhelp = true;
  167. 								}
  168. 								timeout *= 1000; // specified in seconds, timer requires milliseconds
  169. 								break;
  170. 							case "WH": // /WH:window_height
  171. 								if ( !int.TryParse( val, out windowheight ) )
  172. 								{
  173. 									showhelp = true;
  174. 								}
  175. 								break;
  176. 							case "WW": // /WW:window_width
  177. 								if ( !int.TryParse( val, out windowwidth ) )
  178. 								{
  179. 									showhelp = true;
  180. 								}
  181. 								break;
  182. 							case "X": // /X:X_coordinate of upper left window corner
  183. 								if ( !int.TryParse( val, out windowx ) )
  184. 								{
  185. 									showhelp = true;
  186. 								}
  187. 								break;
  188. 							case "Y": // /Y:Y_coordinate of upper left window corner
  189. 								if ( !int.TryParse( val, out windowy ) )
  190. 								{
  191. 									showhelp = true;
  192. 								}
  193. 								break;
  194. 						}
  195. 					}
  196. 					else
  197. 					{
  198. 						switch ( arg.ToUpper( ).Substring( 1 ) )
  199. 						{
  200. 							case "ALWAYSONTOP":
  201. 							case "MODAL":
  202. 							case "TOPMOST":
  203. 								topmost = true;
  204. 								break;
  205. 							case "BOLD":
  206. 								fontstyle |= FontStyle.Bold;
  207. 								break;
  208. 							case "ITALIC":
  209. 							case "ITALICS":
  210. 								fontstyle |= FontStyle.Italic;
  211. 								break;
  212. 							case "SHOWINTASKBAR":
  213. 							case "TASKBAR":
  214. 								showintaskbar = true;
  215. 								break;
  216. 							case "STRIKE":
  217. 							case "STRIKEOUT":
  218. 								fontstyle |= FontStyle.Strikeout;
  219. 								break;
  220. 							case "UNDERLINE":
  221. 							case "UNDERLINED":
  222. 								fontstyle |= FontStyle.Underline;
  223. 								break;
  224. 							default:
  225. 								showhelp = true;
  226. 								break;
  227. 						}
  228. 					}
  229. 				}
  230. 				else
  231. 				{
  232. 					if ( string.IsNullOrWhiteSpace( message ) )
  233. 					{
  234. 						message = arg; // the message to be displayed in the dialog
  235. 					}
  236. 					else if ( string.IsNullOrWhiteSpace( title ) )
  237. 					{
  238. 						title = arg; // the title of the dialog window
  239. 					}
  240. 					else
  241. 					{
  242. 						showhelp = true;
  243. 					}
  244. 				}
  245. 			}
  246.  
  247. 			#endregion Parse Command Line
  248.  
  249.  
  250. 			#region Validate Command Line Settings
  251.  
  252. 			// Check mandatory button text, and if default button number is valid
  253. 			if ( string.IsNullOrWhiteSpace( message ) )
  254. 			{
  255. 				showhelp = true;
  256. 			}
  257.  
  258. 			if ( string.IsNullOrWhiteSpace( button1text ) )
  259. 			{
  260. 				showhelp = true;
  261. 			}
  262.  
  263. 			if ( string.IsNullOrWhiteSpace( button2text ) && defaultbutton > 1 )
  264. 			{
  265. 				showhelp = true;
  266. 			}
  267.  
  268. 			if ( string.IsNullOrWhiteSpace( button3text ) && defaultbutton > 2 )
  269. 			{
  270. 				showhelp = true;
  271. 			}
  272.  
  273. 			if ( showhelp )
  274. 			{
  275. 				ShowHelp( );
  276. 				// Restore defaults before showing help in GUI
  277. 				button1text = "OK";
  278. 				button2text = string.Empty;
  279. 				button3text = string.Empty;
  280. 				buttonheight = defaultbuttonheight;
  281. 				buttonwidth = defaultbuttonwidth;
  282. 				fontfamily = "Courier New";
  283. 				fontsize = 10; // slightly smaller font to fit in the help text
  284. 				fontstyle = defaultfontstyle;
  285. 				textcolor = defaulttextcolor;
  286. 				title = defaulttitle;
  287. 				windowheight = defaultwindowheight;
  288. 				windowwidth = defaultwindowwidth;
  289. 				windowx = Convert.ToInt32( ( screenwidth - defaultwindowwidth ) / 2 );
  290. 				windowy = Convert.ToInt32( ( screenheight - defaultwindowheight ) / 2 );
  291. 			}
  292. 			else
  293. 			{
  294. 				windowheight = Math.Min( windowheight, screenheight );
  295. 				windowwidth = Math.Min( windowwidth, screenwidth );
  296. 				if ( windowx == -1 )
  297. 				{
  298. 					windowx = Convert.ToInt32( ( screenwidth - windowwidth ) / 2 );
  299. 				}
  300. 				if ( windowy == -1 )
  301. 				{
  302. 					windowy = Convert.ToInt32( ( screenheight - windowheight ) / 2 );
  303. 				}
  304. 				windowx = Math.Min( windowx, screenwidth - windowwidth );
  305. 				windowy = Math.Min( windowy, screenheight - windowheight );
  306. 			}
  307.  
  308. 			#endregion Validate Command Line Settings
  309.  
  310.  
  311. 			#region Prepare Dialog Form
  312.  
  313. 			// The dialog form itself
  314. 			rtmbform = new Form
  315. 			{
  316. 				Text = title,
  317. 				ClientSize = new Size( windowwidth, windowheight ),
  318. 				Location = new Point( windowx, windowy ),
  319. 				MaximizeBox = false,
  320. 				SizeGripStyle = SizeGripStyle.Hide,
  321. 				ShowInTaskbar = showintaskbar,
  322. 				StartPosition = FormStartPosition.Manual,
  323. 				TopMost = topmost,
  324. 				WindowState = FormWindowState.Normal
  325. 			};
  326. 			rtmbform.BringToFront( );
  327.  
  328. 			// The rich text box
  329. 			rtmbox = new RichTextBox
  330. 			{
  331. 				Text = message,
  332. 				Height = ( windowheight - buttonheight - 30 ),
  333. 				Width = ( windowwidth - 20 ),
  334. 				Font = new Font( fontfamily, fontsize, fontstyle ),
  335. 				ForeColor = textcolor,
  336. 				Location = new Point( 10, 10 ),
  337. 				ReadOnly = true
  338. 			};
  339. 			rtmbform.Controls.Add( rtmbox );
  340.  
  341. 			// Button 1
  342. 			Button button1 = new Button
  343. 			{
  344. 				Text = button1text,
  345. 				Height = buttonheight,
  346. 				Width = buttonwidth,
  347. 				Location = ButtonLocation( 1 )
  348. 			};
  349. 			button1.Click += Button1_Click;
  350. 			rtmbform.Controls.Add( button1 );
  351. 			if ( defaultbutton == 1 )
  352. 			{
  353. 				//rtmbform.AcceptButton = button1;
  354. 				button1.Focus( );
  355. 			}
  356.  
  357. 			// Optional button 2
  358. 			if ( !string.IsNullOrWhiteSpace( button2text ) )
  359. 			{
  360. 				Button button2 = new Button
  361. 				{
  362. 					Text = button2text,
  363. 					Height = buttonheight,
  364. 					Width = buttonwidth,
  365. 					Location = ButtonLocation( 2 )
  366. 				};
  367. 				button2.Click += Button2_Click;
  368. 				rtmbform.Controls.Add( button2 );
  369. 				if ( defaultbutton == 2 )
  370. 				{
  371. 					//rtmbform.AcceptButton = button2;
  372. 					button2.Focus( );
  373. 				}
  374.  
  375. 				// Optional button 3, only if button 2 is also specified
  376. 				if ( !string.IsNullOrWhiteSpace( button3text ) )
  377. 				{
  378. 					Button button3 = new Button
  379. 					{
  380. 						Text = button3text,
  381. 						Height = buttonheight,
  382. 						Width = buttonwidth,
  383. 						Location = ButtonLocation( 3 )
  384. 					};
  385. 					button3.Click += Button3_Click;
  386. 					rtmbform.Controls.Add( button3 );
  387. 					if ( defaultbutton == 3 )
  388. 					{
  389. 						//rtmbform.AcceptButton = button3;
  390. 						button3.Focus( );
  391. 					}
  392. 				}
  393. 			}
  394.  
  395.  
  396. 			#endregion Prepare Dialog Form
  397.  
  398. 			// Optional timer for timeout feature
  399. 			if ( timeout > 0 )
  400. 			{
  401. 				timer = new System.Timers.Timer( );
  402. 				timer.Elapsed += new ElapsedEventHandler( Timer_Elapsed );
  403. 				timer.Interval = timeout;
  404. 				timer.Start( );
  405. 			}
  406.  
  407. 			// Show dialog window
  408. 			rtmbform.ShowDialog( );
  409.  
  410. 			// Interpret the result to be returned
  411. 			if ( timeoutelapsed )
  412. 			{
  413. 				buttonclickednumber = defaultbutton;
  414. 				switch ( defaultbutton )
  415. 				{
  416. 					case 1:
  417. 						buttonclickedtext = button1text;
  418. 						break;
  419. 					case 2:
  420. 						buttonclickedtext = button2text;
  421. 						break;
  422. 					case 3:
  423. 						buttonclickedtext = button3text;
  424. 						break;
  425. 					default:
  426. 						buttonclickedtext = "Timeout";
  427. 						buttonclickednumber = 4;
  428. 						break;
  429. 				}
  430. 			}
  431.  
  432. 			if ( showhelp )
  433. 			{
  434. 				return -1;
  435. 			}
  436. 			else
  437. 			{
  438. 				Console.WriteLine( buttonclickedtext );
  439. 				return buttonclickednumber;
  440. 			}
  441. 		}
  442.  
  443.  
  444. 		private static void Button1_Click( object sender, EventArgs e )
  445. 		{
  446. 			buttonclickedtext = button1text;
  447. 			buttonclickednumber = 1;
  448. 			rtmbform.Close( );
  449. 		}
  450.  
  451.  
  452. 		private static void Button2_Click( object sender, EventArgs e )
  453. 		{
  454. 			buttonclickedtext = button2text;
  455. 			buttonclickednumber = 2;
  456. 			rtmbform.Close( );
  457. 		}
  458.  
  459.  
  460. 		private static void Button3_Click( object sender, EventArgs e )
  461. 		{
  462. 			buttonclickedtext = button3text;
  463. 			buttonclickednumber = 2;
  464. 			rtmbform.Close( );
  465. 		}
  466.  
  467. 		public static void Timer_Elapsed( object sender, System.EventArgs e )
  468. 		{
  469. 			timeoutelapsed = true;
  470. 			Process.GetCurrentProcess( ).CloseMainWindow( );
  471. 		}
  472.  
  473.  
  474. 		private static Point ButtonLocation( int button )
  475. 		{
  476. 			Point location = new Point( );
  477. 			switch ( buttoncount )
  478. 			{
  479. 				case 1:
  480. 					location.X = ( windowwidth - buttonwidth ) / 2; // center
  481. 					break;
  482. 				case 2:
  483. 					if ( button == 1 )
  484. 					{
  485. 						location.X = windowwidth / 2 - buttonwidth - 10; // left
  486. 					}
  487. 					else
  488. 					{
  489. 						location.X = windowwidth / 2 + buttonwidth + 10; // right
  490. 					}
  491. 					break;
  492. 				case 3:
  493. 					if ( button == 1 )
  494. 					{
  495. 						location.X = ( windowwidth - buttonwidth ) / 2 - buttonwidth - 10; // left
  496. 					}
  497. 					else if ( button == 2 )
  498. 					{
  499. 						location.X = ( windowwidth - buttonwidth ) / 2; // center
  500. 					}
  501. 					else
  502. 					{
  503. 						location.X = ( windowwidth - buttonwidth ) / 2 + buttonwidth + 10; // right
  504. 					}
  505. 					break;
  506. 			}
  507. 			location.Y = windowheight - buttonheight - 10;
  508. 			return location;
  509. 		}
  510.  
  511.  
  512. 		static void ShowHelp( )
  513. 		{
  514. 			/*
  515. 			RichTextMessageBox,  Version 1.00
  516. 			Show a fully customizable message dialog and return which button is clicked
  517.  
  518. 			Usage:    RichTextMessageBox.exe  message  [ title ]  [ options ]
  519.  
  520. 			          message       is the text to be displayed in the dialog
  521. 			          title         is the dialog's window title
  522. 									(default: program name and version)
  523.  
  524. 			Options:  /AlwaysOnTop  Modal window, always on top
  525. 			          /B1:"caption" Caption for button 1 (default: OK)
  526. 			          /B2:"caption" Caption for button 2 (default: empty)
  527. 			          /B3:"caption" Caption for button 3 (default: empty)
  528. 			          /BH:height    Button height (default: 25)
  529. 			          /Bold         Bold text for message
  530. 			          /BW:width     Button width (default: 100)
  531. 			          /C:color      Text color for dialog (default: Black)
  532. 			          /DB:default   Default button (default: 1; values 2 or 3 are
  533. 			                        valid only if there are that many buttons)
  534. 			          /Font:name    Font family name (default: Sans-Serif)
  535. 			          /FS:fontsize  Font size (default: 12)
  536. 			          /Italic       Italic text for message
  537. 			          /Strike       Strikeout text
  538. 			          /T:seconds    Timeout in seconds (default: no timeout)
  539. 			          /Taskbar      Show in taskbar
  540. 			          /Underline    Underline text
  541. 			          /WH:height    Window height (default: 480)
  542. 			          /WW:width     Window width (default: 640)
  543. 			          /X:x          X-coordinate of upper left window corner
  544. 			          /Y:y          Y-coordinate of upper left window corner
  545. 			                        (default: center window on screen)
  546.  
  547. 			Notes:    The caption of the button that is clicked will be sent to the
  548. 			          console, the number of the button is returned as "errorlevel".
  549. 			          In case of errors, the "errorlevel" will be -1, if a timeout
  550. 			          elapsed and no default button was specified, the "errorlevel"
  551. 			          will be 4, and the text "Timeout" is sent to the console.
  552. 			          If an invalid text color is specified, it will be ignored.
  553.  
  554. 			Written by Rob van der Woude
  555. 			https://www.robvanderwoude.com/
  556. 			*/
  557.  
  558. 			message = string.Format( "RichTextMessageBox,  Version {0}\n", progver );
  559. 			message += "Show a fully customizable message dialog and return which button is clicked\n\n";
  560. 			message += "Usage:    RichTextMessageBox.exe  message  [ title ]  [ options ]\n\n";
  561. 			message += "          message       is the text to be displayed in the dialog\n";
  562. 			message += "          title         is the dialog's window title\n";
  563. 			message += "                        (default: program name and version)\n\n";
  564. 			message += "Options:  /AlwaysOnTop  Modal window, always on top\n";
  565. 			message += "          /B1:\"caption\" Caption for button 1 (default: OK)\n";
  566. 			message += "          /B2:\"caption\" Caption for button 2 (default: empty)\n";
  567. 			message += "          /B3:\"caption\" Caption for button 3 (default: empty)\n";
  568. 			message += string.Format( "          /BH:height    Button height (default: {0})\n", defaultbuttonheight );
  569. 			message += "          /Bold         Bold text for message\n";
  570. 			message += string.Format( "          /BW:width     Button width (default: {0})\n", defaultbuttonwidth );
  571. 			message += "          /C:color      Text color for dialog (default: Black)\n";
  572. 			message += "          /DB:default   Default button (default: 1; values 2 or 3 are\n";
  573. 			message += "                        valid only if there are that many buttons)\n";
  574. 			message += string.Format( "          /Font:name    Font family name (default: {0})\n", defaultfontfamily );
  575. 			message += string.Format( "          /FS:fontsize  Font size (default: {0})\n", defaultfontsize );
  576. 			message += "          /Italic       Italic text for message\n";
  577. 			message += "          /Strike       Strikeout text\n";
  578. 			message += "          /T:seconds    Timeout in seconds (default: no timeout)\n";
  579. 			message += "          /Taskbar      Show in taskbar\n";
  580. 			message += "          /Underline    Underline text\n";
  581. 			message += string.Format( "          /WH:height    Window height (default: {0})\n", defaultwindowheight );
  582. 			message += string.Format( "          /WW:width     Window width (default: {0})\n", defaultwindowwidth );
  583. 			message += "          /X:x          X-coordinate of upper left window corner\n";
  584. 			message += "          /Y:y          Y-coordinate of upper left window corner\n";
  585. 			message += "                        (default: center window on screen)\n\n";
  586. 			message += "Notes:    The caption of the button that is clicked will be sent to the\n";
  587. 			message += "          console, the number of the button is returned as \"errorlevel\".\n";
  588. 			message += "          In case of errors, the \"errorlevel\" will be -1, if a timeout\n";
  589. 			message += "          elapsed and no default button was specified, the \"errorlevel\"\n";
  590. 			message += "          will be 4, and the text \"Timeout\" is sent to the console.\n";
  591. 			message += "          If an invalid text color is specified, it will be ignored.\n\n";
  592. 			message += "Written by Rob van der Woude\n";
  593. 			message += "https://www.robvanderwoude.com/";
  594.  
  595. 			Console.Error.WriteLine( message );
  596. 		}
  597. 	}
  598. }
  599.  

page last uploaded: 2019-01-21, 22:48