Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printerselectbox.cs

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

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Management;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Forms;
  10.  
  11.  
  12. namespace RobvanderWoude
  13. {
  14. 	class PrinterSelectBox
  15. 	{
  16. 		public static string progver = "2.05";
  17.  
  18. 		public static ComboBox combobox;
  19. 		public static Label statusField;
  20. 		public static Label typeField;
  21. 		public static Label whereField;
  22. 		public static Label commentField;
  23. 		public static List<string[]> printerList;
  24.  
  25.  
  26. 		[STAThread]
  27. 		static int Main( string[] args )
  28. 		{
  29. 			try
  30. 			{
  31. 				// On-the fly form inspired by Gorkem Gencay's code on StackOverflow.com:
  32. 				// http://stackoverflow.com/questions/97097/what-is-the-c-sharp-version-of-vb-nets-inputdialog#17546909
  33.  
  34. 				#region Initialize Variables
  35.  
  36. 				string selected;
  37. 				string title = "Select Printer";
  38. 				bool titleset = false;
  39. 				string preselected = String.Empty;
  40. 				bool preselectedset = false;
  41. 				string cancelcaption = "&Cancel";
  42. 				string okcaption = "&OK";
  43. 				string printcaption = "&Print";
  44. 				bool printcaptionset = false;
  45. 				SortedList printerNames;
  46. 				int height = 220;
  47. 				int width = 400;
  48. 				bool widthset = false;
  49. 				string localizationstring = String.Empty;
  50. 				bool localizedcaptionset = false;
  51. 				string namecaption = "Name";
  52. 				string statuscaption = "Status";
  53. 				string typecaption = "Type";
  54. 				string wherecaption = "Where";
  55. 				string commentcaption = "Comment";
  56.  
  57. 				#endregion Initialize Variables
  58.  
  59.  
  60. 				#region Populate Printers List
  61.  
  62. 				QueryPrinters( );
  63. 				printerNames = new SortedList( );
  64. 				foreach ( string[] prn in printerList )
  65. 				{
  66. 					printerNames.Add( prn[0], prn[5] );
  67. 					// Make the default printer the initial preselected printer in the dropdown list
  68. 					if ( prn[5] == "1" )
  69. 					{
  70. 						preselected = prn[0];
  71. 					}
  72. 				}
  73.  
  74. 				#endregion Populate Printers List
  75.  
  76.  
  77. 				#region Parse Command Line
  78.  
  79. 				if ( args.Length > 5 )
  80. 				{
  81. 					return ShowHelp( );
  82. 				}
  83. 				foreach ( string arg in args )
  84. 				{
  85. 					if ( arg == "/?" )
  86. 					{
  87. 						return ShowHelp( );
  88. 					}
  89. 				}
  90.  
  91. 				if ( args.Length > 0 )
  92. 				{
  93. 					foreach ( string arg in args )
  94. 					{
  95. 						if ( arg.Length > 0 && arg[0] == '/' )
  96. 						{
  97. 							if ( arg.Substring( 0, 2 ).ToUpper( ) == "/L" )
  98. 							{
  99. 								if ( localizedcaptionset )
  100. 								{
  101. 									return ShowHelp( "Duplicate command line switch /L" );
  102. 								}
  103. 								localizedcaptionset = true;
  104. 								if ( arg.Length > 3 )
  105. 								{
  106. 									if ( arg[2] == ':' )
  107. 									{
  108. 										localizationstring = arg.Substring( 3 );
  109. 									}
  110. 									else
  111. 									{
  112. 										return ShowHelp( "Invalid command line switch \"{0}\"", arg );
  113. 									}
  114. 								}
  115. 							}
  116. 							else if ( arg.Length > 1 && arg.Substring( 0, 2 ).ToUpper( ) == "/P" )
  117. 							{
  118. 								if ( printcaptionset )
  119. 								{
  120. 									return ShowHelp( "Duplicate command line switch /P" );
  121. 								}
  122. 								printcaptionset = true;
  123. 								if ( arg.Length > 3 )
  124. 								{
  125. 									if ( arg[2] == ':' )
  126. 									{
  127. 										printcaption = arg.Substring( 3 );
  128. 									}
  129. 									else
  130. 									{
  131. 										return ShowHelp( "Invalid command line switch \"{0}\"", arg );
  132. 									}
  133. 								}
  134. 							}
  135. 							else
  136. 							{
  137. 								return ShowHelp( "Invalid command line switch \"{0}\"", arg );
  138. 							}
  139. 						}
  140. 						else
  141. 						{
  142. 							if ( !titleset )
  143. 							{
  144. 								title = arg;
  145. 								titleset = true;
  146. 							}
  147. 							else if ( !preselectedset )
  148. 							{
  149. 								// Make the specified printer the selected one in the dropdown list
  150. 								if ( printerNames.ContainsKey( arg ) )
  151. 								{
  152. 									preselected = arg;
  153. 								}
  154. 								else
  155. 								{
  156. 									string pattern = arg;
  157. 									bool match = false;
  158. 									foreach ( string printer in printerNames.Keys )
  159. 									{
  160. 										if ( Regex.IsMatch( printer, pattern, RegexOptions.IgnoreCase ) )
  161. 										{
  162. 											match = true;
  163. 											preselected = printer;
  164. 											break; // Search no further after first match
  165. 										}
  166. 									}
  167. 									if ( !match )
  168. 									{
  169. 										return ShowHelp( "No printer name found matching \"{0}\"", preselected );
  170. 									}
  171. 								}
  172. 								preselectedset = true;
  173. 							}
  174. 							else if ( !widthset )
  175. 							{
  176. 								try
  177. 								{
  178. 									width = Convert.ToInt32( arg );
  179. 									widthset = true;
  180. 								}
  181. 								catch ( Exception )
  182. 								{
  183. 									return ShowHelp( "Invalid width \"{0}\"", arg );
  184. 								}
  185. 							}
  186. 							else
  187. 							{
  188. 								return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  189. 							}
  190. 						}
  191. 					}
  192. 				}
  193.  
  194. 				#endregion Parse Command Line
  195.  
  196.  
  197. 				#region Set Localized Captions
  198.  
  199. 				if ( localizedcaptionset )
  200. 				{
  201. 					cancelcaption = Load( "user32.dll", 801, cancelcaption );
  202. 					okcaption = Load( "user32.dll", 800, okcaption );
  203. 					if ( !String.IsNullOrWhiteSpace( localizationstring ) )
  204. 					{
  205. 						string pattern = @"^((Name|Status|Type|Where|Comment|OK|Cancel)=[^;\""]*;)*((Name|Status|Type|Where|Comment|OK|Cancel)=[^;\""]*);?$";
  206. 						Regex regex = new Regex( pattern, RegexOptions.IgnoreCase );
  207. 						if ( regex.IsMatch( localizationstring ) )
  208. 						{
  209. 							string[] locstrings = localizationstring.Split( ";".ToCharArray( ) );
  210. 							foreach ( string locstring in locstrings )
  211. 							{
  212. 								string key = locstring.Substring( 0, locstring.IndexOf( '=' ) );
  213. 								string val = locstring.Substring( Math.Min( locstring.IndexOf( '=' ) + 1, locstring.Length - 1 ) );
  214. 								if ( !String.IsNullOrWhiteSpace( val ) )
  215. 								{
  216. 									switch ( key.ToUpper( ) )
  217. 									{
  218. 										case "NAME":
  219. 											namecaption = val;
  220. 											break;
  221. 										case "STATUS":
  222. 											statuscaption = val;
  223. 											break;
  224. 										case "TYPE":
  225. 											typecaption = val;
  226. 											break;
  227. 										case "WHERE":
  228. 											wherecaption = val;
  229. 											break;
  230. 										case "COMMENT":
  231. 											commentcaption = val;
  232. 											break;
  233. 										case "OK":
  234. 											okcaption = val;
  235. 											break;
  236. 										case "CANCEL":
  237. 											cancelcaption = val;
  238. 											break;
  239. 										default:
  240. 											return ShowHelp( "Invalid localization key \"{0}\"", key );
  241. 									}
  242. 								}
  243. 							}
  244. 						}
  245. 						else
  246. 						{
  247. 							return ShowHelp( "Invalid localization string:\n\t{0}", localizationstring );
  248. 						}
  249. 					}
  250. 				}
  251.  
  252. 				#endregion Set Localized Captions
  253.  
  254.  
  255. 				#region Build Form
  256.  
  257. 				// Create the dialog box
  258. 				Form printerSelect = new Form( );
  259. 				Size size = new Size( width, height );
  260. 				printerSelect.FormBorderStyle = FormBorderStyle.FixedDialog;
  261. 				printerSelect.MaximizeBox = false;
  262. 				printerSelect.MinimizeBox = false;
  263. 				printerSelect.StartPosition = FormStartPosition.CenterParent;
  264. 				printerSelect.ClientSize = size;
  265. 				if ( printcaptionset && !titleset )
  266. 				{
  267. 					title = printcaption.Replace( "&", String.Empty );
  268. 				}
  269. 				printerSelect.Text = title;
  270.  
  271. 				// Label "Name:" for printer name
  272. 				Label labelName = new Label( );
  273. 				labelName.Size = new Size( 70, 20 );
  274. 				labelName.Location = new Point( 10, 27 );
  275. 				labelName.Text = namecaption + ":";
  276. 				printerSelect.Controls.Add( labelName );
  277.  
  278. 				// Dropdown list to contain available printer names
  279. 				combobox = new ComboBox( );
  280. 				combobox.Size = new Size( size.Width - 100, 25 );
  281. 				combobox.Location = new Point( 90, 25 );
  282. 				combobox.AutoCompleteMode = AutoCompleteMode.Append;
  283. 				combobox.AutoCompleteSource = AutoCompleteSource.ListItems;
  284. 				combobox.DropDownStyle = ComboBoxStyle.DropDownList;
  285. 				combobox.SelectedIndexChanged += new EventHandler( combobox_SelectedIndexChanged );
  286. 				printerSelect.Controls.Add( combobox );
  287.  
  288. 				// Label "Status:" for selected printer's current status
  289. 				Label labelStatus = new Label( );
  290. 				labelStatus.Size = new Size( 70, 20 );
  291. 				labelStatus.Location = new Point( 10, 57 );
  292. 				labelStatus.Text = statuscaption + ":";
  293. 				printerSelect.Controls.Add( labelStatus );
  294.  
  295. 				// Label to contain selected printer's current status
  296. 				statusField = new Label( );
  297. 				statusField.Size = new Size( size.Width - 10 - 80, 20 );
  298. 				statusField.Location = new Point( 90, 57 );
  299. 				statusField.Text = "Status Value";
  300. 				printerSelect.Controls.Add( statusField );
  301.  
  302. 				// Label "Type:" for selected printer's type description
  303. 				Label labelType = new Label( );
  304. 				labelType.Size = new Size( 70, 20 );
  305. 				labelType.Location = new Point( 10, 82 );
  306. 				labelType.Text = typecaption + ":";
  307. 				printerSelect.Controls.Add( labelType );
  308.  
  309. 				// Label to contain selected printer's type description
  310. 				typeField = new Label( );
  311. 				typeField.Size = new Size( size.Width - 10 - 80, 20 );
  312. 				typeField.Location = new Point( 90, 82 );
  313. 				typeField.Text = "Type Value";
  314. 				printerSelect.Controls.Add( typeField );
  315.  
  316. 				// Label "Where:" for selected printer's location or printer port
  317. 				Label labelWhere = new Label( );
  318. 				labelWhere.Size = new Size( 70, 20 );
  319. 				labelWhere.Location = new Point( 10, 107 );
  320. 				labelWhere.Text = wherecaption + ":";
  321. 				printerSelect.Controls.Add( labelWhere );
  322.  
  323. 				// Label to contain selected printer's location or printer port
  324. 				whereField = new Label( );
  325. 				whereField.Size = new Size( size.Width - 10 - 80, 20 );
  326. 				whereField.Location = new Point( 90, 107 );
  327. 				whereField.Text = "Where Value";
  328. 				printerSelect.Controls.Add( whereField );
  329.  
  330. 				// Label "Comment:" for selected printer's optional comment line
  331. 				Label labelComment = new Label( );
  332. 				labelComment.Size = new Size( 70, 20 );
  333. 				labelComment.Location = new Point( 10, 132 );
  334. 				labelComment.Text = commentcaption + ":";
  335. 				printerSelect.Controls.Add( labelComment );
  336.  
  337. 				// Label to contain selected printer's optional comment line
  338. 				commentField = new Label( );
  339. 				commentField.Size = new Size( size.Width - 10 - 80, 20 );
  340. 				commentField.Location = new Point( 90, 132 );
  341. 				commentField.Text = "Comment Value";
  342. 				printerSelect.Controls.Add( commentField );
  343.  
  344. 				Button okButton = new Button( );
  345. 				okButton.DialogResult = DialogResult.OK;
  346. 				okButton.Name = "okButton";
  347. 				okButton.Size = new Size( 80, 25 );
  348. 				if ( printcaptionset )
  349. 				{
  350. 					okButton.Text = printcaption;
  351. 				}
  352. 				else
  353. 				{
  354. 					okButton.Text = okcaption;
  355. 				}
  356. 				okButton.Location = new Point( size.Width / 2 - 10 - 80, 175 );
  357. 				printerSelect.Controls.Add( okButton );
  358.  
  359. 				Button cancelButton = new Button( );
  360. 				cancelButton.DialogResult = DialogResult.Cancel;
  361. 				cancelButton.Name = "cancelButton";
  362. 				cancelButton.Size = new Size( 80, 25 );
  363. 				cancelButton.Text = cancelcaption;
  364. 				cancelButton.Location = new Point( size.Width / 2 + 10, 175 );
  365. 				printerSelect.Controls.Add( cancelButton );
  366.  
  367. 				printerSelect.AcceptButton = okButton;  // OK on Enter
  368. 				printerSelect.CancelButton = cancelButton; // Cancel on Esc
  369. 				printerSelect.Activate( );
  370.  
  371. 				// Populate the dropdown list with the printers from the sorted list
  372. 				int index = 0;
  373. 				foreach ( string prn in printerNames.Keys )
  374. 				{
  375. 					combobox.Items.Add( prn );
  376. 					// Select the preselected printer if specified, or the default printer otherwise
  377. 					if ( prn.ToLower( ) == preselected.ToLower( ) )
  378. 					{
  379. 						combobox.SelectedIndex = index;
  380. 						foreach ( string[] printer in printerList )
  381. 						{
  382. 							if ( printer[0].ToLower( ) == preselected.ToLower( ) )
  383. 							{
  384. 								statusField.Text = printer[1];
  385. 								typeField.Text = printer[2];
  386. 								whereField.Text = printer[3];
  387. 								commentField.Text = printer[4];
  388. 							}
  389. 						}
  390. 					}
  391. 					index += 1;
  392. 				}
  393.  
  394. 				#endregion Build Form
  395.  
  396.  
  397. 				DialogResult result = printerSelect.ShowDialog( );
  398. 				if ( result == DialogResult.OK )
  399. 				{
  400. 					selected = combobox.SelectedItem.ToString( );
  401. 					Console.WriteLine( selected );
  402. 					return 0;
  403. 				}
  404. 				else
  405. 				{
  406. 					return 2;
  407. 				}
  408. 			}
  409. 			catch ( Exception e )
  410. 			{
  411. 				return ShowHelp( e.Message );
  412. 			}
  413. 		}
  414.  
  415.  
  416. 		public static void combobox_SelectedIndexChanged( object sender, EventArgs e )
  417. 		{
  418. 			string printername = combobox.Text;
  419. 			foreach ( string[] prn in printerList )
  420. 			{
  421. 				if ( prn[0] == printername )
  422. 				{
  423. 					statusField.Text = prn[1];
  424. 					typeField.Text = prn[2];
  425. 					whereField.Text = prn[3];
  426. 					commentField.Text = prn[4];
  427. 				}
  428. 			}
  429. 		}
  430.  
  431.  
  432. 		public static void QueryPrinters( )
  433. 		{
  434. 			printerList = new List<string[]>( );
  435.  
  436. 			string query = "SELECT * FROM Win32_Printer";
  437. 			ManagementObjectSearcher searcher = new ManagementObjectSearcher( "root\\CIMV2", query );
  438. 			foreach ( ManagementObject queryObj in searcher.Get( ) )
  439. 			{
  440. 				string printer = (string) queryObj["DeviceID"];
  441. 				UInt32 printerstatus = Convert.ToUInt32( queryObj["ExtendedPrinterStatus"] );
  442. 				string printertype = (string) queryObj["DriverName"];
  443. 				string printerlocation = (string) queryObj["Location"];
  444. 				string printerport = (string) queryObj["PortName"];
  445. 				string printercomment = (string) queryObj["Comment"];
  446. 				bool isdefault = (bool) queryObj["Default"];
  447. 				string[] printerProperties = new string[6];
  448. 				printerProperties[0] = printer;
  449. 				printerProperties[1] = ( (WMIPrinterStatus) printerstatus ).ToString( );
  450. 				printerProperties[2] = printertype;
  451. 				if ( String.IsNullOrWhiteSpace( printerlocation ) )
  452. 				{
  453. 					printerProperties[3] = printerport;
  454. 				}
  455. 				else
  456. 				{
  457. 					printerProperties[3] = printerlocation;
  458. 				}
  459. 				printerProperties[4] = printercomment;
  460. 				printerProperties[5] = ( isdefault ? "1" : "0" );
  461. 				printerList.Add( printerProperties );
  462. 			}
  463. 		}
  464.  
  465.  
  466. 		public static void UpdateFields( )
  467. 		{
  468. 			string printername = combobox.SelectedValue.ToString( );
  469. 			foreach ( string[] prn in printerList )
  470. 			{
  471. 				if ( prn[0] == printername )
  472. 				{
  473. 					statusField.Text = prn[1];
  474. 					typeField.Text = prn[2];
  475. 					whereField.Text = prn[3];
  476. 					commentField.Text = prn[4];
  477. 				}
  478. 			}
  479. 		}
  480.  
  481.  
  482. 		public static int ShowHelp( params string[] errmsg )
  483. 		{
  484. 			/*
  485. 			PrinterSelectBox,  Version 2.03
  486. 			Batch tool to present a Printer Select dialog and return the selected printer
  487.  
  488. 			Usage:    PRINTERSELECTBOX  [ "title"  [ "selected"  [ width ] ] ]  [ options ]
  489.  
  490. 			Where:    "title"     is the optional caption in the title bar
  491. 			          "selected"  is the optional selected printer, either its full name or
  492. 			                      a regular expression (default: the default printer name)
  493. 			          width       is the optional width of the input box
  494. 			                      (default: 400; minimum: 400; maximum: screen width)
  495. 			Options:  /L[:string] Use Localized or custom captions (see Notes)
  496. 			          /P[:text]   Alternative "OK" button caption "Print" or "text"
  497.  
  498. 			Notes:    Use /L for Localized "OK" and "Cancel" button captions only.
  499. 			          Custom captions require a "localization string" in the format
  500. 			          /L:"key=value;key=value;..." e.g. for Dutch on English computers:
  501. 			          /L:"Name=Naam;Where=Lokatie;Comment=Opmerking;Cancel=Annuleren"
  502. 			          The name of the selected printer is written to Standard Out if the
  503. 			          "OK" button is clicked, otherwise an empty string is returned.
  504. 			          If "selected" is specified, the program will try an exact (literal)
  505. 			          match first; if no match is found, "selected" will be interpreted as
  506. 			          a regular expression, and the first match in the sorted printer list
  507. 			          will be used.
  508. 			          If no "title" is specified and /P is used, the alternative "OK"
  509. 			          button caption ("Print" or "text") will be used for "title" too.
  510. 			          Return code 0 for "OK", 1 for (command line) errors, 2 for "Cancel".
  511.  
  512. 			Credits:  On-the-fly form based on code by Gorkem Gencay on StackOverflow:
  513. 			          http://stackoverflow.com/questions/97097
  514. 			          /what-is-the-c-sharp-version-of-vb-nets-inputdialog#17546909
  515. 			          Code to retrieve localized button captions by Martin Stoeckli:
  516. 			          http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources
  517.  
  518. 			Written by Rob van der Woude
  519. 			http://www.robvanderwoude.com
  520. 			*/
  521.  
  522. 			if ( errmsg.Length > 0 )
  523. 			{
  524. 				List<string> errargs = new List<string>( errmsg );
  525. 				errargs.RemoveAt( 0 );
  526. 				Console.Error.WriteLine( );
  527. 				Console.ForegroundColor = ConsoleColor.Red;
  528. 				Console.Error.Write( "ERROR:\t" );
  529. 				Console.ForegroundColor = ConsoleColor.White;
  530. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  531. 				Console.ResetColor( );
  532. 			}
  533.  
  534. 			Console.Error.WriteLine( );
  535.  
  536. 			Console.Error.WriteLine( "PrinterSelectBox,  Version {0}", progver );
  537.  
  538. 			Console.Error.WriteLine( "Batch tool to present a Printer Select dialog and return the selected printer" );
  539.  
  540. 			Console.Error.WriteLine( );
  541.  
  542. 			Console.Error.Write( "Usage:    " );
  543. 			Console.ForegroundColor = ConsoleColor.White;
  544. 			Console.Error.WriteLine( "PRINTERSELECTBOX  [ \"title\"  [ \"selected\"  [ width ] ] ]  [ options ]" );
  545. 			Console.ResetColor( );
  546.  
  547. 			Console.Error.WriteLine( );
  548.  
  549. 			Console.Error.Write( "Where:    " );
  550. 			Console.ForegroundColor = ConsoleColor.White;
  551. 			Console.Error.Write( "\"title\"" );
  552. 			Console.ResetColor( );
  553. 			Console.Error.WriteLine( "     is the optional caption in the title bar" );
  554.  
  555. 			Console.ForegroundColor = ConsoleColor.White;
  556. 			Console.Error.Write( "          \"selected\"" );
  557. 			Console.ResetColor( );
  558. 			Console.Error.WriteLine( "  is the optional selected printer, either its full name or" );
  559.  
  560. 			Console.Error.WriteLine( "                      a regular expression (default: the default printer name)" );
  561.  
  562. 			Console.ForegroundColor = ConsoleColor.White;
  563. 			Console.Error.Write( "          width" );
  564. 			Console.ResetColor( );
  565. 			Console.Error.WriteLine( "       is the optional width of the input box" );
  566.  
  567. 			Console.Error.WriteLine( "                      (default: 400; minimum: 400; maximum: screen width)" );
  568.  
  569. 			Console.Error.Write( "Options:  " );
  570. 			Console.ForegroundColor = ConsoleColor.White;
  571. 			Console.Error.Write( "/L[:string]" );
  572. 			Console.ResetColor( );
  573. 			Console.Error.Write( " Use " );
  574. 			Console.ForegroundColor = ConsoleColor.White;
  575. 			Console.Error.Write( "L" );
  576. 			Console.ResetColor( );
  577. 			Console.Error.WriteLine( "ocalized or custom captions (see Notes)" );
  578.  
  579. 			Console.ForegroundColor = ConsoleColor.White;
  580. 			Console.Error.Write( "          /P[:text]" );
  581. 			Console.ResetColor( );
  582. 			Console.Error.Write( "   Alternative \"OK\" button caption \"" );
  583. 			Console.ForegroundColor = ConsoleColor.White;
  584. 			Console.Error.Write( "P" );
  585. 			Console.ResetColor( );
  586. 			Console.Error.Write( "rint\" or \"" );
  587. 			Console.ForegroundColor = ConsoleColor.White;
  588. 			Console.Error.Write( "text" );
  589. 			Console.ResetColor( );
  590. 			Console.Error.WriteLine( "\"" );
  591.  
  592. 			Console.Error.WriteLine( );
  593.  
  594. 			Console.Error.Write( "Notes:    Use " );
  595. 			Console.ForegroundColor = ConsoleColor.White;
  596. 			Console.Error.Write( "/L" );
  597. 			Console.ResetColor( );
  598. 			Console.Error.Write( " for " );
  599. 			Console.ForegroundColor = ConsoleColor.White;
  600. 			Console.Error.Write( "L" );
  601. 			Console.ResetColor( );
  602. 			Console.Error.WriteLine( "ocalized \"OK\" and \"Cancel\" button captions only." );
  603.  
  604. 			Console.Error.Write( "          Custom captions require a \"localization " );
  605. 			Console.ForegroundColor = ConsoleColor.White;
  606. 			Console.Error.Write( "string" );
  607. 			Console.ResetColor( );
  608. 			Console.Error.WriteLine( "\" in the format" );
  609.  
  610. 			Console.ForegroundColor = ConsoleColor.White;
  611. 			Console.Error.Write( "          /L:\"key=value;key=value;...\"" );
  612. 			Console.ResetColor( );
  613. 			Console.Error.WriteLine( " e.g. for Dutch on English computers:" );
  614.  
  615. 			Console.ForegroundColor = ConsoleColor.White;
  616. 			Console.Error.WriteLine( "          /L:\"Name=Naam;Where=Lokatie;Comment=Opmerking;Cancel=Annuleren\"" );
  617. 			Console.ResetColor( );
  618.  
  619. 			Console.Error.WriteLine( "          The name of the selected printer is written to Standard Out if the" );
  620.  
  621. 			Console.Error.WriteLine( "          \"OK\" button is clicked, otherwise an empty string is returned." );
  622.  
  623. 			Console.Error.Write( "          If \"" );
  624. 			Console.ForegroundColor = ConsoleColor.White;
  625. 			Console.Error.Write( "selected" );
  626. 			Console.ResetColor( );
  627. 			Console.Error.WriteLine( "\" is specified, the program will try an exact (literal)" );
  628.  
  629. 			Console.Error.Write( "          match first; if no match is found, \"" );
  630. 			Console.ForegroundColor = ConsoleColor.White;
  631. 			Console.Error.Write( "selected" );
  632. 			Console.ResetColor( );
  633. 			Console.Error.WriteLine( "\" will be interpreted as" );
  634.  
  635. 			Console.Error.WriteLine( "          a regular expression, and the first match in the sorted printer list" );
  636.  
  637. 			Console.Error.WriteLine( "          will be used." );
  638.  
  639. 			Console.Error.Write( "          If no \"" );
  640. 			Console.ForegroundColor = ConsoleColor.White;
  641. 			Console.Error.Write( "title" );
  642. 			Console.ResetColor( );
  643. 			Console.Error.Write( "\" is specified and " );
  644. 			Console.ForegroundColor = ConsoleColor.White;
  645. 			Console.Error.Write( "/P" );
  646. 			Console.ResetColor( );
  647. 			Console.Error.WriteLine( " is used, the alternative \"OK\"" );
  648.  
  649. 			Console.Error.Write( "          button caption (\"" );
  650. 			Console.ForegroundColor = ConsoleColor.White;
  651. 			Console.Error.Write( "P" );
  652. 			Console.ResetColor( );
  653. 			Console.Error.Write( "rint\" or \"" );
  654. 			Console.ForegroundColor = ConsoleColor.White;
  655. 			Console.Error.Write( "text" );
  656. 			Console.ResetColor( );
  657. 			Console.Error.Write( "\") will be used for \"" );
  658. 			Console.ForegroundColor = ConsoleColor.White;
  659. 			Console.Error.Write( "title" );
  660. 			Console.ResetColor( );
  661. 			Console.Error.WriteLine( "\" too." );
  662.  
  663. 			Console.Error.WriteLine( "          Return code 0 for \"OK\", 1 for (command line) errors, 2 for \"Cancel\"." );
  664.  
  665. 			Console.Error.WriteLine( );
  666.  
  667. 			Console.Error.WriteLine( "Credits:  On-the-fly form based on code by Gorkem Gencay on StackOverflow:" );
  668.  
  669. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  670. 			Console.Error.WriteLine( "          http://stackoverflow.com/questions/97097" );
  671.  
  672. 			Console.Error.WriteLine( "          /what-is-the-c-sharp-version-of-vb-nets-inputdialog#17546909" );
  673. 			Console.ResetColor( );
  674.  
  675. 			Console.Error.WriteLine( "          Code to retrieve localized button captions by Martin Stoeckli:" );
  676.  
  677. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  678. 			Console.Error.WriteLine( "          http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources" );
  679. 			Console.ResetColor( );
  680.  
  681. 			Console.Error.WriteLine( );
  682.  
  683. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  684.  
  685. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  686.  
  687. 			return 1;
  688. 		}
  689.  
  690.  
  691. 		#region Get Localized Captions
  692.  
  693. 		// Code to retrieve localized captions by Martin Stoeckli
  694. 		// http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources
  695.  
  696. 		/// <summary>
  697. 		/// Searches for a text resource in a Windows library.
  698. 		/// Sometimes, using the existing Windows resources, you can make your code
  699. 		/// language independent and you don't have to care about translation problems.
  700. 		/// </summary>
  701. 		/// <example>
  702. 		///   btnCancel.Text = Load("user32.dll", 801, "Cancel");
  703. 		///   btnYes.Text = Load("user32.dll", 805, "Yes");
  704. 		/// </example>
  705. 		/// <param name="libraryName">Name of the windows library like "user32.dll"
  706. 		/// or "shell32.dll"</param>
  707. 		/// <param name="ident">Id of the string resource.</param>
  708. 		/// <param name="defaultText">Return this text, if the resource string could
  709. 		/// not be found.</param>
  710. 		/// <returns>Requested string if the resource was found,
  711. 		/// otherwise the <paramref name="defaultText"/></returns>
  712. 		public static string Load( string libraryName, UInt32 ident, string defaultText )
  713. 		{
  714. 			IntPtr libraryHandle = GetModuleHandle( libraryName );
  715. 			if ( libraryHandle != IntPtr.Zero )
  716. 			{
  717. 				StringBuilder sb = new StringBuilder( 1024 );
  718. 				int size = LoadString( libraryHandle, ident, sb, 1024 );
  719. 				if ( size > 0 )
  720. 					return sb.ToString( );
  721. 			}
  722. 			return defaultText;
  723. 		}
  724.  
  725. 		[DllImport( "kernel32.dll", CharSet = CharSet.Auto )]
  726. 		private static extern IntPtr GetModuleHandle( string lpModuleName );
  727.  
  728. 		[DllImport( "user32.dll", CharSet = CharSet.Auto )]
  729. 		private static extern int LoadString( IntPtr hInstance, UInt32 uID, StringBuilder lpBuffer, Int32 nBufferMax );
  730.  
  731. 		#endregion Get Localized Captions
  732. 	}
  733.  
  734.  
  735. 	public enum WMIPrinterStatus
  736. 	{
  737. 		Other = 1,
  738. 		Unknown = 2,
  739. 		Idle = 3,
  740. 		Printing = 4,
  741. 		Warmup = 5,
  742. 		StoppedPrinting = 6,
  743. 		Offline = 7,
  744. 		Paused = 8,
  745. 		Error = 9,
  746. 		Busy = 10,
  747. 		NotAvailable = 11,
  748. 		Waiting = 12,
  749. 		Processing = 13,
  750. 		Initialization = 14,
  751. 		PowerSave = 15,
  752. 		PendingDeletion = 16,
  753. 		IOActive = 17,
  754. 		ManualFeed = 18
  755. 	}
  756. }
  757.  

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