/// <summary>
		/// Switch to the main running instance and optionally send data to the main instance.
		/// </summary>
		/// <param name="args">The command line arguments to pass.</param>
		public void SwitchTo(params string[] args)
		{
			var process = GetOtherInstance();
			if (process == null)
				throw new InvalidOperationException("Can't switch to other instance. The current instance is the first instance.");

			// Get the main window and ensure it's running in foreground.
			var mainWindow = new Window(process.MainWindowHandle);
			if (mainWindow.IsMinimized)
				mainWindow.Show(ShowWindow.Restore);
			mainWindow.SetToForeground();

			SendData(args, mainWindow.Handle);
		}
Пример #2
0
		/// <summary>
		/// Returns an enumerable of child windows, filtered by specified predicate match.
		/// </summary>
		/// <param name="parentWindow">The parent window.</param>
		/// <param name="match">The predicate to filter by.</param>
		public static IEnumerable<Window> GetChildWindows(Window parentWindow, Func<Window, bool> match = null)
		{
			if (!parentWindow.Exists) return new Window[0];

			var windows = new List<Window>();
			NativeMethods.EnumThreadWindowsCallback enumWindows = (hwnd, lParam) =>
			{
				var window = new Window(hwnd);
				if (match == null || match(window)) windows.Add(window);
				return true;
			};

			UnsafeNativeMethods.EnumChildWindows(new HandleRef(parentWindow, parentWindow.Handle), enumWindows, IntPtr.Zero);
			return windows;
		}
Пример #3
0
		/// <summary>
		/// Determines whether the specified object is equal to the current object.
		/// </summary>
		/// <param name="other">The object to compare with the current object. </param>
		/// <returns>
		/// true if the specified object  is equal to the current object; otherwise, false.
		/// </returns>
		protected bool Equals(Window other)
		{
			return Handle.Equals(other.Handle);
		}
Пример #4
0
		/// <summary>
		/// Returns an enumerable of windows, filtered by specified predicate match.
		/// </summary>
		/// <param name="match">The predicate to filter by.</param>
		public static IEnumerable<Window> GetWindows(Func<Window, bool> match = null)
		{
			var windows = new List<Window>();
			NativeMethods.EnumThreadWindowsCallback enumWindows = (hwnd, lParam) =>
			{
				var window = new Window(hwnd);
				if (match == null || match(window)) windows.Add(window);
				return true;
			};

			UnsafeNativeMethods.EnumWindows(enumWindows, IntPtr.Zero);
			return windows;
		}