Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for activatewindow.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class ActivateWindow
  10. 	{
  11. 		static readonly string progver = "1.00";
  12.  
  13.  
  14. 		static bool debug = false;
  15. 		static string matchingtitle = string.Empty;
  16.  
  17.  
  18. 		static int Main( string[] args )
  19. 		{
  20. 			string windowtitle = string.Empty;
  21. 			string exact = "partial ";
  22. 			bool exacttitle = false;
  23.  
  24.  
  25. 			#region Parse and Validate Command Line
  26.  
  27. 			foreach ( string arg in args )
  28. 			{
  29. 				if ( arg[0] == '/' )
  30. 				{
  31. 					switch ( arg.ToUpper( ) )
  32. 					{
  33. 						case "/?":
  34. 							return ShowHelp( );
  35. 						case "/D":
  36. 						case "/DEBUG":
  37. 							if ( debug )
  38. 							{
  39. 								return ShowHelp( "Duplicate switch /D" );
  40. 							}
  41. 							debug = true;
  42. 							break;
  43. 						case "/X":
  44. 						case "/EXACT":
  45. 							if ( exacttitle )
  46. 							{
  47. 								return ShowHelp( "Duplicate switch /X" );
  48. 							}
  49. 							exacttitle = true;
  50. 							exact = "exact ";
  51. 							break;
  52. 						default:
  53. 								return ShowHelp( "Invalid switch \"{0}\"", arg );
  54. 					}
  55. 				}
  56. 				else
  57. 				{
  58. 					if ( string.IsNullOrWhiteSpace( windowtitle ) )
  59. 					{
  60. 						// First unnamed argument is title
  61. 						windowtitle = arg;
  62. 					}
  63. 					else
  64. 					{
  65. 						// No second unnamed argument allowed
  66. 						return ShowHelp( "Invalid unnamed argument \"{0}\"", arg );
  67. 					}
  68. 				}
  69. 			}
  70.  
  71.  
  72. #if DEBUG
  73. 			debug = true;
  74. #endif
  75.  
  76.  
  77. 			#endregion Parse and validate Command Line
  78.  
  79.  
  80. 			// Find the window with the specified title
  81. 			IntPtr handle = FindWindow( windowtitle, exacttitle );
  82. 			if ( handle == IntPtr.Zero )
  83. 			{
  84. 				return ShowHelp( "No window was found with {0}title \"{1}\"", exact, windowtitle );
  85. 			}
  86. 			else
  87. 			{
  88. 				if ( debug && !exacttitle )
  89. 				{
  90. 					Console.WriteLine( "Specified title : \"{0}\"\nMatching title  : \"{1}\"\n", windowtitle, matchingtitle );
  91. 				}
  92. 				// If found, make it the foreground window
  93. 				if ( !SetForegroundWindow( handle ) )
  94. 				{
  95. 					return ShowHelp( "Unable to move the specified window to the foreground" );
  96. 				}
  97. 			}
  98.  
  99. 			return 0;
  100. 		}
  101.  
  102.  
  103. 		static IntPtr FindWindow( string title, bool exacttitlematch = false )
  104. 		{
  105. 			foreach ( Process process in Process.GetProcesses( ) )
  106. 			{
  107. 				if ( process.MainWindowTitle.Equals( title ) )
  108. 				{
  109. 					return process.MainWindowHandle; // Return the FIRST matching window
  110. 				}
  111. 				else if ( !exacttitlematch )
  112. 				{
  113. 					if ( process.MainWindowTitle.Contains( title ) )
  114. 					{
  115. 						matchingtitle = process.MainWindowTitle;
  116. 						return process.MainWindowHandle; // Return the FIRST matching window
  117. 					}
  118. 				}
  119. 			}
  120. 			return IntPtr.Zero; // In case no matching title was found
  121. 		}
  122.  
  123.  
  124. 		#region DLL Imports
  125.  
  126. 		[DllImport( "user32.dll" )]
  127. 		[return: MarshalAs( UnmanagedType.Bool )]
  128. 		static extern bool SetForegroundWindow( IntPtr hWnd );
  129.  
  130. 		#endregion DLL Imports
  131.  
  132.  
  133. 		#region Error handling
  134.  
  135. 		static int ShowHelp( params string[] errmsg )
  136. 		{
  137. 			#region Error Message
  138.  
  139. 			if ( errmsg.Length > 0 )
  140. 			{
  141. 				List<string> errargs = new List<string>( errmsg );
  142. 				errargs.RemoveAt( 0 );
  143. 				Console.Error.WriteLine( );
  144. 				Console.ForegroundColor = ConsoleColor.Red;
  145. 				Console.Error.Write( "ERROR:\t" );
  146. 				Console.ForegroundColor = ConsoleColor.White;
  147. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  148. 				Console.ResetColor( );
  149. 			}
  150.  
  151. 			#endregion Error Message
  152.  
  153.  
  154. 			#region Help Text
  155.  
  156. 			/*
  157. 			ActivateWindow.exe,  Version 1.00
  158. 			Activate the specified window
  159.  
  160. 			Usage:   ActivateWindow.exe  title  [ options ]
  161.  
  162. 			Where:   title   is the window title
  163.  
  164. 			Options: /D      Debug mode: show the screen coordinates used
  165. 			         /X      window title and specified title must match eXactly
  166. 			                 (default: window title contains specified title)
  167.  
  168. 			Note:    Return code -1 in case of errors, otherwise 0.
  169.  
  170. 			Written by Rob van der Woude
  171. 			https://www.robvanderwoude.com
  172. 			*/
  173.  
  174. 			#endregion Help Text
  175.  
  176.  
  177. 			#region Display Help Text
  178.  
  179. 			Console.Error.WriteLine( );
  180.  
  181. 			Console.Error.WriteLine( "ActivateWindow.exe,  Version {0}", progver );
  182.  
  183. 			Console.Error.WriteLine( "Activate the specified window" );
  184.  
  185. 			Console.Error.WriteLine( );
  186.  
  187. 			Console.Error.Write( "Usage:   " );
  188. 			Console.ForegroundColor = ConsoleColor.White;
  189. 			Console.Error.WriteLine( "ActivateWindow.exe  title  [ options ]" );
  190. 			Console.ResetColor( );
  191.  
  192. 			Console.Error.WriteLine( );
  193.  
  194. 			Console.Write( "Where:   " );
  195. 			Console.ForegroundColor = ConsoleColor.White;
  196. 			Console.Write( "title   " );
  197. 			Console.ResetColor( );
  198. 			Console.WriteLine( "is the window title" );
  199.  
  200. 			Console.Error.WriteLine( );
  201.  
  202. 			Console.Error.Write( "Options: " );
  203. 			Console.ForegroundColor = ConsoleColor.White;
  204. 			Console.Error.Write( "/D      D" );
  205. 			Console.ResetColor( );
  206. 			Console.Error.WriteLine( "ebug mode: show the screen coordinates used" );
  207.  
  208. 			Console.ForegroundColor = ConsoleColor.White;
  209. 			Console.Error.Write( "         /X      " );
  210. 			Console.ResetColor( );
  211. 			Console.Error.Write( "window title and specified title must match e" );
  212. 			Console.ForegroundColor = ConsoleColor.White;
  213. 			Console.Error.Write( "X" );
  214. 			Console.ResetColor( );
  215. 			Console.Error.WriteLine( "actly" );
  216.  
  217. 			Console.Error.Write( "                 (default: window title " );
  218. 			Console.ForegroundColor = ConsoleColor.White;
  219. 			Console.Error.Write( "contains " );
  220. 			Console.ResetColor( );
  221. 			Console.Error.WriteLine( "specified title)" );
  222.  
  223. 			Console.Error.WriteLine( );
  224.  
  225. 			Console.Error.WriteLine( "Note:    Return code -1 in case of errors, otherwise 0." );
  226.  
  227. 			Console.Error.WriteLine( );
  228.  
  229. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  230.  
  231. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  232.  
  233. 			#endregion Display Help Text
  234.  
  235.  
  236. 			return -1;
  237. 		}
  238.  
  239. 		#endregion Error handling
  240. 	}
  241. }
  242.  

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