Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for dialogboxes_common.cs

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

  1. using System.Linq;
  2.  
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	[System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.None )]
  7. 	[System.Runtime.InteropServices.ComVisible( false )]
  8. 	public class Global
  9. 	{
  10. 		[System.Runtime.InteropServices.ComVisible( false )]
  11. 		public class Common
  12. 		{
  13. 			public static int[] BorderDimensions( )
  14. 			{
  15. 				System.Windows.Forms.Form testform = new System.Windows.Forms.Form( );
  16. 				System.Drawing.Size testsize = new System.Drawing.Size( 300, 200 );
  17. 				testform.Size = testsize;
  18. 				int deltaX = testform.Size.Width - testform.ClientSize.Width + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
  19. 				int deltaY = testform.Size.Height - testform.ClientSize.Height + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
  20. 				testform.Dispose( );
  21. 				return new int[] { deltaX, deltaY };
  22. 			}
  23.  
  24.  
  25. 			// Available standard console colors;
  26. 			// do NOT change the order of these values,
  27. 			// even if it seems illogical at index 7 and 8
  28. 			public static int[] consolecolors = new int[] {
  29. 					0x000000, // 0 Black
  30. 					0x000080, // 1 DarkBlue
  31. 					0x008000, // 2 DarkGreen
  32. 					0x008080, // 3 DarkCyan
  33. 					0x800000, // 4 DarkRed
  34. 					0x800080, // 5 DarkMagenta
  35. 					0x808000, // 6 DarkYellow
  36. 					0xC0C0C0, // 7 Gray
  37. 					0x808080, // 8 DarkGray
  38. 					0x0000FF, // 9 Blue
  39. 					0x00FF00, // A Green
  40. 					0x00FFFF, // B Cyan
  41. 					0xFF0000, // C Red
  42. 					0xFF00FF, // D Magenta
  43. 					0xFFFF00, // E Yellow
  44. 					0xFFFFFF  // F White
  45. 			};
  46.  
  47.  
  48. 			public static string Credits( )
  49. 			{
  50. 				string credits = "Credits";
  51. 				credits += "\n" + new string( '\u2550', credits.Length ) + "\n\n";
  52.  
  53. 				credits += "Building COM Objects in C# by Mubbasher Adeel:\n";
  54. 				credits += "https://www.codeproject.com/Articles/7859/Building-COM-Objects-in-C\n\n";
  55.  
  56. 				credits += "On-the-fly forms by Gorkem Gencay on StackOverflow.com:\n";
  57. 				credits += "http://stackoverflow.com/questions/97097#17546909\n\n";
  58.  
  59. 				credits += "Localize captions by Martin Stoeckli:\n";
  60. 				credits += "http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources\n\n";
  61.  
  62. 				credits += "Extract icons from Shell32.dll by Thomas Levesque on StackOverflow.com:\n";
  63. 				credits += "http://stackoverflow.com/questions/6873026\n\n";
  64.  
  65. 				credits += "Get ISO week of year by \"il_guru\" on StackOverflow.com:\n";
  66. 				credits += "https://stackoverflow.com/a/11155102\n\n";
  67.  
  68. 				credits += "Check if a font is a fixed pitch font, tip from Hans Passant on StackOverflow.com:\n";
  69. 				credits += "https://stackoverflow.com/q/21965321\n\n";
  70.  
  71. 				credits += "Extension method to limit number to range by \"dtb\" on StackOverflow.com:\n";
  72. 				credits += "https://stackoverflow.com/a/3176628\n\n";
  73.  
  74. 				credits += "Mask language table by Microsoft:\n";
  75. 				credits += "http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx\n\n";
  76.  
  77. 				credits += "And last but not least, thanks for Wolfgang Struensee,\n";
  78. 				credits += "for testing the many pre-release versions of this DLL\n\n";
  79.  
  80. 				return credits;
  81. 			}
  82.  
  83.  
  84. 			public static string GetCultureFromDateTimeString( string datetimestring )
  85. 			{
  86. 				System.Collections.Generic.List<string> result = new System.Collections.Generic.List<string>( );
  87. 				foreach ( System.Globalization.CultureInfo cultureinfo in System.Globalization.CultureInfo.GetCultures( System.Globalization.CultureTypes.AllCultures ) )
  88. 				{
  89. 					try
  90. 					{
  91. 						System.DateTime test = System.DateTime.Parse( datetimestring, cultureinfo );
  92. 						result.Add( string.Format( "{0}    {1,-40}    {2}", cultureinfo.Name, cultureinfo.DisplayName, System.DateTime.Now.ToString( cultureinfo ) ) );
  93. 					}
  94. 					catch { };
  95. 				}
  96. 				result.Sort( );
  97. 				return result.Count.ToString( ) + "\n\n" + string.Join( "\n", result.ToArray( ) );
  98. 			}
  99.  
  100.  
  101. 			public static int GetIso8601WeekOfYear( System.DateTime date )
  102. 			{
  103. 				// This presumes that weeks start with Monday.
  104. 				// Week 1 is the 1st week of the year with a Thursday in it.
  105. 				// Code by "il_guru" on StackOverflow.com:
  106. 				// https://stackoverflow.com/a/11155102
  107. 				//
  108. 				// Seriously cheat.  If its Monday, Tuesday or Wednesday, then it'll be the same
  109. 				// week# as whatever Thursday, Friday or Saturday are, and we always get those right
  110. 				System.DayOfWeek day = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetDayOfWeek( date );
  111. 				if ( day >= System.DayOfWeek.Monday && day <= System.DayOfWeek.Wednesday )
  112. 				{
  113. 					date = date.AddDays( 3 );
  114. 				}
  115. 				// Return the week of our adjusted day
  116. 				return System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear( date, System.Globalization.CalendarWeekRule.FirstFourDayWeek, System.DayOfWeek.Monday );
  117. 			}
  118.  
  119.  
  120. 			public static System.Globalization.DateTimeFormatInfo GetDateTimeFormat( string culture ) => System.Globalization.CultureInfo.GetCultureInfo( culture ).DateTimeFormat;
  121.  
  122.  
  123. 			public static bool TestIfRegexPattern( string pattern )
  124. 			{
  125. 				if ( string.IsNullOrWhiteSpace( pattern ) )
  126. 				{
  127. 					return false;
  128. 				}
  129. 				try
  130. 				{
  131. 					return System.Text.RegularExpressions.Regex.IsMatch( "", pattern );
  132. 				}
  133. 				catch ( System.ArgumentException )
  134. 				{
  135. 					return false;
  136. 				}
  137. 			}
  138.  
  139.  
  140. 			public static string TimeStamp( )
  141. 			{
  142. 				return System.DateTime.Now.ToString( "[yyyy-MM-dd HH:mm:ss.fff] " );
  143. 			}
  144.  
  145.  
  146. 			[System.Runtime.InteropServices.ComVisible( false )]
  147. 			public class Help
  148. 			{
  149. 				public static string _checkupdate = "Check the developer's website for updates";
  150.  
  151. 				public static string _credits = "Shows credits";
  152.  
  153. 				public static string _help = "Returns this help text as plain text, or if optional parameter equals 1, as html";
  154.  
  155. 				public static string _listproperties = "Returns a list of this class' properties as key=value pairs (1 per line)";
  156.  
  157. 				public static string _samplecode = "Returns VBScript sample code for this dialog";
  158.  
  159. 				public static string captioncancel = "Caption for \"Cancel\" button";
  160.  
  161. 				public static string captionok = "Caption for \"OK\" button";
  162.  
  163. 				public static string debuginfo = "A log of actions for debugging";
  164.  
  165. 				public static string errors = "List of errors with current settings";
  166.  
  167. 				public static string fixedpitchonly = "Allow only monospaced fonts      (0=false; 1=true)";
  168.  
  169. 				public static string fontfamily = "Font family used in dialog";
  170.  
  171. 				public static string fontsize = "Font size used in dialog";
  172.  
  173. 				public static string left = "Offset of window from left screen border";
  174.  
  175. 				public static string literal = "Treat \"prompt\" as literal string (0=false; 1=true)";
  176.  
  177. 				public static string localizecaptions = "Use localized button captions    (0=false; 1=true)";
  178.  
  179. 				public static string modal = "Modal dialog, i.e. always on top (0=false; 1=true)";
  180.  
  181. 				public static string prompt = "Optional text above controls";
  182.  
  183. 				public static string timeout = "Optional dialog timeout in seconds  (0=no timeout)";
  184.  
  185. 				public static string timeoutelapsed = "Timeout elapsed status           (0=false; 1=true)";
  186.  
  187. 				public static string title = "Dialog window title";
  188.  
  189. 				public static string top = "Offset of window from top of screen";
  190.  
  191. 				public static string version = "Show this DLL's version";
  192.  
  193. 				public static string windowheight = "Height of dialog window";
  194.  
  195. 				public static string windowwidth = "Width of dialog window";
  196.  
  197. 				public static System.Collections.Generic.List<string> HelpTableCell( string celltext, int textwidth )
  198. 				{
  199. 					System.Collections.Generic.List<string> colcells = new System.Collections.Generic.List<string>( );
  200. 					string[] textlines = celltext.Split( "\n".ToCharArray( ) );
  201. 					foreach ( string line in textlines )
  202. 					{
  203. 						string textline = line;
  204. 						while ( textline.Length > 0 )
  205. 						{
  206. 							if ( textline.Length <= textwidth )
  207. 							{
  208. 								colcells.Add( textline );
  209. 								textline = string.Empty;
  210. 							}
  211. 							else
  212. 							{
  213. 								int lastspace = textline.Substring( 0, textwidth ).LastIndexOf( ' ' );
  214. 								int lastsemicolon = textline.Substring( 0, textwidth ).LastIndexOf( ';' );
  215. 								int lastcomma = textline.Substring( 0, textwidth ).LastIndexOf( ',' );
  216. 								int lasthyphen = textline.Substring( 0, textwidth ).LastIndexOf( '-' );
  217. 								if ( lastspace > -1 )
  218. 								{
  219. 									colcells.Add( textline.Substring( 0, lastspace ).Trim( " \t\n\r".ToCharArray( ) ) );
  220. 									textline = textline.Substring( lastspace ).Trim( " \t\n\r".ToCharArray( ) );
  221. 								}
  222. 								else if ( lastsemicolon > -1 )
  223. 								{
  224. 									colcells.Add( textline.Substring( 0, lastsemicolon ).Trim( " \t\n\r".ToCharArray( ) ) );
  225. 									textline = textline.Substring( lastsemicolon ).Trim( " \t\n\r".ToCharArray( ) );
  226. 								}
  227. 								else if ( lastcomma > -1 )
  228. 								{
  229. 									colcells.Add( textline.Substring( 0, lastcomma ).Trim( " \t\n\r".ToCharArray( ) ) );
  230. 									textline = textline.Substring( lastcomma ).Trim( " \t\n\r".ToCharArray( ) );
  231. 								}
  232. 								else if ( lasthyphen > -1 )
  233. 								{
  234. 									colcells.Add( textline.Substring( 0, lasthyphen ).Trim( " \t\n\r".ToCharArray( ) ) );
  235. 									textline = textline.Substring( lasthyphen ).Trim( " \t\n\r".ToCharArray( ) );
  236. 								}
  237. 								else
  238. 								{
  239. 									colcells.Add( textline.Substring( 0, textwidth ).Trim( " \t\n\r".ToCharArray( ) ) );
  240. 									textline = textline.Substring( textwidth ).Trim( " \t\n\r".ToCharArray( ) );
  241. 								}
  242. 							}
  243. 						}
  244. 					}
  245. 					return colcells;
  246. 				}
  247.  
  248. 				public static string HelpTableRowHTML( string col1text, string col2text, string col3text )
  249. 				{
  250. 					string row = "<tr>\n";
  251. 					row += string.Format( "\t<td>{0}</td>\n", col1text );
  252. 					row += string.Format( "\t<td>{0}</td>\n", col2text );
  253. 					row += string.Format( "\t<td>{0}</td>\n", col3text );
  254. 					row += "</tr>\n";
  255. 					return row;
  256. 				}
  257.  
  258. 				public static string HelpTableRowHTML( string col1text, string col2text, string col3text, string col4text, string col5text )
  259. 				{
  260. 					string row = "<tr>\n";
  261. 					row += string.Format( "\t<td>{0}</td>\n", col1text );
  262. 					row += string.Format( "\t<td>{0}</td>\n", col2text );
  263. 					row += string.Format( "\t<td>{0}</td>\n", col3text );
  264. 					row += string.Format( "\t<td>{0}</td>\n", col4text );
  265. 					row += string.Format( "\t<td>{0}</td>\n", col5text );
  266. 					row += "</tr>\n";
  267. 					return row;
  268. 				}
  269.  
  270. 				public static string HelpTableRowText( string template, string col1text, string col2text, string col3text, string col4text, string col5text )
  271. 				{
  272. 					string row = string.Empty;
  273. 					int rows = 0;
  274. 					int columns = 5;
  275. 					int[] colwidth = { 0, 0, 0, 0, 0 };
  276. 					string[] coltext = { col1text, col2text, col3text, col4text, col5text };
  277.  
  278. 					// Parse the linetemplate string to get the columns' widths
  279. 					string pattern = @"\{(\d+),-?(\d+)\}";
  280. 					System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( pattern );
  281. 					System.Text.RegularExpressions.MatchCollection matches = regex.Matches( template );
  282. 					//string text = string.Empty;
  283. 					foreach ( System.Text.RegularExpressions.Match match in matches )
  284. 					{
  285. 						System.Text.RegularExpressions.GroupCollection groups = match.Groups;
  286. 						colwidth[int.Parse( groups[1].Value )] = int.Parse( groups[2].Value );
  287. 					}
  288. 					for ( int i = 0; i < columns; i++ )
  289. 					{
  290. 						if ( colwidth[i] == 0 )
  291. 						{
  292. 							columns = i;
  293. 							break;
  294. 						}
  295. 					}
  296.  
  297. 					// Wrap strings containing newlines and/or lines that are too long for their column
  298. 					System.Collections.Generic.List<string> col1cells = new System.Collections.Generic.List<string>( );
  299. 					if ( columns >= 1 )
  300. 					{
  301. 						col1cells = HelpTableCell( coltext[0], colwidth[0] );
  302. 						rows = System.Math.Max( rows, col1cells.Count );
  303. 					}
  304.  
  305. 					System.Collections.Generic.List<string> col2cells = new System.Collections.Generic.List<string>( );
  306. 					if ( columns >= 2 )
  307. 					{
  308. 						col2cells = HelpTableCell( coltext[1], colwidth[1] );
  309. 						rows = System.Math.Max( rows, col2cells.Count );
  310. 					}
  311.  
  312. 					System.Collections.Generic.List<string> col3cells = new System.Collections.Generic.List<string>( );
  313. 					if ( columns >= 3 )
  314. 					{
  315. 						col3cells = HelpTableCell( coltext[2], colwidth[2] );
  316. 						rows = System.Math.Max( rows, col3cells.Count );
  317. 					}
  318.  
  319. 					System.Collections.Generic.List<string> col4cells = new System.Collections.Generic.List<string>( );
  320. 					if ( columns >= 4 )
  321. 					{
  322. 						col4cells = HelpTableCell( coltext[3], colwidth[3] );
  323. 						rows = System.Math.Max( rows, col4cells.Count );
  324. 					}
  325.  
  326. 					System.Collections.Generic.List<string> col5cells = new System.Collections.Generic.List<string>( );
  327. 					if ( columns >= 5 )
  328. 					{
  329. 						col5cells = HelpTableCell( coltext[4], colwidth[4] );
  330. 						rows = System.Math.Max( rows, col5cells.Count );
  331. 					}
  332.  
  333. 					for ( int i = 0; i < rows; i++ )
  334. 					{
  335. 						col1cells.Add( "" );
  336. 						col2cells.Add( "" );
  337. 						col3cells.Add( "" );
  338. 						col4cells.Add( "" );
  339. 						col5cells.Add( "" );
  340. 						row += string.Format( template, col1cells[i], col2cells[i], col3cells[i], col4cells[i], col5cells[i] );
  341. 					}
  342.  
  343. 					return row;
  344. 				}
  345. 			}
  346.  
  347.  
  348. 			[System.Runtime.InteropServices.ComVisible( false )]
  349. 			public class Lists
  350. 			{
  351. 				public static System.Collections.Generic.List<string> ConsoleColors( )
  352. 				{
  353. 					System.Collections.Generic.List<string> consolecolors = new System.Collections.Generic.List<string>( );
  354. 					foreach ( int color in typeof( ConsoleColors ).GetEnumValues( ) )
  355. 					{
  356. 						string name = typeof( ConsoleColors ).GetEnumName( color );
  357. 						consolecolors.Add( name + "=0X" + color.ToString( "X6" ) );
  358. 					}
  359. 					consolecolors.Sort( );
  360. 					return consolecolors;
  361. 				}
  362.  
  363.  
  364. 				public static System.Collections.Generic.List<string> Cultures( )
  365. 				{
  366. 					return System.Globalization.CultureInfo.GetCultures( System.Globalization.CultureTypes.AllCultures ).Select( c => c.Name ).ToList<string>( );
  367. 				}
  368.  
  369.  
  370. 				public static System.Collections.Generic.List<string> KnownColors( )
  371. 				{
  372. 					System.Collections.Generic.List<string> allcolors = new System.Collections.Generic.List<string>( );
  373. 					System.Drawing.KnownColor[] colors = (System.Drawing.KnownColor[]) System.Enum.GetValues( typeof( System.Drawing.KnownColor ) );
  374. 					foreach ( System.Drawing.KnownColor knowncolor in colors )
  375. 					{
  376. 						System.Drawing.Color color = System.Drawing.Color.FromKnownColor( knowncolor );
  377. 						allcolors.Add( string.Format( "{0}=0x{1:X6}", color.Name, (uint) color.ToArgb( ) % 0x01000000 ) );
  378. 					}
  379. 					allcolors.Sort( );
  380. 					return allcolors;
  381. 				}
  382.  
  383.  
  384. 				public static string MaskLanguageTable( )
  385. 				{
  386. 					string text = string.Empty;
  387. 					string separatorline = "\u251C" + new string( '\u2500', 12 ) + "\u253C" + new string( '\u2500', 82 ) + "\u2524\n";
  388. 					string linetemplate = "\u2502 {0,-10} \u2502 {1,-80} \u2502\n";
  389.  
  390. 					text = "System.Windows.Forms.MaskedTextBox.Mask Property";
  391. 					text += "\n" + new string( '\u2550', text.Length ) + "\n\n";
  392. 					text += "\u250C" + new string( '\u2500', 12 ) + "\u252C" + new string( '\u2500', 82 ) + "\u2510\n";
  393. 					text += string.Format( linetemplate, "Masking", string.Empty );
  394. 					text += string.Format( linetemplate, "element", "Description" );
  395. 					text += "\u255E" + new string( '\u2550', 12 ) + "\u256A" + new string( '\u2550', 82 ) + "\u2561\n";
  396. 					text += string.Format( linetemplate, "0", "Digit, required. This element will accept any single digit between 0 and 9." );
  397. 					text += separatorline;
  398. 					text += string.Format( linetemplate, "9", "Digit or space, optional." );
  399. 					text += separatorline;
  400. 					text += string.Format( linetemplate, "#", "Digit or space, optional. If this position is blank in the mask, it will be" );
  401. 					text += string.Format( linetemplate, " ", "rendered as a space in the Text property. Plus (+) and minus (-) signs allowed." );
  402. 					text += separatorline;
  403. 					text += string.Format( linetemplate, "L", "Letter, required. Restricts input to the ASCII letters a-z and A-Z." );
  404. 					text += string.Format( linetemplate, " ", "This mask element is equivalent to [a-zA-Z] in regular expressions." );
  405. 					text += separatorline;
  406. 					text += string.Format( linetemplate, "?", "Letter, optional. Restricts input to the ASCII letters a-z and A-Z." );
  407. 					text += string.Format( linetemplate, " ", "This mask element is equivalent to [a-zA-Z]? in regular expressions." );
  408. 					text += separatorline;
  409. 					text += string.Format( linetemplate, "&", "Character, required. Any non-control character." );
  410. 					text += string.Format( linetemplate, " ", "If ASCII only is set, this element behaves like the \"A\" element." );
  411. 					text += separatorline;
  412. 					text += string.Format( linetemplate, "C", "Character, optional. Any non-control character." );
  413. 					text += string.Format( linetemplate, " ", "If ASCII only is set, this element behaves like the \"a\" element." );
  414. 					text += separatorline;
  415. 					text += string.Format( linetemplate, "A", "Alphanumeric, required. If ASCII only is set, the only characters it will accept" );
  416. 					text += string.Format( linetemplate, " ", "are the ASCII letters a-z and A-Z and numbers. This mask element behaves like" );
  417. 					text += string.Format( linetemplate, " ", "the \"&\" element." );
  418. 					text += separatorline;
  419. 					text += string.Format( linetemplate, "a", "Alphanumeric, optional. If ASCII only is set, the only characters it will accept" );
  420. 					text += string.Format( linetemplate, " ", "are the ASCII letters a-z and A-Z and numbers. This mask element behaves like" );
  421. 					text += string.Format( linetemplate, " ", "the \"C\" element." );
  422. 					text += separatorline;
  423. 					text += string.Format( linetemplate, ".", "Decimal placeholder." );
  424. 					text += separatorline;
  425. 					text += string.Format( linetemplate, ",", "Thousands placeholder." );
  426. 					text += separatorline;
  427. 					text += string.Format( linetemplate, ":", "Time separator." );
  428. 					text += separatorline;
  429. 					text += string.Format( linetemplate, "/", "Date separator." );
  430. 					text += separatorline;
  431. 					text += string.Format( linetemplate, "$", "Currency symbol." );
  432. 					text += separatorline;
  433. 					text += string.Format( linetemplate, "<", "Shift down. Converts all characters that follow to lowercase." );
  434. 					text += separatorline;
  435. 					text += string.Format( linetemplate, ">", "Shift up. Converts all characters that follow to uppercase." );
  436. 					text += separatorline;
  437. 					text += string.Format( linetemplate, "|", "Disable a previous shift up or shift down." );
  438. 					text += separatorline;
  439. 					text += string.Format( linetemplate, "\\", "Escape. Escapes a mask character, turning it into a literal." );
  440. 					text += string.Format( linetemplate, " ", "\"\\\\\" is the escape sequence for a backslash." );
  441. 					text += separatorline;
  442. 					text += string.Format( linetemplate, "All other", "Literals. All non-mask elements will appear as themselves within MaskedTextBox." );
  443. 					text += string.Format( linetemplate, "characters", "Literals always occupy a static position in the mask at run time, and cannot be" );
  444. 					text += string.Format( linetemplate, " ", "moved or deleted by the user." );
  445. 					text += "\u2514" + new string( '\u2500', 12 ) + "\u2534" + new string( '\u2500', 82 ) + "\u2518\n\n";
  446. 					text += "Source: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.maskedtextbox.mask\n";
  447.  
  448. 					return text;
  449. 				}
  450.  
  451.  
  452. 				public static string MessageBoxButtons( )
  453. 				{
  454. 					return string.Join( ";", System.Enum.GetNames( typeof( System.Windows.Forms.MessageBoxButtons ) ) );
  455. 				}
  456.  
  457.  
  458. 				public static string MessageBoxIcons( )
  459. 				{
  460. 					return string.Join( ";", System.Enum.GetNames( typeof( System.Windows.Forms.MessageBoxIcon ) ) );
  461. 				}
  462.  
  463.  
  464. 				public static string MessageBoxOptions( )
  465. 				{
  466. 					return string.Join( ";", System.Enum.GetNames( typeof( System.Windows.Forms.MessageBoxOptions ) ) );
  467. 				}
  468.  
  469.  
  470. 				public static string Properties( object obj )
  471. 				{
  472. 					System.Collections.Generic.List<string> result = new System.Collections.Generic.List<string>( );
  473. 					System.Reflection.PropertyInfo[] propertyinfo = obj.GetType( ).GetProperties( );
  474. 					for ( int i = 0; i < propertyinfo.Length; i++ )
  475. 					{
  476. 						string propertyname = propertyinfo[i].Name;
  477. 						string propertyvalue = obj.GetType( ).GetProperty( propertyinfo[i].Name ).GetValue( obj: obj, index: null ).ToString( );
  478. 						result.Add( string.Format( "{0} = {1}", propertyname, propertyvalue.ToString( ) ) );
  479. 					}
  480. 					result.Sort( );
  481. 					return string.Join( System.Environment.NewLine, result.ToArray( ) );
  482. 				}
  483.  
  484.  
  485. 				public static System.Collections.Generic.List<string> SpecialFolders( )
  486. 				{
  487. 					System.Collections.Generic.List<string> specialfolders = new System.Collections.Generic.List<string>( );
  488. 					foreach ( System.Environment.SpecialFolder sf in typeof( System.Environment.SpecialFolder ).GetEnumValues( ) )
  489. 					{
  490. 						if ( !specialfolders.Contains( sf.ToString( ) ) )
  491. 						{
  492. 							specialfolders.Add( sf.ToString( ) );
  493. 						}
  494. 					}
  495. 					specialfolders.Sort( );
  496. 					return specialfolders;
  497. 				}
  498.  
  499.  
  500. 				public static string ToolTipIcons( )
  501. 				{
  502. 					return string.Join( ";", System.Enum.GetNames( typeof( System.Windows.Forms.ToolTipIcon ) ) );
  503. 				}
  504. 			}
  505.  
  506.  
  507. 			[System.Runtime.InteropServices.ComVisible( false )]
  508. 			public class Localization
  509. 			{
  510. 				public static string Caption( uint resource )
  511. 				{
  512. 					return Load( "user32.dll", resource, "" ).Replace( "&", "" );
  513. 				}
  514.  
  515.  
  516. 				public static string CaptionsTable( )
  517. 				{
  518. 					string text = "Localized Captions Table";
  519. 					text += "\n" + new string( '\u2550', text.Length ) + "\n\n";
  520.  
  521. 					string separatorline = "\u251C" + new string( '\u2500', 10 ) + "\u253C" + new string( '\u2500', 34 ) + "\u2524\n";
  522. 					string linetemplate = "\u2502 {0,8} \u2502 {1,-32} \u2502\n";
  523.  
  524. 					text += "\u250C" + new string( '\u2500', 10 ) + "\u252C" + new string( '\u2500', 34 ) + "\u2510\n";
  525. 					text += string.Format( linetemplate, "Resource", "Caption" );
  526. 					text += "\u255E" + new string( '\u2550', 10 ) + "\u256A" + new string( '\u2550', 34 ) + "\u2561\n";
  527.  
  528. 					for ( uint i = 800; i < 1000; i++ )
  529. 					{
  530. 						string caption = Load( "user32.dll", i, "" ).Replace( "&", "" );
  531. 						if ( !string.IsNullOrWhiteSpace( caption ) )
  532. 						{
  533. 							if ( i != 800 )
  534. 							{
  535. 								text += separatorline;
  536. 							}
  537. 							text += string.Format( linetemplate, i, caption );
  538. 						}
  539. 					}
  540. 					text += "\u2514" + new string( '\u2500', 10 ) + "\u2534" + new string( '\u2500', 34 ) + "\u2518\n\n";
  541.  
  542. 					text += "Code to retrieve localized captions by Martin Stoeckli;\n";
  543. 					text += "http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources\n";
  544.  
  545. 					return text;
  546. 				}
  547.  
  548.  
  549. 				[System.Runtime.InteropServices.DllImport( "kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto )]
  550. 				private static extern System.IntPtr GetModuleHandle( string lpModuleName );
  551.  
  552.  
  553. 				// Code to retrieve localized captions by Martin Stoeckli
  554. 				// http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources
  555. 				/// <summary>
  556. 				/// Searches for a text resource in a Windows library.
  557. 				/// Sometimes, using the existing Windows resources, you can make your code
  558. 				/// language independent and you don't have to care about translation problems.
  559. 				/// </summary>
  560. 				/// <example>
  561. 				///   btnCancel.Text = Load("user32.dll", 801, "Cancel");
  562. 				///   btnYes.Text = Load("user32.dll", 805, "Yes");
  563. 				/// </example>
  564. 				/// <param name="libraryName">Name of the windows library like "user32.dll"
  565. 				/// or "shell32.dll"</param>
  566. 				/// <param name="ident">Id of the string resource.</param>
  567. 				/// <param name="defaultText">Return this text, if the resource string could
  568. 				/// not be found.</param>
  569. 				/// <returns>Requested string if the resource was found,
  570. 				/// otherwise the <paramref name="defaultText"/></returns>
  571. 				public static string Load( string libraryName, uint ident, string defaultText )
  572. 				{
  573. 					System.IntPtr libraryHandle = GetModuleHandle( libraryName );
  574. 					if ( libraryHandle != System.IntPtr.Zero )
  575. 					{
  576. 						System.Text.StringBuilder sb = new System.Text.StringBuilder( 1024 );
  577. 						int size = LoadString( libraryHandle, ident, sb, 1024 );
  578. 						if ( size > 0 )
  579. 							return sb.ToString( );
  580. 					}
  581. 					return defaultText;
  582. 				}
  583.  
  584.  
  585. 				[System.Runtime.InteropServices.DllImport( "user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto )]
  586. 				private static extern int LoadString( System.IntPtr hInstance, uint uID, System.Text.StringBuilder lpBuffer, int nBufferMax );
  587. 			}
  588.  
  589.  
  590. 			[System.Runtime.InteropServices.ComVisible( false )]
  591. 			public class ProgramInfo
  592. 			{
  593. 				private static string _path = System.Reflection.Assembly.GetAssembly( typeof( RobvanderWoude.DialogBoxes ) ).Location;
  594. 				public static string Path
  595. 				{
  596. 					get
  597. 					{
  598. 						return _path;
  599. 					}
  600. 				}
  601.  
  602.  
  603. 				private static System.Diagnostics.FileVersionInfo _progfileversioninfo = System.Diagnostics.FileVersionInfo.GetVersionInfo( _path );
  604. 				public static string FileName
  605. 				{
  606. 					get
  607. 					{
  608. 						return System.IO.Path.GetFileName( _progfileversioninfo.FileName );
  609. 					}
  610. 				}
  611. 				public static string FileVersion
  612. 				{
  613. 					get
  614. 					{
  615. 						return _progfileversioninfo.FileVersion;
  616. 					}
  617. 				}
  618. 				public static string Comment
  619. 				{
  620. 					get
  621. 					{
  622. 						return _progfileversioninfo.Comments;
  623. 					}
  624. 				}
  625. 				public static string Copyrights
  626. 				{
  627. 					get
  628. 					{
  629. 						return _progfileversioninfo.LegalCopyright;
  630. 					}
  631. 				}
  632.  
  633.  
  634. 				public static void CheckUpdate( )
  635. 				{
  636. 					System.Diagnostics.Process.Start( "https://www.robvanderwoude.com/dialogboxesdll.php?ver=" + FileVersion );
  637. 				}
  638. 			}
  639.  
  640.  
  641. 			[System.Runtime.InteropServices.ComVisible( false )]
  642. 			public class Validate
  643. 			{
  644. 				public static System.Drawing.Color ColorName( string testcolor, System.Drawing.Color defaultcolor )
  645. 				{
  646. 					if ( System.Enum.TryParse( testcolor, true, out System.Drawing.Color color ) )
  647. 					{
  648. 						return color;
  649. 					}
  650. 					else
  651. 					{
  652. 						ValidationErrors.Add( "ColorName", string.Format( "\"{0}\" is not recognized as a valid color name", testcolor ) );
  653. 						return defaultcolor;
  654. 					}
  655. 				}
  656.  
  657.  
  658. 				public static System.Drawing.Color ColorRGB( string testrgb, System.Drawing.Color defaultcolor )
  659. 				{
  660. 					System.Drawing.Color result = defaultcolor;
  661. 					bool found = false;
  662. 					string[] RGB = testrgb.Split( ',' );
  663. 					byte R = System.Convert.ToByte( System.Convert.ToInt32( RGB[0] ).LimitToRange( 0, 255 ) );
  664. 					byte G = System.Convert.ToByte( System.Convert.ToInt32( RGB[1] ).LimitToRange( 0, 255 ) );
  665. 					byte B = System.Convert.ToByte( System.Convert.ToInt32( RGB[2] ).LimitToRange( 0, 255 ) );
  666. 					foreach ( System.Drawing.Color color in System.Enum.GetValues( typeof( System.Drawing.Color ) ) )
  667. 					{
  668. 						if ( color.R == R && color.G == G && color.B == B )
  669. 						{
  670. 							result = color;
  671. 							found = true;
  672. 							break;
  673. 						}
  674.  
  675. 					}
  676. 					if ( !found )
  677. 					{
  678. 						ValidationErrors.Add( "ColorRGB", string.Format( "\"{0}\" is not recognized as a valid RGB value", testrgb ) );
  679. 					}
  680. 					return result;
  681. 				}
  682.  
  683.  
  684. 				public static FontCharSet FontCharSet( string testcharset )
  685. 				{
  686. 					if ( System.Enum.TryParse( testcharset, true, out FontCharSet charset ) )
  687. 					{
  688. 						return charset;
  689. 					}
  690. 					else
  691. 					{
  692. 						ValidationErrors.Add( "FontCharSet", string.Format( "\"{0}\" is not recognized as a valid FontCharSet", testcharset ) );
  693. 						return (FontCharSet) 0;
  694. 					}
  695. 				}
  696.  
  697.  
  698. 				public static System.Drawing.FontFamily FontFamily( string fontfamilyname, System.Drawing.FontFamily defaultfontfamily, bool monospaced = false )
  699. 				{
  700. 					try
  701. 					{
  702. 						if ( System.Enum.GetNames( typeof( System.Drawing.FontFamily ) ).Contains( fontfamilyname, System.StringComparer.InvariantCultureIgnoreCase ) )
  703. 						{
  704. 							System.Drawing.FontFamily suggestedfontfamily = new System.Drawing.Text.InstalledFontCollection( ).Families.Where( f => f.Name.Equals( fontfamilyname ) ).First( );
  705. 							bool ismonospaced = suggestedfontfamily.IsFixedPitch( );
  706. 							if ( ismonospaced == monospaced )
  707. 							{
  708. 								return suggestedfontfamily;
  709. 							}
  710. 							else
  711. 							{
  712. 								if ( monospaced )
  713. 								{
  714. 									ValidationErrors.Add( "FontFamily", string.Format( "\"{0}\" is not a fixed pitch font family, but fixed pitch was requested", fontfamilyname ) );
  715. 								}
  716. 								else
  717. 								{
  718. 									ValidationErrors.Add( "FontFamily", string.Format( "\"{0}\" is a fixed pitch font family, but fixed pitch was not requested", fontfamilyname ) );
  719. 								}
  720. 								return defaultfontfamily;
  721. 							}
  722. 						}
  723. 					}
  724. 					catch ( System.Exception e )
  725. 					{
  726. 						ValidationErrors.Add( "FontFamily", string.Format( "Error while trying to make sense of the specified font family name: " + e.Message ) );
  727. 					}
  728. 					ValidationErrors.Add( "FontFamily", string.Format( "\"{0}\" is not recognized as a valid font family name", fontfamilyname ) );
  729. 					return defaultfontfamily;
  730. 				}
  731.  
  732.  
  733. 				public static System.Drawing.FontStyle FontStyle( string fontstyle )
  734. 				{
  735. 					if ( System.Enum.TryParse( fontstyle, true, out System.Drawing.FontStyle style ) )
  736. 					{
  737. 						return style;
  738. 					}
  739. 					else
  740. 					{
  741. 						ValidationErrors.Add( "FontStyle", string.Format( "\"{0}\" is not recognized as a valid FontStyle", fontstyle ) );
  742. 						return System.Drawing.FontStyle.Regular;
  743. 					}
  744. 				}
  745.  
  746.  
  747. 				public static System.Collections.SortedList ValidationErrors = new System.Collections.SortedList( );
  748. 			}
  749. 		}
  750.  
  751.  
  752. 		[System.Runtime.InteropServices.ComVisible( false )]
  753. 		public class IconExtractor
  754. 		{
  755. 			// Code to extract icons from Shell32.dll by Thomas Levesque
  756. 			// http://stackoverflow.com/questions/6873026
  757. 			public static System.Drawing.Icon Extract( string file, int number, bool largeIcon )
  758. 			{
  759. 				System.IntPtr large;
  760. 				System.IntPtr small;
  761. 				ExtractIconEx( file, number, out large, out small, 1 );
  762. 				try
  763. 				{
  764. 					return System.Drawing.Icon.FromHandle( largeIcon ? large : small );
  765. 				}
  766. 				catch
  767. 				{
  768. 					return null;
  769. 				}
  770. 			}
  771.  
  772.  
  773. 			[System.Runtime.InteropServices.DllImport( "Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall )]
  774. 			private static extern int ExtractIconEx( string sFile, int iIndex, out System.IntPtr piLargeVersion, out System.IntPtr piSmallVersion, int amountIcons );
  775. 		}
  776.  
  777.  
  778. 		[System.Runtime.InteropServices.ComVisible( false )]
  779. 		public class PrinterSelectBox
  780. 		{
  781. 			public const int defaultheight = 220;
  782. 			public const int defaultwidth = 400;
  783.  
  784.  
  785. 			private static System.Windows.Forms.Label _commentfield;
  786. 			public static System.Windows.Forms.Label CommentField
  787. 			{
  788. 				get
  789. 				{
  790. 					return _commentfield;
  791. 				}
  792. 				set
  793. 				{
  794. 					_commentfield = value;
  795. 				}
  796. 			}
  797.  
  798.  
  799. 			private static System.Windows.Forms.Form _form;
  800. 			public static System.Windows.Forms.Form Form
  801. 			{
  802. 				get
  803. 				{
  804. 					return _form;
  805. 				}
  806. 				set
  807. 				{
  808. 					_form = value;
  809. 				}
  810. 			}
  811.  
  812.  
  813. 			private static System.Windows.Forms.ComboBox _printersdropdown;
  814. 			public static System.Windows.Forms.ComboBox PrintersDropDown
  815. 			{
  816. 				get
  817. 				{
  818. 					return _printersdropdown;
  819. 				}
  820. 				set
  821. 				{
  822. 					_printersdropdown = value;
  823. 				}
  824. 			}
  825.  
  826.  
  827. 			private static System.Collections.Generic.List<string[]> _printerslist;// = new System.Collections.Generic.List<System.String[]>( );
  828. 			public static System.Collections.Generic.List<string[]> PrintersList
  829. 			{
  830. 				get
  831. 				{
  832. 					return _printerslist;
  833. 				}
  834. 				set
  835. 				{
  836. 					_printerslist = value;
  837. 				}
  838. 			}
  839.  
  840.  
  841. 			private static string _selectedprinterattimeout = string.Empty;
  842. 			public static string SelectedPrinterAtTimeout
  843. 			{
  844. 				get
  845. 				{
  846. 					return _selectedprinterattimeout;
  847. 				}
  848. 				set
  849. 				{
  850. 					_selectedprinterattimeout = value;
  851. 				}
  852. 			}
  853.  
  854.  
  855. 			private static System.Windows.Forms.Label _statusfield;
  856. 			public static System.Windows.Forms.Label StatusField
  857. 			{
  858. 				get
  859. 				{
  860. 					return _statusfield;
  861. 				}
  862. 				set
  863. 				{
  864. 					_statusfield = value;
  865. 				}
  866. 			}
  867.  
  868.  
  869. 			private static System.Timers.Timer _timer;
  870. 			public static System.Timers.Timer Timer
  871. 			{
  872. 				get
  873. 				{
  874. 					return _timer;
  875. 				}
  876. 				set
  877. 				{
  878. 					_timer = value;
  879. 				}
  880. 			}
  881.  
  882.  
  883. 			private static bool _timeoutelapsed = false;
  884. 			public static bool TimeoutElapsed
  885. 			{
  886. 				get
  887. 				{
  888. 					return _timeoutelapsed;
  889. 				}
  890. 				set
  891. 				{
  892. 					_timeoutelapsed = value;
  893. 				}
  894. 			}
  895.  
  896.  
  897. 			private static System.Windows.Forms.Label _typefield;
  898. 			public static System.Windows.Forms.Label TypeField
  899. 			{
  900. 				get
  901. 				{
  902. 					return _typefield;
  903. 				}
  904. 				set
  905. 				{
  906. 					_typefield = value;
  907. 				}
  908. 			}
  909.  
  910.  
  911. 			private static System.Windows.Forms.Label _wherefield;
  912. 			public static System.Windows.Forms.Label WhereField
  913. 			{
  914. 				get
  915. 				{
  916. 					return _wherefield;
  917. 				}
  918. 				set
  919. 				{
  920. 					_wherefield = value;
  921. 				}
  922. 			}
  923. 		}
  924.  
  925.  
  926. 		[System.Runtime.InteropServices.ComVisible( false )]
  927. 		public class RadioButtonBox
  928. 		{
  929. 			public const int defaultheight = 320;
  930. 			public const int defaultwidth = 480;
  931.  
  932.  
  933. 			private static System.Windows.Forms.Form _form;
  934. 			public static System.Windows.Forms.Form Form
  935. 			{
  936. 				get
  937. 				{
  938. 					return _form;
  939. 				}
  940. 				set
  941. 				{
  942. 					_form = value;
  943. 				}
  944. 			}
  945.  
  946.  
  947. 			private static System.Windows.Forms.GroupBox _groupbox;
  948. 			public static System.Windows.Forms.GroupBox GroupBox
  949. 			{
  950. 				get
  951. 				{
  952. 					return _groupbox;
  953. 				}
  954. 				set
  955. 				{
  956. 					_groupbox = value;
  957. 				}
  958. 			}
  959.  
  960.  
  961. 			private static string _selecteditemattimeout = string.Empty;
  962. 			public static string SelectedTextAtTimeout
  963. 			{
  964. 				get
  965. 				{
  966. 					return _selecteditemattimeout;
  967. 				}
  968. 				set
  969. 				{
  970. 					_selecteditemattimeout = value;
  971. 				}
  972. 			}
  973.  
  974.  
  975. 			private static System.Timers.Timer _timer = null;
  976. 			public static System.Timers.Timer Timer
  977. 			{
  978. 				get
  979. 				{
  980. 					return _timer;
  981. 				}
  982. 				set
  983. 				{
  984. 					_timer = value;
  985. 				}
  986. 			}
  987.  
  988.  
  989. 			private static bool _timeoutelapsed = false;
  990. 			public static bool TimeoutElapsed
  991. 			{
  992. 				get
  993. 				{
  994. 					return _timeoutelapsed;
  995. 				}
  996. 				set
  997. 				{
  998. 					_timeoutelapsed = value;
  999. 				}
  1000. 			}
  1001. 		}
  1002. 	}
  1003.  
  1004.  
  1005. 	[System.Runtime.InteropServices.ComVisible( false )]
  1006. 	public static class ExtensionMethods
  1007. 	{
  1008. 		public static int LimitToRange( this int value, int inclusiveMinimum, int inclusiveMaximum )
  1009. 		{
  1010. 			// Extension by "dtb" on StackOverflow.com:
  1011. 			// https://stackoverflow.com/a/3176628
  1012. 			if ( value < inclusiveMinimum )
  1013. 			{
  1014. 				return inclusiveMinimum;
  1015. 			}
  1016. 			if ( value > inclusiveMaximum )
  1017. 			{
  1018. 				return inclusiveMaximum;
  1019. 			}
  1020. 			return value;
  1021. 		}
  1022.  
  1023.  
  1024. 		public static float LimitToRange( this float value, float inclusiveMinimum, float inclusiveMaximum )
  1025. 		{
  1026. 			if ( value < inclusiveMinimum )
  1027. 			{
  1028. 				return inclusiveMinimum;
  1029. 			}
  1030. 			if ( value > inclusiveMaximum )
  1031. 			{
  1032. 				return inclusiveMaximum;
  1033. 			}
  1034. 			return value;
  1035. 		}
  1036.  
  1037.  
  1038. 		public static bool IsFixedPitch( this System.Drawing.FontFamily value )
  1039. 		{
  1040. 			System.Drawing.Font font = new System.Drawing.Font( value, 12, System.Drawing.FontStyle.Regular );
  1041. 			return ( System.Windows.Forms.TextRenderer.MeasureText( "WWWWW", font ) == System.Windows.Forms.TextRenderer.MeasureText( "IIIII", font ) );
  1042. 		}
  1043.  
  1044.  
  1045. 		public static bool IsFixedPitch( this System.Drawing.Font value )
  1046. 		{
  1047. 			// Inspired by a tip from Hans passant
  1048. 			// https://stackoverflow.com/q/21965321
  1049. 			return ( System.Windows.Forms.TextRenderer.MeasureText( "WWWWW", value ) == System.Windows.Forms.TextRenderer.MeasureText( "IIIII", value ) );
  1050. 		}
  1051. 	}
  1052.  
  1053.  
  1054. 	public enum ConsoleColors : int
  1055. 	{
  1056. 		Black = 0x000000,
  1057. 		DarkBlue = 0x000080,
  1058. 		DarkGreen = 0x008000,
  1059. 		DarkCyan = 0x008080,
  1060. 		DarkRed = 0x800000,
  1061. 		DarkMagenta = 0x800080,
  1062. 		DarkYellow = 0x808000,
  1063. 		Gray = 0xC0C0C0,
  1064. 		DarkGray = 0x808080,
  1065. 		Blue = 0x0000FF,
  1066. 		Green = 0x00FF00,
  1067. 		Cyan = 0x00FFFF,
  1068. 		Red = 0xFF0000,
  1069. 		Magenta = 0xFF00FF,
  1070. 		Yellow = 0xFFFF00,
  1071. 		White = 0xFFFFFF
  1072. 	}
  1073.  
  1074.  
  1075. 	public enum FontCharSet : byte
  1076. 	{
  1077. 		ANSI = 0,
  1078. 		Default = 1,
  1079. 		Symbol = 2,
  1080. 		Mac = 77,
  1081. 		ShiftJIS = 128,
  1082. 		Hangeul = 129,
  1083. 		Johab = 130,
  1084. 		GB2312 = 134,
  1085. 		ChineseBig5 = 136,
  1086. 		Greek = 161,
  1087. 		Turkish = 162,
  1088. 		Hebrew = 177,
  1089. 		Arabic = 178,
  1090. 		Baltic = 186,
  1091. 		Russian = 204,
  1092. 		Thai = 222,
  1093. 		EastEurope = 238,
  1094. 		OEM = 255,
  1095. 	}
  1096.  
  1097.  
  1098. 	public enum PrinterCategory
  1099. 	{
  1100. 		All,
  1101. 		Local,
  1102. 		Network,
  1103. 		Physical,
  1104. 		Virtual
  1105. 	}
  1106.  
  1107.  
  1108. 	public enum WMIPrinterStatus
  1109. 	{
  1110. 		Other = 1,
  1111. 		Unknown = 2,
  1112. 		Idle = 3,
  1113. 		Printing = 4,
  1114. 		Warmup = 5,
  1115. 		StoppedPrinting = 6,
  1116. 		Offline = 7,
  1117. 		Paused = 8,
  1118. 		Error = 9,
  1119. 		Busy = 10,
  1120. 		NotAvailable = 11,
  1121. 		Waiting = 12,
  1122. 		Processing = 13,
  1123. 		Initialization = 14,
  1124. 		PowerSave = 15,
  1125. 		PendingDeletion = 16,
  1126. 		IOActive = 17,
  1127. 		ManualFeed = 18
  1128. 	}
  1129. }

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