Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for consolestate.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5.  
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class ConsoleState
  11. 	{
  12. 		static string progver = "1.00";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			try
  18. 			{
  19. 				switch ( args.Length )
  20. 				{
  21. 					case 0:
  22. 						if ( IsWindowVisible( GetConsoleWindow( ) ) )
  23. 						{
  24. 							return 0; // visible
  25. 						}
  26. 						else
  27. 						{
  28. 							return 2; // hidden
  29. 						}
  30. 					case 1:
  31. 						if ( args[0].ToLower( ) == "/hide" )
  32. 						{
  33. 							HideConsoleWindow( );
  34. 							return 0; // success
  35. 						}
  36. 						if ( args[0].ToLower( ) == "/show" )
  37. 						{
  38. 							ShowConsoleWindow( );
  39. 							return 0; // success
  40. 						}
  41. 						if ( args[0] == "/?" )
  42. 						{
  43. 							return ShowHelp( ); // show help
  44. 						}
  45. 						return ShowHelp( "Invalid command line argument \"{0}\"", args[0] ); // invalid command line argument
  46. 					default:
  47. 						return ShowHelp( ); // invalid command line arguments
  48. 				}
  49. 			}
  50. 			catch ( Exception e )
  51. 			{
  52. 				return ShowHelp( e.Message ); // error
  53. 			}
  54. 		}
  55.  
  56.  
  57. 		static int ShowHelp( params string[] errmsg )
  58. 		{
  59. 			// restore console visibility before showing help and/or error message
  60. 			try
  61. 			{
  62. 				if ( !IsWindowVisible( GetConsoleWindow( ) ) )
  63. 				{
  64. 					ShowConsoleWindow( );
  65. 				}
  66. 			}
  67. 			catch { };
  68.  
  69. 			if ( errmsg.Length > 0 )
  70. 			{
  71. 				List<string> errargs = new List<string>( errmsg );
  72. 				errargs.RemoveAt( 0 );
  73. 				Console.Error.WriteLine( );
  74. 				Console.ForegroundColor = ConsoleColor.Red;
  75. 				Console.Error.Write( "ERROR:\t" );
  76. 				Console.ForegroundColor = ConsoleColor.White;
  77. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  78. 				Console.ResetColor( );
  79. 			}
  80.  
  81. 			/*
  82. 			ConsoleState.exe,  Version 1.00
  83. 			Set or get the visibility state of the current console
  84.  
  85. 			Usage:    ConsoleState.exe  [ /Hide | /Show ]
  86.  
  87. 			Where:    /Hide    Hides the console
  88. 			          /Show    Shows the console (restores visibility)
  89.  
  90. 			Notes:    If no command line argument is used, the program will
  91. 			          check the current state and return "errorlevel" 0 if
  92. 			          visible, 2 if hidden, or 1 on errors.
  93. 			          If a command line argument is used, the program will
  94. 			          return "errorlevel" 0 if successful, or 1 on errors.
  95.  
  96. 			Credits:  Code to hide and show console by Anthony on
  97. 			          http://stackoverflow.com/a/15079092
  98. 			          Code to check current console state by "dtb" on
  99. 			          http://stackoverflow.com/a/2655954
  100.  
  101. 			Written by Rob van der Woude
  102. 			http://www.robvanderwoude.com
  103. 			*/
  104.  
  105. 			Console.Error.WriteLine( );
  106.  
  107. 			Console.Error.WriteLine( "ConsoleState.exe,  Version {0}", progver );
  108.  
  109. 			Console.Error.WriteLine( "Set or get the visibility state of the current console" );
  110.  
  111. 			Console.Error.WriteLine( );
  112.  
  113. 			Console.Error.Write( "Usage:    " );
  114. 			Console.ForegroundColor = ConsoleColor.White;
  115. 			Console.Error.WriteLine( "ConsoleState.exe  [ /Hide | /Show ]" );
  116. 			Console.ResetColor( );
  117.  
  118. 			Console.Error.WriteLine( );
  119.  
  120. 			Console.Error.Write( "Where:    " );
  121. 			Console.ForegroundColor = ConsoleColor.White;
  122. 			Console.Error.Write( "/Hide    H" );
  123. 			Console.ResetColor( );
  124. 			Console.Error.WriteLine( "ides the console" );
  125.  
  126. 			Console.ForegroundColor = ConsoleColor.White;
  127. 			Console.Error.Write( "          /Show    S" );
  128. 			Console.ResetColor( );
  129. 			Console.Error.WriteLine( "hows the console (restores visibility)" );
  130.  
  131. 			Console.Error.WriteLine( );
  132.  
  133. 			Console.Error.WriteLine( "Notes:    If no command line argument is used, the program will" );
  134.  
  135. 			Console.Error.WriteLine( "          check the current state and return \"errorlevel\" 0 if" );
  136.  
  137. 			Console.Error.WriteLine( "          visible, 2 if hidden, or 1 on errors." );
  138.  
  139. 			Console.Error.WriteLine( "          If a command line argument is used, the program will" );
  140.  
  141. 			Console.Error.WriteLine( "          return \"errorlevel\" 0 if successful, or 1 on errors." );
  142.  
  143. 			Console.Error.WriteLine( );
  144.  
  145. 			Console.Error.WriteLine( "Credits:  Code to hide and show console by Anthony on" );
  146.  
  147. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  148. 			Console.Error.WriteLine( "          http://stackoverflow.com/a/15079092" );
  149. 			Console.ResetColor( );
  150.  
  151. 			Console.Error.WriteLine( "          Code to check current console state by \"dtb\" on" );
  152.  
  153. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  154. 			Console.Error.WriteLine( "          http://stackoverflow.com/a/2655954" );
  155. 			Console.ResetColor( );
  156.  
  157. 			Console.Error.WriteLine( );
  158.  
  159. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  160.  
  161. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  162. 			return 1;
  163. 		}
  164.  
  165.  
  166. 		#region Hide or Show Console
  167.  
  168. 		// Source: http://stackoverflow.com/a/15079092
  169. 		public static void ShowConsoleWindow( )
  170. 		{
  171. 			var handle = GetConsoleWindow( );
  172. 			if ( handle == IntPtr.Zero )
  173. 			{
  174. 				AllocConsole( );
  175. 			}
  176. 			else
  177. 			{
  178. 				ShowWindow( handle, SW_SHOW );
  179. 			}
  180. 		}
  181.  
  182.  
  183. 		// Source: http://stackoverflow.com/a/15079092
  184. 		public static void HideConsoleWindow( )
  185. 		{
  186. 			var handle = GetConsoleWindow( );
  187. 			ShowWindow( handle, SW_HIDE );
  188. 		}
  189.  
  190.  
  191. 		// Source: http://stackoverflow.com/a/15079092
  192. 		[DllImport( "kernel32.dll", SetLastError = true )]
  193. 		static extern bool AllocConsole( );
  194.  
  195.  
  196. 		// Source: http://stackoverflow.com/a/15079092
  197. 		[DllImport( "kernel32.dll" )]
  198. 		static extern IntPtr GetConsoleWindow( );
  199.  
  200.  
  201. 		// Source: http://stackoverflow.com/a/15079092
  202. 		[DllImport( "user32.dll" )]
  203. 		static extern bool ShowWindow( IntPtr hWnd, int nCmdShow );
  204.  
  205.  
  206. 		// Source: http://stackoverflow.com/a/15079092
  207. 		const int SW_HIDE = 0;
  208. 		const int SW_SHOW = 5;
  209.  
  210.  
  211. 		// Source: http://stackoverflow.com/a/2655954
  212. 		[DllImport( "user32.dll" )]
  213. 		[return: MarshalAs( UnmanagedType.Bool )]
  214. 		static extern bool IsWindowVisible( IntPtr hWnd );
  215.  
  216. 		#endregion Hide or Show Console
  217. 	}
  218. }
  219.  

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