public Image GetStaticThumbnail(IntPtr source) { var sourceContext = User32NativeMethods.GetDC(source); User32NativeMethods.GetClientRect(source, out RECT windowRect); var width = windowRect.Right - windowRect.Left; var height = windowRect.Bottom - windowRect.Top; // Check if there is anything to make thumbnail of if ((width < WINDOW_SIZE_THRESHOLD) || (height < WINDOW_SIZE_THRESHOLD)) { return(null); } var destContext = Gdi32NativeMethods.CreateCompatibleDC(sourceContext); var bitmap = Gdi32NativeMethods.CreateCompatibleBitmap(sourceContext, width, height); var oldBitmap = Gdi32NativeMethods.SelectObject(destContext, bitmap); Gdi32NativeMethods.BitBlt(destContext, 0, 0, width, height, sourceContext, 0, 0, Gdi32NativeMethods.SRCCOPY); Gdi32NativeMethods.SelectObject(destContext, oldBitmap); Gdi32NativeMethods.DeleteDC(destContext); User32NativeMethods.ReleaseDC(source, sourceContext); Image image = Image.FromHbitmap(bitmap); Gdi32NativeMethods.DeleteObject(bitmap); return(image); }
/// <summary> /// ウィンドウの表示領域を取得する /// </summary> public static Rectangle GetWindowBounds(IntPtr windowHandle) { var window = new Rectangle(); User32NativeMethods.GetWindowRect(windowHandle, ref window); return(window); }
/// <summary> /// ウィンドウを最前面に表示する /// </summary> public static void SetForceForegroundWindow(IntPtr targetHandle) { // ターゲットとなるハンドルのスレッドIDを取得する var targetThreadId = User32NativeMethods.GetWindowThreadProcessId(targetHandle, out var _); // 現在アクティブとなっているウィンドウのスレッドIDを取得する var currentActiveThreadId = User32NativeMethods.GetWindowThreadProcessId(User32NativeMethods.GetForegroundWindow(), out var _); // アクティブ処理 User32NativeMethods.SetForegroundWindow(targetHandle); if (targetThreadId == currentActiveThreadId) { // 現在アクティブなウィンドウがキャプチャ対象のウィンドウの場合は前面に持ってくる User32NativeMethods.BringWindowToTop(targetHandle); } else { // 別のプロセスがアクティブな場合は、そのプロセスにアタッチし、入力を奪う User32NativeMethods.AttachThreadInput(targetThreadId, currentActiveThreadId, true); try { // 前面に持ってくる User32NativeMethods.BringWindowToTop(targetHandle); } finally { // アタッチを解除する User32NativeMethods.AttachThreadInput(targetThreadId, currentActiveThreadId, false); } } }
public static async Task ClickAsync(IntPtr windowHandle, int x, int y, TimeSpan delayButtonUp) { User32NativeMethods.SendMessage(windowHandle, User32NativeMethods.WM_MOUSEMOVE, IntPtr.Zero, WinAPI.CreateLParam(x, y)); User32NativeMethods.SendMessage(windowHandle, User32NativeMethods.WM_LBUTTONDOWN, new IntPtr(User32NativeMethods.MK_LBUTTON), WinAPI.CreateLParam(x, y)); await Task.Delay(delayButtonUp); User32NativeMethods.SendMessage(windowHandle, User32NativeMethods.WM_LBUTTONUP, new IntPtr(User32NativeMethods.MK_LBUTTON), WinAPI.CreateLParam(x, y)); }
/// <summary> /// ウィンドウの表示領域を設定する /// </summary> public static void SetWindowBounds(IntPtr windowHandle, System.Drawing.Rectangle bounds) { var wflags = User32NativeMethods.SWP_NOZORDER | User32NativeMethods.SWP_SHOWWINDOW; User32NativeMethods.SetWindowPos(windowHandle, 0, bounds.X, bounds.Y, bounds.Width, bounds.Height, wflags); }
public void ActivateWindow(IntPtr handle) { User32NativeMethods.SetForegroundWindow(handle); int style = User32NativeMethods.GetWindowLong(handle, InteropConstants.GWL_STYLE); if ((style & InteropConstants.WS_MINIMIZE) == InteropConstants.WS_MINIMIZE) { User32NativeMethods.ShowWindowAsync(handle, InteropConstants.SW_RESTORE); } }
public void MinimizeWindow(IntPtr handle, bool enableAnimation) { if (enableAnimation) { User32NativeMethods.SendMessage(handle, InteropConstants.WM_SYSCOMMAND, InteropConstants.SC_MINIMIZE, 0); } else { WINDOWPLACEMENT param = new WINDOWPLACEMENT(); param.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT)); User32NativeMethods.GetWindowPlacement(handle, ref param); param.showCmd = WINDOWPLACEMENT.SW_MINIMIZE; User32NativeMethods.SetWindowPlacement(handle, ref param); } }
/// <summary> /// Windowハンドルとタイトルの一覧を取得する /// </summary> public static IList <(string title, IntPtr hWnd)> GetWindows() { var list = new List <(string title, IntPtr hWnd)>(); User32NativeMethods.EnumWindows(new User32NativeMethods.EnumWindowsDelegate((hWnd, lparam) => { // ウィンドウのタイトルの長さを取得し、タイトルを持つもののみ結果とする int textLen = User32NativeMethods.GetWindowTextLength(hWnd); if (textLen > 0) { // ウィンドウのタイトルを取得する StringBuilder tsb = new StringBuilder(textLen + 1); User32NativeMethods.GetWindowText(hWnd, tsb, tsb.Capacity); list.Add((tsb.ToString(), hWnd)); } return(true); }), IntPtr.Zero); return(list); }
public Bitmap Capture() { if (!EnableWriterBitmap) { UpdateWriterBitmap(); } IntPtr windowDC = User32NativeMethods.GetWindowDC(windowHandle); IntPtr hDC = writerBitmapGraphcis.GetHdc(); Gdi32NativeMethods.BitBlt(hDC, 0, 0, WriterBitmap.Width, WriterBitmap.Height, windowDC, CaptureBoundsInWindow.X, CaptureBoundsInWindow.Y, (int)Gdi32NativeMethods.TernaryRasterOperations.SRCCOPY); writerBitmapGraphcis.ReleaseHdc(hDC); User32NativeMethods.ReleaseDC(windowHandle, windowDC); return(WriterBitmap); }
public bool IsWindowMinimized(IntPtr handle) { return(User32NativeMethods.IsIconic(handle)); }
public bool IsWindowMaximized(IntPtr handle) { return(User32NativeMethods.IsZoomed(handle)); }
public (int Left, int Top, int Right, int Bottom) GetWindowPosition(IntPtr handle) { User32NativeMethods.GetWindowRect(handle, out RECT windowRectangle); return(windowRectangle.Left, windowRectangle.Top, windowRectangle.Right, windowRectangle.Bottom); }
public void MaximizeWindow(IntPtr handle) { User32NativeMethods.ShowWindowAsync(handle, InteropConstants.SW_SHOWMAXIMIZED); }
public IntPtr GetForegroundWindowHandle() { return(User32NativeMethods.GetForegroundWindow()); }
public static void Move(IntPtr windowHandle, int x, int y) { User32NativeMethods.SendMessage(windowHandle, User32NativeMethods.WM_MOUSEMOVE, IntPtr.Zero, WinAPI.CreateLParam(x, y)); }
public static void Click(IntPtr windowHandle, int x, int y) { User32NativeMethods.SendMessage(windowHandle, User32NativeMethods.WM_MOUSEMOVE, IntPtr.Zero, WinAPI.CreateLParam(x, y)); User32NativeMethods.SendMessage(windowHandle, User32NativeMethods.WM_LBUTTONDOWN, new IntPtr(User32NativeMethods.MK_LBUTTON), WinAPI.CreateLParam(x, y)); User32NativeMethods.SendMessage(windowHandle, User32NativeMethods.WM_LBUTTONUP, new IntPtr(User32NativeMethods.MK_LBUTTON), WinAPI.CreateLParam(x, y)); }
public void MoveWindow(IntPtr handle, int left, int top, int width, int height) { User32NativeMethods.MoveWindow(handle, left, top, width, height, true); }