/// <summary> /// Verifies whether the window can be activated /// </summary> /// <param name="hWnd">WIndow handle</param> /// <returns>true if it can</returns> private static bool canBeActivated(IntPtr hWnd) { if (isShellWindow(hWnd)) { return(false); } var root = User32Interop.GetAncestor(hWnd, User32Interop.GetAncestorFlags.GetRootOwner); if (getWindowPopup(root) != hWnd) { return(false); } var className = Windows.GetWindowClassName(hWnd); if (String.IsNullOrEmpty(className)) { return(false); } if (Array.IndexOf(_ignoreClassNames, className) > -1) { return(false); } if (className.StartsWith("WMP9MediaBarFlyout")) { return(false); } return(true); }
/// <summary> /// Simulates mouse left click at the indicated /// by doing a mouse down followed by a mouse up /// </summary> /// <param name="x">x position of cursor</param> /// <param name="y">y position of the cursor</param> public static void ClickLeftMouseButton(int x, int y) { float screenWidth = Screen.PrimaryScreen.Bounds.Width; float screenHeight = Screen.PrimaryScreen.Bounds.Height; var mouseInput = new User32Interop.INPUT { type = User32Interop.SendInputEventType.InputMouse }; //mouseInput.mkhi.mi.dx = x; //mouseInput.mkhi.mi.dy = y; mouseInput.mkhi.mi.dx = (int)Math.Round(x * (65535 / screenWidth), 0); mouseInput.mkhi.mi.dy = (int)Math.Round(y * (65535 / screenHeight), 0); mouseInput.mkhi.mi.mouseData = 0; mouseInput.mkhi.mi.dwFlags = User32Interop.MouseEventFlags.MOUSEEVENTF_MOVE | User32Interop.MouseEventFlags.MOUSEEVENTF_ABSOLUTE; User32Interop.SendInput(1, ref mouseInput, Marshal.SizeOf(new User32Interop.INPUT())); var inputDown = new User32Interop.INPUT(); inputDown.mkhi.mi.dx = 0; inputDown.mkhi.mi.dy = 0; inputDown.mkhi.mi.mouseData = 0; inputDown.mkhi.mi.dwFlags = User32Interop.MouseEventFlags.MOUSEEVENTF_LEFTDOWN; User32Interop.SendInput(1, ref inputDown, Marshal.SizeOf(new User32Interop.INPUT())); inputDown.mkhi.mi.dwFlags = User32Interop.MouseEventFlags.MOUSEEVENTF_LEFTUP; User32Interop.SendInput(1, ref inputDown, Marshal.SizeOf(new User32Interop.INPUT())); }
/// <summary> /// Turns caps lock on/off and sets the state of the /// shift key accordingly /// </summary> /// <param name="onOff">true to turn on</param> private static void setCapsLockState(bool onOff) { const int KEYEVENTF_EXTENDEDKEY = 0x1; const int KEYEVENTF_KEYUP = 0x2; if (onOff) { if (!Form.IsKeyLocked(Keys.CapsLock)) { // turn on capslock User32Interop.keybd_event((byte)Keys.CapsLock, 0xAA, 0, UIntPtr.Zero); User32Interop.keybd_event((byte)Keys.CapsLock, 0xAA, (uint)KEYEVENTF_KEYUP, UIntPtr.Zero); turnOn(_shift); _stickyShift = true; } } else { if (Form.IsKeyLocked(Keys.CapsLock)) { // turn off capslock User32Interop.keybd_event((byte)Keys.CapsLock, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0); User32Interop.keybd_event((byte)Keys.CapsLock, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0); ClearShift(); } } }
public static Process GetProcessForWindow(IntPtr hwnd) { int pid; User32Interop.GetWindowThreadProcessId(hwnd, out pid); return(Process.GetProcessById(pid)); }
/// <summary> /// Simulates a middle button double click at the current /// cursor position /// </summary> public static void SimulateMiddleMouseDoubleClick() { int mouseX = Cursor.Position.X; int mouseY = Cursor.Position.Y; User32Interop.mouse_event(User32Interop.MOUSEEVENTF_MIDDLEDOWN | User32Interop.MOUSEEVENTF_MIDDLEUP, mouseX, mouseY, 0, 0); User32Interop.mouse_event(User32Interop.MOUSEEVENTF_MIDDLEDOWN | User32Interop.MOUSEEVENTF_MIDDLEUP, mouseX, mouseY, 0, 0); }
/// Simulates a right button double click at the current /// cursor position public static void SimulateRightMouseDoubleClick() { int mouseX = Cursor.Position.X; int mouseY = Cursor.Position.Y; User32Interop.mouse_event(User32Interop.MOUSEEVENTF_RIGHTDOWN | User32Interop.MOUSEEVENTF_RIGHTUP, mouseX, mouseY, 0, 0); User32Interop.mouse_event(User32Interop.MOUSEEVENTF_RIGHTDOWN | User32Interop.MOUSEEVENTF_RIGHTUP, mouseX, mouseY, 0, 0); }
/// <summary> /// Synchronously enumerates windows and returns a window list /// </summary> /// <returns>list of windows</returns> public static List <WindowInfo> Enumerate(bool excludeThisProcess = true) { _currentProcess = Process.GetCurrentProcess(); _excludeThisProcess = excludeThisProcess; _shellWindowHandle = User32Interop.GetShellWindow(); windowList = new List <WindowInfo>(); User32Interop.EnumDesktopWindows(IntPtr.Zero, enumWindowsCallback, IntPtr.Zero); return(windowList); }
/// <summary> /// Callback function for the enumwindows function /// </summary> /// <param name="winHandle">enumerated window handle</param> /// <param name="lParam">nod used </param> /// <returns></returns> private static bool enumWindowsCallback(IntPtr winHandle, int lParam) { if (!User32Interop.IsWindowVisible(winHandle)) { return(true); } if (_excludeThisProcess) { uint pid = 0; User32Interop.GetWindowThreadProcessId(winHandle, out pid); if (pid == 0 || pid == _currentProcess.Id) { return(true); } } if (!Windows.IsApplicationWindow(winHandle)) { return(true); } if (!canBeActivated(winHandle)) { return(true); } if (Windows.IsCloakedWindow(winHandle)) { return(true); } var buffer = new StringBuilder(1024); User32Interop.GetWindowText(winHandle, buffer, buffer.Capacity); var windowTitle = buffer.ToString(); // save the window only if it is visible and contains a title // and has an app icon (this criterium is needed to not display "Start" and "Program Manager") // this can be altered in the future to add other filtering criteria if (!string.IsNullOrEmpty(windowTitle)) { Log.Debug("hWnd=" + winHandle + " windowTitle=" + windowTitle); var info = new WindowInfo(winHandle, windowTitle); windowList.Add(info); } // return true to continue enumerating windows. return(true); }
/// <summary> /// Gets window class long pointer for hWnd /// </summary> /// <param name="hWnd">window handle</param> /// <param name="nIndex">index number</param> /// <returns>window class long</returns> private static IntPtr getClassLongPtr(IntPtr hWnd, int nIndex) { try { if (IntPtr.Size > 4) { return(User32Interop.GetClassLongPtr64(hWnd, nIndex)); } uint ret = User32Interop.GetClassLongPtr32(hWnd, nIndex); return(new IntPtr((int)ret)); // without the cast, it may result in an overflow } catch { return(IntPtr.Zero); } }
/// <summary> /// Moves the specified window to the monitor other than the one it is /// currently in /// </summary> /// <param name="handle">Handle to the window</param> public static void MoveWindowToOtherMonitor(IntPtr handle) { if (handle == IntPtr.Zero || !MultipleMonitors || Windows.IsMinimized(handle)) { return; } var scr = GetMonitorForHandle(handle); var other = scr.Primary ? GetSecondaryMonitor() : GetPrimaryMonitor(); if (Windows.IsMaximized(handle)) { Windows.RestoreWindow(handle); } User32Interop.RECT rect; User32Interop.GetWindowRect(handle, out rect); User32Interop.MoveWindow(handle, other.WorkingArea.Left, other.WorkingArea.Top, rect.right - rect.left, rect.bottom - rect.top, true); }
/// <summary> /// Callback function for the enumwindows function /// </summary> /// <param name="winHandle">enumerated window handle</param> /// <param name="lParam">nod used </param> /// <returns></returns> private static bool enumWindowsCallback(IntPtr winHandle, int lParam) { var buffer = new StringBuilder(1024); User32Interop.GetWindowText(winHandle, buffer, buffer.Capacity); var windowTitle = buffer.ToString(); // save the window only if it is visible and contains a title // and has an app icon (this criterium is needed to not display "Start" and "Program Manager") // this can be altered in the future to add other filtering criteria if (User32Interop.IsWindowVisible(winHandle) && (!string.IsNullOrEmpty(windowTitle)) && (GetAppIcon(winHandle) != null)) { Log.Debug("hWnd=" + winHandle + " windowTitle=" + windowTitle); var info = new WindowInfo(winHandle, windowTitle); windowList.Add(info); } // return true to continue enumerating windows. return(true); }
/// <summary> /// Gets the pop window (if any) of the specified window /// </summary> /// <param name="window">Window handle</param> /// <returns>handle if found, IntPtr.Zero else</returns> private static IntPtr getWindowPopup(IntPtr window) { var windowLevel = 32; var targetWindow = window; while (windowLevel-- > 0) { IntPtr popupWindow = User32Interop.GetLastActivePopup(targetWindow); if (User32Interop.IsWindowVisible(popupWindow)) { return(popupWindow); } if (popupWindow == targetWindow) { return(IntPtr.Zero); } targetWindow = popupWindow; } return(IntPtr.Zero); }
/// <summary> /// Gets the icon associated with the specified window /// </summary> /// <param name="winHandle">window handle</param> /// <returns>the icon, null if it can't find one</returns> public static Icon GetAppIcon(IntPtr winHandle) { Log.Debug("hWnd=" + winHandle); IntPtr hIcon = User32Interop.SendMessage(winHandle, WM_GETICON, ICON_BIG, 0); if (hIcon == IntPtr.Zero) { hIcon = User32Interop.SendMessage(winHandle, WM_GETICON, ICON_SMALL, 0); if (hIcon == IntPtr.Zero) { hIcon = User32Interop.SendMessage(winHandle, WM_GETICON, ICON_SMALL2, 0); if (hIcon == IntPtr.Zero) { hIcon = getClassLongPtr(winHandle, GCL_HICON); if (hIcon == IntPtr.Zero) { hIcon = getClassLongPtr(winHandle, GCL_HICONSM); if (hIcon == IntPtr.Zero) { return(null); } } } } } Icon icon = null; if (hIcon != IntPtr.Zero) { icon = Icon.FromHandle(hIcon); } return(icon); }
/// <summary> /// Returns if the specified key is printable or not /// </summary> /// <param name="key">key to check</param> /// <returns>true if it is</returns> public static bool IsPrintable(Keys key) { return(key == Keys.Enter || !char.IsControl((char)User32Interop.MapVirtualKey((int)key, 2))); }
/// <summary> /// Is capslock engaged? /// </summary> /// <returns></returns> public static bool IsNumLockOn() { return(User32Interop.GetKeyState(VK_NUMLOCK) != 0); }
/// <summary> /// Is capslock engaged? /// </summary> /// <returns></returns> public static bool IsCapsLockOn() { return(User32Interop.GetKeyState(VK_CAPITAL) != 0); }
/// <summary> /// Simulates a right mouse drag at the current /// cursor position by sending a right button down event /// </summary> public static void SimulateRightMouseDrag() { User32Interop.mouse_event(User32Interop.MOUSEEVENTF_RIGHTDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0); }
/// <summary> /// Simulates a middle button drag at the current /// cursor position by sending a middle button down event /// </summary> public static void SimulateMiddleMouseDrag() { User32Interop.mouse_event(User32Interop.MOUSEEVENTF_MIDDLEDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0); }
/// <summary> /// Simulates a left click at the current /// mouse position /// </summary> public static void SimulateLeftMouseClick() { User32Interop.mouse_event(User32Interop.MOUSEEVENTF_LEFTDOWN | User32Interop.MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0); }
/// <summary> /// Synchronously enumerates windows and returns a window list /// </summary> /// <returns>list of windows</returns> public static List <WindowInfo> Enumerate() { windowList = new List <WindowInfo>(); User32Interop.EnumDesktopWindows(IntPtr.Zero, enumWindowsCallback, IntPtr.Zero); return(windowList); }