public static Rectangle getWindowRect(Pointer hWnd) throws JnaUtilException { if (hWnd == null) { throw new JnaUtilException( "Failed to getWindowRect since Pointer hWnd is null"); } Rectangle result = null; RECT rect = new RECT(); boolean rectOK = user32.GetWindowRect(hWnd, rect); if (rectOK) { int x = rect.left; int y = rect.top; int width = rect.right - rect.left; int height = rect.bottom - rect.top; result = new Rectangle(x, y, width, height); } return result; }
/** * @param args (ignored) */ public static void main(String[] args) { System.out.println("Installed Physical Monitors: " + User32.INSTANCE.GetSystemMetrics(WinUser.SM_CMONITORS)); User32.INSTANCE.EnumDisplayMonitors(null, null, new MONITORENUMPROC() { @Override public int apply(HMONITOR hMonitor, HDC hdc, RECT rect, LPARAM lparam) { enumerate(hMonitor); return 1; } }, new LPARAM(0)); }
public BufferedImage capture(final HWND hWnd) { final HDC hdcWindow = User32.INSTANCE.GetDC(hWnd); final HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow); final RECT bounds = new RECT(); User32Extra.INSTANCE.GetClientRect(hWnd, bounds); final int width = bounds.right - bounds.left; final int height = bounds.bottom - bounds.top; if (width * height <= 0) { return null; } final HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height); final HANDLE hOld = GDI32.INSTANCE.SelectObject(hdcMemDC, hBitmap); GDI32Extra.INSTANCE.BitBlt(hdcMemDC, 0, 0, width, height, hdcWindow, 0, 0, WinGDIExtra.SRCCOPY); GDI32.INSTANCE.SelectObject(hdcMemDC, hOld); GDI32.INSTANCE.DeleteDC(hdcMemDC); final BITMAPINFO bmi = new BITMAPINFO(); bmi.bmiHeader.biWidth = width; bmi.bmiHeader.biHeight = -height; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = WinGDI.BI_RGB; final Memory buffer = new Memory(width * height * 4); GDI32.INSTANCE.GetDIBits(hdcWindow, hBitmap, 0, height, buffer, bmi, WinGDI.DIB_RGB_COLORS); final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.setRGB(0, 0, width, height, buffer.getIntArray(0, width * height), 0, width); GDI32.INSTANCE.DeleteObject(hBitmap); User32.INSTANCE.ReleaseDC(hWnd, hdcWindow); return image; }
public MonitorControllerJna() { System.out.println("Monitors: " + User32.INSTANCE.GetSystemMetrics(User32.SM_CMONITORS)); User32.INSTANCE.EnumDisplayMonitors(null, null, new MONITORENUMPROC() { @Override public int apply(HMONITOR hMonitor, HDC hdc, RECT rect, LPARAM lparam) { System.out.println("Monitor handle: " + hMonitor); MONITORINFOEX info = new MONITORINFOEX(); User32.INSTANCE.GetMonitorInfo(hMonitor, info); System.out.println(info.rcMonitor); DWORDByReference pdwNumberOfPhysicalMonitors = new DWORDByReference(); Dxva2.INSTANCE.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors); int monitorCount = pdwNumberOfPhysicalMonitors.getValue().intValue(); System.out.println("Physical monitors for " + hMonitor + ": " + monitorCount); PHYSICAL_MONITOR[] physMons = new PHYSICAL_MONITOR[monitorCount]; Dxva2.INSTANCE.GetPhysicalMonitorsFromHMONITOR(hMonitor, monitorCount, physMons); for (PHYSICAL_MONITOR mon : physMons) { String desc = new String(mon.szPhysicalMonitorDescription); monitors.add(new MonitorJna(mon.hPhysicalMonitor, desc)); } return 1; } }, new LPARAM(0)); }
public WindowHolder(HWND parentWindow, RECT rect) { super(); this.parentWindow = parentWindow; this.rect = rect; }
@Override public void update(float delta) { compWin.update(delta, window); if (run) { if (thumbnail.getValue() != 0) DWMapiExt.INSTANCE.DwmUnregisterThumbnail(new INT_PTR(thumbnail.getValue())); DWMapiExt.INSTANCE.DwmRegisterThumbnail(local, hwndWin, thumbnail); SIZE size = new SIZE(); DWMapiExt.INSTANCE.DwmQueryThumbnailSourceSize(new INT_PTR(thumbnail.getValue()), size); size.read(); float aspectX = (float) size.cx / (float) size.cy; float aspectY = (float) size.cy / (float) size.cx; DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.dwFlags = DWM_TNP.DWM_TNP_VISIBLE | DWM_TNP.DWM_TNP_RECTDESTINATION | DWM_TNP.DWM_TNP_OPACITY; props.fVisible = true; props.opacity = (byte) 0xFF; props.rcDestination = new RECT(); if (size.cx > size.cy) { props.rcDestination.left = 5; props.rcDestination.top = 100 - (int) (190 / aspectX / 2); props.rcDestination.right = props.rcDestination.left + 190; props.rcDestination.bottom = props.rcDestination.top + (int) (190 / aspectX); } else { props.rcDestination.left = 100 - (int) (190 / aspectY / 2); props.rcDestination.top = 5; props.rcDestination.right = props.rcDestination.left + (int) (190 / aspectY); props.rcDestination.bottom = props.rcDestination.top + 190; } props.write(); DWMapiExt.INSTANCE.DwmUpdateThumbnailProperties(new INT_PTR(thumbnail.getValue()), props); //DWMapiExt.INSTANCE.InvokeAeroPeek(true, hwndWin, local, 3, new INT_PTR(32), 0x3244); //DWMapiExt.INSTANCE.DwmpActivateLivePreview(1, hwndWin, local, 1); TaskManager.addTask(() -> window.setVisible(true)); run = false; } }
/** * Searches a rectangle of pixels for the pixel color provided. * * @param left * left coordinate of rectangle. * @param top * top coordinate of rectangle. * @param right * right coordinate of rectangle. * @param bottom * bottom coordinate of rectangle. * @param color * Colour value of pixel to find (in decimal or hex). * @param shadeVariation * A number between 0 and 255 to indicate the allowed number of * shades of variation of the red, green, and blue components of * the colour. Default is 0 (exact match). * @param step * Instead of searching each pixel use a value larger than 1 to * skip pixels (for speed). E.g. A value of 2 will only check * every other pixel. Default is 1. * @return Return a 2 element array containing the pixel's coordinates if * success, return null if color is not found. */ public static int[] search(final int left, final int top, final int right, final int bottom, final int color, Integer shadeVariation, Integer step) { final POINT point = new POINT(); if ((shadeVariation == null) || (shadeVariation < 0) || (shadeVariation > 255)) { shadeVariation = 0; } if ((step == null) || (step <= 0)) { step = DEFAULT_STEP; } RECT rect = new RECT(); rect.left = left; rect.top = top; rect.right = right; rect.bottom = bottom; autoItX.AU3_PixelSearch(rect, color, shadeVariation, step, point); return hasError() ? null : new int[] { point.x, point.y }; }
public Rectangle getHSWindowBounds() { RECT bounds = new RECT(); User32Extra.INSTANCE.GetWindowRect(windowHandle, bounds); return bounds.toRectangle(); }
/** * Retrieves the size of a given window's client area. * * If the window is minimized, the returned height is null. However, * getClientSize works correctly on (non-minimized) hidden windows. If the * window title "Program Manager" is used, the function will return the size * of the desktop. getClientSize("") matches the active window. If multiple * windows match the criteria, the most recently active window is used. * * @param title * The title of the window to read. * @param text * The text of the window to read. * @return Returns the size of the window's client area if success, returns * null if windows is not found or window is minimized. */ public static int[] getClientSize(final String title, final String text) { int[] clientSize = null; if (!minimized(title, text)) { RECT rect = new RECT(); autoItX.AU3_WinGetClientSize(stringToWString(defaultString(title)), stringToWString(text), rect); if (!hasError()) { clientSize = new int[] { rect.right - rect.left, rect.bottom - rect.top }; } } return clientSize; }
/** * Retrieves the position of a given window. * * getPos returns null for minimized windows, but works fine with * (non-minimized) hidden windows. If the window title "Program Manager" is * used, the function will return [0, 0]. If multiple windows match the * criteria, the most recently active window is used. * * @param title * The title of the window to read. * @param text * The text of the window to read. * @return Returns the position of the window if success, return null if * windows is not found or window is minimized. */ public static int[] getPos(final String title, final String text) { int[] pos = null; if (!Win.minimized(title, text)) { RECT rect = new RECT(); autoItX.AU3_WinGetPos(stringToWString(defaultString(title)), stringToWString(text), rect); if (!hasError()) { pos = new int[] { rect.left, rect.top }; } } return pos; }
/** * Retrieves the size of a given window. * * getSize returns null for minimized windows, but works fine with * (non-minimized) hidden windows. If the window title "Program Manager" is * used, the function will return the size of the desktop. If multiple * windows match the criteria, the most recently active window is used. * * @param title * The title of the window to read. * @param text * The text of the window to read. * @return Returns the size of the window if success, returns null if * windows is not found or window is minimized. */ public static int[] getSize(final String title, final String text) { int[] size = null; if (!minimized(title, text)) { RECT rect = new RECT(); autoItX.AU3_WinGetPos(stringToWString(defaultString(title)), stringToWString(text), rect); if (!hasError()) { size = new int[] { rect.right - rect.left, rect.bottom - rect.top }; } } return size; }
/** * Generates a checksum for a region of pixels. * * Performing a checksum of a region is very time consuming, so use the * smallest region you are able to reduce CPU load. On some machines a * checksum of the whole screen could take many seconds! * * A checksum only allows you to see if "something" has changed in a region * - it does not tell you exactly what has changed. * * When using a step value greater than 1 you must bear in mind that the * checksumming becomes less reliable for small changes as not every pixel * is checked. * * @param left * left coordinate of rectangle. * @param top * top coordinate of rectangle. * @param right * right coordinate of rectangle. * @param bottom * bottom coordinate of rectangle. * @param step * Instead of checksumming each pixel use a value larger than 1 * to skip pixels (for speed). E.g. A value of 2 will only check * every other pixel. Default is 1. * @return Returns the checksum value of the region. */ public static int checksum(final int left, final int top, final int right, final int bottom, Integer step) { if ((step == null) || (step <= 0)) { step = DEFAULT_STEP; } RECT rect = new RECT(); rect.left = left; rect.top = top; rect.right = right; rect.bottom = bottom; return autoItX.AU3_PixelChecksum(rect, step); }
/** * Retrieves the position of a control relative to it's window. * * When using a control name in the Control functions, you need to add a * number to the end of the name to indicate which control. For example, if * there two controls listed called "MDIClient", you would refer to these as * "MDIClient1" and "MDIClient2". * * @param title * The title of the window to access. * @param text * The text of the window to access. * @param control * The control to interact with. * @return Returns the position of the control if success, returns null if * failed. */ public static int[] getPos(final String title, final String text, final String control) { RECT rect = new RECT(); autoItX.AU3_ControlGetPos(stringToWString(defaultString(title)), stringToWString(text), stringToWString(defaultString(control)), rect); return hasError() ? null : new int[] { rect.left, rect.top }; }
/** * Retrieves the size of a control. * * When using a control name in the Control functions, you need to add a * number to the end of the name to indicate which control. For example, if * there two controls listed called "MDIClient", you would refer to these as * "MDIClient1" and "MDIClient2". * * @param title * The title of the window to access. * @param text * The text of the window to access. * @param control * The control to interact with. * @return Returns the size of the control if success, returns null if * failed. */ public static int[] getSize(final String title, final String text, final String control) { RECT rect = new RECT(); autoItX.AU3_ControlGetPos(stringToWString(defaultString(title)), stringToWString(text), stringToWString(defaultString(control)), rect); return hasError() ? null : new int[] { rect.right - rect.left, rect.bottom - rect.top }; }
private static BufferedImage capture(HWND hWnd) throws WindowNotFoundException { HDC hdcWindow = User32.INSTANCE.GetDC(hWnd); HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow); RECT bounds = new RECT(); User32Extra.INSTANCE.GetClientRect(hWnd, bounds); int width = bounds.right - bounds.left; int height = bounds.bottom - bounds.top; if(width == 0 || height == 0) throw new peeknick.errormanager.WindowNotFoundException(); HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height); HANDLE hOld = GDI32.INSTANCE.SelectObject(hdcMemDC, hBitmap); GDI32Extra.INSTANCE.BitBlt(hdcMemDC, 0, 0, width, height, hdcWindow, 0, 0, WinGDIExtra.SRCCOPY); GDI32.INSTANCE.SelectObject(hdcMemDC, hOld); GDI32.INSTANCE.DeleteDC(hdcMemDC); BITMAPINFO bmi = new BITMAPINFO(); bmi.bmiHeader.biWidth = width; bmi.bmiHeader.biHeight = -height; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = WinGDI.BI_RGB; Memory buffer = new Memory(width * height * 4); GDI32.INSTANCE.GetDIBits(hdcWindow, hBitmap, 0, height, buffer, bmi, WinGDI.DIB_RGB_COLORS); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.setRGB(0, 0, width, height, buffer.getIntArray(0, width * height), 0, width); GDI32.INSTANCE.DeleteObject(hBitmap); User32.INSTANCE.ReleaseDC(hWnd, hdcWindow); return image; }
/** * Retrieves the position and size of a control relative to it's window. * * When using a control name in the Control functions, you need to add a * number to the end of the name to indicate which control. For example, * if there two controls listed called "MDIClient", you would refer to * these as "MDIClient1" and "MDIClient2". * * @param title * The title of the window to access. * @param text * The text of the window to access. * @param control * The control to interact with. * @param rect */ public void AU3_ControlGetPos(final WString title, final WString text, final WString control, final RECT rect);
/** * Generates a checksum for a region of pixels. * * Performing a checksum of a region is very time consuming, so use the * smallest region you are able to reduce CPU load. On some machines a * checksum of the whole screen could take many seconds! * * A checksum only allows you to see if "something" has changed in a * region - it does not tell you exactly what has changed. * * When using a step value greater than 1 you must bear in mind that the * checksumming becomes less reliable for small changes as not every * pixel is checked. * * @param rect * position and size of rectangle * @param step * Instead of checksumming each pixel use a value larger than * 1 to skip pixels (for speed). E.g. A value of 2 will only * check every other pixel. Default is 1. * @return Returns the checksum value of the region. */ public int AU3_PixelChecksum(final RECT rect, final Integer step);
/** * Searches a rectangle of pixels for the pixel color provided. * * @param rect * position and size of rectangle. * @param color * Colour value of pixel to find (in decimal or hex). * @param shadeVariation * A number between 0 and 255 to indicate the allowed number * of shades of variation of the red, green, and blue * components of the colour. Default is 0 (exact match). * @param step * Instead of searching each pixel use a value larger than 1 * to skip pixels (for speed). E.g. A value of 2 will only * check every other pixel. Default is 1. * @param point * Return the pixel's coordinates if success, sets * oAutoIt.error to 1 if color is not found. */ public void AU3_PixelSearch(RECT rect, int color, Integer shadeVariation, Integer step, POINT point);
/** * Retrieves the size of a given window's client area. * * If the window is minimized, the returned width and height values are * both zero. However, WinGetClientSize works correctly on * (non-minimized) hidden windows. If the window title "Program Manager" * is used, the function will return the size of the desktop. * WinGetClientSize("") matches the active window. If multiple windows * match the criteria, the most recently active window is used. * * @param title * The title of the window to read. * @param text * The text of the window to read. * @param rect */ public void AU3_WinGetClientSize(final WString title, final WString text, RECT rect);
/** * Retrieves the position and size of a given window. * * WinGetPos returns negative numbers such as -32000 for minimized * windows, but works fine with (non-minimized) hidden windows. If the * window title "Program Manager" is used, the function will return the * size of the desktop. If multiple windows match the criteria, the most * recently active window is used. * * @param title * The title of the window to read. * @param text * The text of the window to read. * @param rect */ public void AU3_WinGetPos(final WString title, final WString text, RECT rect);
boolean GetWindowRect(Pointer hWnd, WinDef.RECT rect);
public boolean GetClientRect(HWND hWnd, RECT rect);
boolean GetWindowRect(Pointer hWnd, RECT rect);