Search in sources :

Example 41 with User32

use of com.sun.jna.platform.win32.User32 in project jna by java-native-access.

the class User32Test method testGetIconInfo.

@Test
public void testGetIconInfo() throws Exception {
    final ICONINFO iconInfo = new ICONINFO();
    final HANDLE hImage = User32.INSTANCE.LoadImage(null, new File(getClass().getResource("/res/test_icon.ico").toURI()).getAbsolutePath(), WinUser.IMAGE_ICON, 0, 0, WinUser.LR_LOADFROMFILE);
    try {
        // obtain test icon from classpath
        if (!User32.INSTANCE.GetIconInfo(new HICON(hImage), iconInfo))
            throw new Exception("Invocation of User32.GetIconInfo() failed: " + Kernel32Util.getLastErrorMessage());
        iconInfo.read();
    } finally {
        if (iconInfo.hbmColor != null && iconInfo.hbmColor.getPointer() != Pointer.NULL)
            GDI32.INSTANCE.DeleteObject(iconInfo.hbmColor);
        if (iconInfo.hbmMask != null && iconInfo.hbmMask.getPointer() != Pointer.NULL)
            GDI32.INSTANCE.DeleteObject(iconInfo.hbmMask);
    }
}
Also used : ICONINFO(com.sun.jna.platform.win32.WinGDI.ICONINFO) HICON(com.sun.jna.platform.win32.WinDef.HICON) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) File(java.io.File) AWTException(java.awt.AWTException) Test(org.junit.Test)

Example 42 with User32

use of com.sun.jna.platform.win32.User32 in project HearthStats.net-Uploader by HearthStats.

the class ProgramHelperWindows method _getScreenCaptureWindows.

private BufferedImage _getScreenCaptureWindows(HWND hWnd) {
    HDC hdcWindow = User32.INSTANCE.GetDC(hWnd);
    HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow);
    RECT bounds = new RECT();
    User32Extra.INSTANCE.GetClientRect(hWnd, bounds);
    // check to make sure the window's not minimized
    if (bounds.toRectangle().width >= 1024) {
        if (isMinimised) {
            _notifyObserversOfChangeTo("Hearthstone window restored");
            isMinimised = false;
        }
        if (isFullScreen(bounds.toRectangle())) {
            if (!isFullscreenFlag) {
                _notifyObserversOfChangeTo("Hearthstone running in fullscreen");
                isFullscreenFlag = true;
            }
            return null;
        } else {
            int width = bounds.right - bounds.left;
            int height = bounds.bottom - bounds.top;
            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;
        }
    }
    if (!isMinimised) {
        // that the window has been minimised.
        if (minimisedCount < ITERATIONS_FOR_MINIMISE) {
            minimisedCount++;
        } else {
            _notifyObserversOfChangeTo("Warning! Hearthstone minimized. No detection possible.");
            isMinimised = true;
            minimisedCount = 0;
        }
    }
    return null;
}
Also used : RECT(com.sun.jna.platform.win32.WinDef.RECT) HDC(com.sun.jna.platform.win32.WinDef.HDC) Memory(com.sun.jna.Memory) BITMAPINFO(com.sun.jna.platform.win32.WinGDI.BITMAPINFO) HBITMAP(com.sun.jna.platform.win32.WinDef.HBITMAP) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) BufferedImage(java.awt.image.BufferedImage)

Example 43 with User32

use of com.sun.jna.platform.win32.User32 in project HearthStats.net-Uploader by HearthStats.

the class ProgramHelperWindows method getWindowHandle.

private HWND getWindowHandle() {
    // Cache the window handle for five seconds to reduce CPU load and (possibly) minimise memory leaks
    long currentTime = System.currentTimeMillis();
    if (currentTime < lastWindowsHandleCheck + 5000) {
        // It has been less than five seconds since the last check, so use the cached value
        return windowHandle;
    } else {
        debugLog.debug("Updating window handle ({}ms since last update)", currentTime - lastWindowsHandleCheck);
        lastWindowsHandleCheck = currentTime;
        windowHandle = null;
    }
    User32.INSTANCE.EnumWindows(new WNDENUMPROC() {

        @Override
        public boolean callback(HWND hWnd, Pointer arg1) {
            int titleLength = User32.INSTANCE.GetWindowTextLength(hWnd) + 1;
            char[] title = new char[titleLength];
            User32.INSTANCE.GetWindowText(hWnd, title, titleLength);
            String wText = Native.toString(title);
            if (wText.isEmpty()) {
                return true;
            }
            PointerByReference pointer = new PointerByReference();
            User32DLL.GetWindowThreadProcessId(hWnd, pointer);
            Pointer process = Kernel32.OpenProcess(Kernel32.PROCESS_QUERY_INFORMATION | Kernel32.PROCESS_VM_READ, false, pointer.getValue());
            Psapi.GetModuleBaseNameW(process, null, baseNameBuffer, STRING_BUFFER_LENGTH);
            String baseNameString = Native.toString(baseNameBuffer);
            // see https://github.com/JeromeDane/HearthStats.net-Uploader/issues/66#issuecomment-33829132
            User32.INSTANCE.GetClassName(hWnd, classNameBuffer, STRING_BUFFER_LENGTH);
            String classNameString = Native.toString(classNameBuffer);
            if (baseNameString.equals(processName) && classNameString.equals("UnityWndClass")) {
                windowHandle = hWnd;
                if (windowHandleId == null) {
                    windowHandleId = windowHandle.toString();
                    if (lastKnownWindowHandleId == null || lastKnownWindowHandleId != windowHandleId) {
                        // The window handle has changed, so try to find the location the HearthStats executable. This is used to
                        // find the HS log file. Only compatible with Windows Vista and later, so we skip for Windows XP.
                        lastKnownWindowHandleId = windowHandleId;
                        if (Environment.isOsVersionAtLeast(6, 0)) {
                            debugLog.debug("Windows version is Vista or later so the location of the Hearthstone is being determined from the process");
                            Kernel32.QueryFullProcessImageNameW(process, 0, processFileNameBuffer, lpdwSize);
                            String processFileNameString = Native.toString(processFileNameBuffer);
                            if (processFileNameString != null) {
                                int lastSlash = processFileNameString.lastIndexOf('\\');
                                hearthstoneProcessFolder = processFileNameString.substring(0, lastSlash);
                            }
                        }
                    }
                    _notifyObserversOfChangeTo("Hearthstone window found with process name " + processName);
                }
            }
            return true;
        }
    }, null);
    // notify of window lost
    if (windowHandle == null && windowHandleId != null) {
        _notifyObserversOfChangeTo("Hearthstone window with process name " + processName + " closed");
        windowHandleId = null;
    }
    return windowHandle;
}
Also used : HWND(com.sun.jna.platform.win32.WinDef.HWND) PointerByReference(com.sun.jna.ptr.PointerByReference) WNDENUMPROC(com.sun.jna.platform.win32.WinUser.WNDENUMPROC) Pointer(com.sun.jna.Pointer)

Example 44 with User32

use of com.sun.jna.platform.win32.User32 in project HearthStats.net-Uploader by HearthStats.

the class ProgramHelperWindows method bringWindowToForeground.

public boolean bringWindowToForeground() {
    HWND currentWindowHandle = getWindowHandle();
    if (currentWindowHandle == null) {
        debugLog.debug("Cannot run bringWindowToForeground() because window handle is null");
        return false;
    } else {
        User32.INSTANCE.ShowWindow(currentWindowHandle, User32.SW_SHOW);
        User32.INSTANCE.SetForegroundWindow(currentWindowHandle);
        return true;
    }
}
Also used : HWND(com.sun.jna.platform.win32.WinDef.HWND)

Example 45 with User32

use of com.sun.jna.platform.win32.User32 in project HearthStats.net-Uploader by HearthStats.

the class JnaUtil method getWindowRect.

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;
}
Also used : RECT(com.sun.jna.platform.win32.WinDef.RECT) Rectangle(java.awt.Rectangle)

Aggregations

Test (org.junit.Test)21 HWND (com.sun.jna.platform.win32.WinDef.HWND)20 HDC (com.sun.jna.platform.win32.WinDef.HDC)10 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)8 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)8 Pointer (com.sun.jna.Pointer)7 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)7 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)7 RECT (com.sun.jna.platform.win32.WinDef.RECT)6 PIXELFORMATDESCRIPTOR (com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR)6 BufferedImage (java.awt.image.BufferedImage)5 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)4 HICON (com.sun.jna.platform.win32.WinDef.HICON)4 POINT (com.sun.jna.platform.win32.WinDef.POINT)4 PointerByReference (com.sun.jna.ptr.PointerByReference)4 File (java.io.File)4 Memory (com.sun.jna.Memory)3 DesktopWindow (com.sun.jna.platform.DesktopWindow)3 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)3 HBITMAP (com.sun.jna.platform.win32.WinDef.HBITMAP)3