/// <summary> /// Shows a window. /// </summary> /// <param name="hWnd"> The window to show. </param> /// <remarks> /// <para> /// Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />. /// </para> /// </remarks> public static void ShowWindow(IntPtr hWnd) { if (hWnd == IntPtr.Zero) { return; } WindowsWindow.ShowWindow(hWnd, (int)WindowsWindow.SwShow); }
/// <summary> /// Enables or disables a window. /// </summary> /// <param name="hWnd"> The window to enable/disable. </param> /// <param name="enable"> true if the window should be enabled, false if it should be disabled. </param> /// <remarks> /// <para> /// Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />. /// </para> /// </remarks> public static void EnableWindow(IntPtr hWnd, bool enable) { if (hWnd == IntPtr.Zero) { return; } WindowsWindow.EnableWindowInternal(hWnd, enable); }
/// <summary> /// Moves a window to the background (behind other windows). /// </summary> /// <param name="hWnd"> The window to move to the background. </param> /// <remarks> /// <para> /// Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />. /// </para> /// </remarks> public static void MoveWindowToBackground(IntPtr hWnd) { if (hWnd == IntPtr.Zero) { return; } WindowsWindow.SetWindowPos(hWnd, WindowsWindow.HwndBottom, 0, 0, 0, 0, WindowsWindow.SwpNosize | WindowsWindow.SwpNomove | WindowsWindow.SwpNoactivate); }
/// <summary> /// Moves a window to the foreground (ontop of other windows). /// </summary> /// <param name="hWnd"> The window to move to the foreground. </param> /// <remarks> /// <para> /// Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />. /// </para> /// </remarks> public static void MoveWindowToForeground(IntPtr hWnd) { if (hWnd == IntPtr.Zero) { return; } WindowsWindow.SetForegroundWindow(hWnd); WindowsWindow.BringWindowToTop(hWnd); WindowsWindow.SetActiveWindow(hWnd); }
/// <summary> /// Gets the date and time when the last user input was made. /// </summary> /// <returns> /// The date and time when the last user input was made. /// </returns> /// <remarks> /// <note type="note"> /// This method is not necessarily precise and the time since the last user input can be affected by various system /// components or settings, other applications, or connected devices. /// Therefore, only use the returned value for non-critical things which do not need to be precise. /// </note> /// </remarks> public static DateTime GetLastInput() { LASTINPUTINFO info = new LASTINPUTINFO(); info.cbSize = (uint)Marshal.SizeOf(info); WindowsWindow.GetLastInputInfo(ref info); double idleTicks = (Environment.TickCount - info.dwTime) * (-1.0); DateTime inputTimestamp = DateTime.Now.AddMilliseconds(idleTicks); return(inputTimestamp); }
/// <summary> /// Moves a window to a screen. /// </summary> /// <param name="hWnd"> The window to move. </param> /// <param name="screen"> The screen or null to move to the primary screen. </param> /// <remarks> /// <para> /// Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />. /// </para> /// </remarks> public static void MoveWindowToScreen(IntPtr hWnd, Screen screen) { if (hWnd == IntPtr.Zero) { return; } screen = screen ?? Screen.PrimaryScreen; WINDOWINFO info = new WINDOWINFO(); info.cbSize = (uint)Marshal.SizeOf(info); WindowsWindow.GetWindowInfo(hWnd, ref info); WINDOWPLACEMENT placement = new WINDOWPLACEMENT(); placement.Length = (uint)Marshal.SizeOf(placement); WindowsWindow.GetWindowPlacement(hWnd, ref placement); bool isMaximized = placement.ShowCmd == WindowsWindow.SwShowmaximized; bool isMinimized = placement.ShowCmd == WindowsWindow.SwShowminimized; bool isNormal = placement.ShowCmd == WindowsWindow.SwShownormal; int width = info.rcWindow.Right - info.rcWindow.Left; int height = info.rcWindow.Bottom - info.rcWindow.Top; if (isMaximized || isMinimized) { WindowsWindow.ShowWindow(hWnd, (int)WindowsWindow.SwShownormal); } WindowsWindow.SetWindowPos(hWnd, IntPtr.Zero, screen.WorkingArea.Left, screen.WorkingArea.Top, 0, 0, WindowsWindow.SwpNozorder | WindowsWindow.SwpNosize); if (isNormal) { WindowsWindow.SetWindowPos(hWnd, IntPtr.Zero, screen.WorkingArea.Left + ((screen.WorkingArea.Width - width) / 2), screen.WorkingArea.Top + ((screen.WorkingArea.Height - height) / 2), 0, 0, WindowsWindow.SwpNozorder | WindowsWindow.SwpNosize); } else if (isMaximized) { WindowsWindow.ShowWindow(hWnd, (int)WindowsWindow.SwShowmaximized); } else if (isMinimized) { WindowsWindow.ShowWindow(hWnd, (int)WindowsWindow.SwShowminimized); } }
/// <summary> /// Moves a window to a screen. /// </summary> /// <param name="hWnd"> The window to move. </param> /// <param name="screenIndex"> The screen index or -1 to move to the primary screen. </param> /// <remarks> /// <para> /// Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />. /// </para> /// </remarks> public static void MoveWindowToScreen(IntPtr hWnd, int screenIndex) { if (screenIndex < -1) { screenIndex = 0; } if (screenIndex >= Screen.AllScreens.Length) { screenIndex = Screen.AllScreens.Length - 1; } WindowsWindow.MoveWindowToScreen(hWnd, screenIndex == -1 ? null : Screen.AllScreens[screenIndex]); }
/// <summary> /// Gets all top-level windows. /// </summary> /// <returns> /// The array with window handles to all the top-level windows. /// An empty array is returned if no top-level windows are available. /// </returns> public static IntPtr[] FindTopWindows() { lock (WindowsWindow.FindWindowsSyncRoot) { if (WindowsWindow.FindWindowsWindows == null) { WindowsWindow.FindWindowsWindows = new List <IntPtr>(); } WindowsWindow.FindWindowsWindows.Clear(); WindowsWindow.EnumWindows(WindowsWindow.FindWindowsProc, IntPtr.Zero); return(WindowsWindow.FindWindowsWindows.ToArray()); } }
/// <summary> /// Gets the title of a window. /// </summary> /// <param name="hWnd"> The window. </param> /// <returns> /// The title of the window or null if the title cannot be retrieved or <paramref name="hWnd" /> is /// <see cref="IntPtr.Zero" />. /// </returns> /// <remarks> /// <para> /// Retrieving a window title can fail in cases the window is blocked and does not react to window messages within /// one second. /// </para> /// </remarks> public static string GetWindowTitle(IntPtr hWnd) { if (hWnd == IntPtr.Zero) { return(null); } int length = -1; WindowsWindow.SendMessageTimeout1(hWnd, WindowsWindow.WmGettextlength, IntPtr.Zero, IntPtr.Zero, WindowsWindow.SmtoAbortifhung | WindowsWindow.SmtoNormal, 1000, ref length); if (length == -1) { return(null); } if (length == 0) { return(string.Empty); } if (length > 10240) { return(null); } StringBuilder sb = new StringBuilder(length + 1); IntPtr temp = new IntPtr(); WindowsWindow.SendMessageTimeout2(hWnd, WindowsWindow.WmGettext, new IntPtr(sb.Capacity), sb, WindowsWindow.SmtoAbortifhung | WindowsWindow.SmtoNormal, 1000, ref temp); if (temp.ToInt64() == -1) { return(null); } if (temp.ToInt64() == 0) { return(string.Empty); } string title = sb.ToString(); return(title); }
/// <summary> /// Gets all windows associated with a process. /// </summary> /// <param name="process"> The process. </param> /// <returns> /// The array with all window handles to all the windows of the specified process. /// An empty array is returned if the process does not have any windows. /// </returns> /// <exception cref="ArgumentNullException"> <paramref name="process" /> is null. </exception> public static IntPtr[] GetWindows(Process process) { if (process == null) { throw new ArgumentNullException(nameof(process)); } IntPtr[] allWindows = WindowsWindow.FindTopWindows(); IntPtr[] foundWindows = (from w in allWindows where WindowsWindow.GetProcess(w) .Id == process.Id select w).ToArray(); return(foundWindows); }
/// <summary> /// Moves a window to a new position and size. /// </summary> /// <param name="hWnd"> The window to move to a new position and size. </param> /// <param name="x"> The new x position of the window (top-left of the window). </param> /// <param name="y"> The new y position of the window (top-left of the window). </param> /// <param name="width"> The new width of the window. </param> /// <param name="height"> The new height of the window. </param> /// <remarks> /// <para> /// Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />. /// </para> /// </remarks> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="width" /> or <paramref name="height" /> is less than /// zero. /// </exception> public static void RelocateWindow(IntPtr hWnd, int x, int y, int width, int height) { if (width < 0) { throw new ArgumentOutOfRangeException(nameof(width)); } if (height < 0) { throw new ArgumentOutOfRangeException(nameof(height)); } if (hWnd == IntPtr.Zero) { return; } WindowsWindow.MoveWindow(hWnd, x, y, width, height, true); }
/// <summary> /// Gets the process associated with a window. /// </summary> /// <param name="hWnd"> The window. </param> /// <returns> /// The process to which the specified window is associated or null if no process can be found or /// <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />. /// </returns> public static Process GetProcess(IntPtr hWnd) { if (hWnd == IntPtr.Zero) { return(null); } int processId = 0; WindowsWindow.GetWindowThreadProcessId(hWnd, ref processId); Process[] processes = Process.GetProcesses(); Process process = (from p in processes where p.Id == processId select p).FirstOrDefault(); return(process); }
/// <summary> /// Gets all child windows of a window. /// </summary> /// <param name="hWnd"> The window for which all child windows are to be found. </param> /// <returns> /// The array with window handles to all the child windows of the specified window. /// An empty array is returned if no child windows are available or <paramref name="hWnd" /> is /// <see cref="IntPtr.Zero" />. /// </returns> public static IntPtr[] FindChildWindows(IntPtr hWnd) { if (hWnd == IntPtr.Zero) { return(new IntPtr[0]); } lock (WindowsWindow.FindChildWindowsSyncRoot) { if (WindowsWindow.FindChildWindowsWindows == null) { WindowsWindow.FindChildWindowsWindows = new List <IntPtr>(); } WindowsWindow.FindChildWindowsWindows.Clear(); WindowsWindow.EnumChildWindows(hWnd, WindowsWindow.FindChildWindowsProc, IntPtr.Zero); return(WindowsWindow.FindChildWindowsWindows.ToArray()); } }
/// <summary> /// Moves a window to the primary screen. /// </summary> /// <param name="hWnd"> The window to move. </param> /// <remarks> /// <para> /// Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />. /// </para> /// </remarks> public static void MoveWindowToPrimaryScreen(IntPtr hWnd) { WindowsWindow.MoveWindowToScreen(hWnd, null); }