Search in sources :

Example 36 with User32

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

the class WindowUtilsTest method testGetIconSize.

public void testGetIconSize() throws Exception {
    final JFrame w = new JFrame();
    try {
        final BufferedImage expectedIcon = ImageIO.read(new FileInputStream(new File(getClass().getResource("/res/test_icon.png").getPath())));
        w.setIconImage(expectedIcon);
        w.setVisible(true);
        Pointer p = Native.getComponentPointer(w);
        assertNotNull("Could not obtain native HANDLE for JFrame", p);
        HWND hwnd = new HWND(p);
        final DWORDByReference hIconNumber = new DWORDByReference();
        LRESULT result = User32.INSTANCE.SendMessageTimeout(hwnd, WinUser.WM_GETICON, new WPARAM(WinUser.ICON_BIG), new LPARAM(0), WinUser.SMTO_ABORTIFHUNG, 500, hIconNumber);
        assertNotEquals(0, result.intValue());
        final HICON hIcon = new HICON(new Pointer(hIconNumber.getValue().longValue()));
        assertTrue(WindowUtils.getIconSize(hIcon).width >= 32);
        assertTrue(WindowUtils.getIconSize(hIcon).height >= 32);
        assertEquals(WindowUtils.getIconSize(hIcon).width, WindowUtils.getIconSize(hIcon).height);
    } finally {
        w.dispose();
    }
}
Also used : LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) JFrame(javax.swing.JFrame) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) HWND(com.sun.jna.platform.win32.WinDef.HWND) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) HICON(com.sun.jna.platform.win32.WinDef.HICON) Pointer(com.sun.jna.Pointer) File(java.io.File) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) BufferedImage(java.awt.image.BufferedImage) FileInputStream(java.io.FileInputStream)

Example 37 with User32

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

the class KeyHook method main.

public static void main(String[] args) {
    final User32 lib = User32.INSTANCE;
    HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    keyboardHook = new LowLevelKeyboardProc() {

        @Override
        public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
            if (nCode >= 0) {
                switch(wParam.intValue()) {
                    case WinUser.WM_KEYUP:
                    case WinUser.WM_KEYDOWN:
                    case WinUser.WM_SYSKEYUP:
                    case WinUser.WM_SYSKEYDOWN:
                        System.err.println("in callback, key=" + info.vkCode);
                        if (info.vkCode == 81) {
                            quit = true;
                        }
                }
            }
            Pointer ptr = info.getPointer();
            long peer = Pointer.nativeValue(ptr);
            return lib.CallNextHookEx(hhk, nCode, wParam, new LPARAM(peer));
        }
    };
    hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
    System.out.println("Keyboard hook installed, type anywhere, 'q' to quit");
    new Thread() {

        @Override
        public void run() {
            while (!quit) {
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                }
            }
            System.err.println("unhook and exit");
            lib.UnhookWindowsHookEx(hhk);
            System.exit(0);
        }
    }.start();
    // This bit never returns from GetMessage
    int result;
    MSG msg = new MSG();
    while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
        if (result == -1) {
            System.err.println("error in get message");
            break;
        } else {
            System.err.println("got message");
            lib.TranslateMessage(msg);
            lib.DispatchMessage(msg);
        }
    }
    lib.UnhookWindowsHookEx(hhk);
}
Also used : MSG(com.sun.jna.platform.win32.WinUser.MSG) KBDLLHOOKSTRUCT(com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) LowLevelKeyboardProc(com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) User32(com.sun.jna.platform.win32.User32)

Example 38 with User32

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

the class MonitorInfoDemo method main.

/**
	 * @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));
}
Also used : HMONITOR(com.sun.jna.platform.win32.WinUser.HMONITOR) RECT(com.sun.jna.platform.win32.WinDef.RECT) HDC(com.sun.jna.platform.win32.WinDef.HDC) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) MONITORENUMPROC(com.sun.jna.platform.win32.WinUser.MONITORENUMPROC)

Example 39 with User32

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

the class MonitorInfoDemo method enumerate.

static void enumerate(HMONITOR hMonitor) {
    System.out.println("Found HMONITOR: " + hMonitor.getPointer().toString());
    MONITORINFOEX info = new MONITORINFOEX();
    User32.INSTANCE.GetMonitorInfo(hMonitor, info);
    System.out.println("Screen " + info.rcMonitor);
    System.out.println("Work area " + info.rcWork);
    boolean isPrimary = (info.dwFlags & WinUser.MONITORINFOF_PRIMARY) != 0;
    System.out.println("Primary? " + (isPrimary ? "yes" : "no"));
    System.out.println("Device " + new String(info.szDevice));
    DWORDByReference pdwNumberOfPhysicalMonitors = new DWORDByReference();
    Dxva2.INSTANCE.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors);
    int monitorCount = pdwNumberOfPhysicalMonitors.getValue().intValue();
    System.out.println("HMONITOR is linked to " + monitorCount + " physical monitors");
    PHYSICAL_MONITOR[] physMons = new PHYSICAL_MONITOR[monitorCount];
    Dxva2.INSTANCE.GetPhysicalMonitorsFromHMONITOR(hMonitor, monitorCount, physMons);
    for (int i = 0; i < monitorCount; i++) {
        HANDLE hPhysicalMonitor = physMons[0].hPhysicalMonitor;
        System.out.println("Monitor " + i + " - " + new String(physMons[i].szPhysicalMonitorDescription));
        enumeratePhysicalMonitor(hPhysicalMonitor);
    }
    Dxva2.INSTANCE.DestroyPhysicalMonitors(monitorCount, physMons);
}
Also used : MONITORINFOEX(com.sun.jna.platform.win32.WinUser.MONITORINFOEX) PHYSICAL_MONITOR(com.sun.jna.platform.win32.PhysicalMonitorEnumerationAPI.PHYSICAL_MONITOR) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 40 with User32

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

the class AlphaMaskDemo method updateW32.

private void updateW32(boolean a, boolean i) {
    User32 user = User32.INSTANCE;
    GDI32 gdi = GDI32.INSTANCE;
    HWND hWnd = null;
    if (!alphaWindow.isDisplayable()) {
        alphaWindow.pack();
        hWnd = getHwnd(alphaWindow);
        int flags = user.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);
        flags |= WinUser.WS_EX_LAYERED;
        user.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, flags);
        Window parent = alphaWindow.getOwner();
        Point where = parent.getLocationOnScreen();
        where.translate(parent.getWidth(), 0);
        alphaWindow.setLocation(where);
    } else {
        hWnd = getHwnd(alphaWindow);
    }
    if (i) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        HDC screenDC = user.GetDC(null);
        HDC memDC = gdi.CreateCompatibleDC(screenDC);
        HBITMAP hBitmap = null;
        HANDLE oldBitmap = null;
        try {
            BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics g = buf.getGraphics();
            g.drawImage(image, 0, 0, w, h, null);
            BITMAPINFO bmi = new BITMAPINFO();
            bmi.bmiHeader.biWidth = w;
            bmi.bmiHeader.biHeight = h;
            bmi.bmiHeader.biPlanes = 1;
            bmi.bmiHeader.biBitCount = 32;
            bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
            bmi.bmiHeader.biSizeImage = w * h * 4;
            PointerByReference ppbits = new PointerByReference();
            hBitmap = gdi.CreateDIBSection(memDC, bmi, WinGDI.DIB_RGB_COLORS, ppbits, null, 0);
            oldBitmap = gdi.SelectObject(memDC, hBitmap);
            Pointer pbits = ppbits.getValue();
            Raster raster = buf.getData();
            int[] pixel = new int[4];
            int[] bits = new int[w * h];
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    raster.getPixel(x, h - y - 1, pixel);
                    int alpha = (pixel[3] & 0xFF) << 24;
                    int red = (pixel[2] & 0xFF);
                    int green = (pixel[1] & 0xFF) << 8;
                    int blue = (pixel[0] & 0xFF) << 16;
                    bits[x + y * w] = alpha | red | green | blue;
                }
            }
            pbits.write(0, bits, 0, bits.length);
            SIZE size = new SIZE();
            size.cx = w;
            size.cy = h;
            POINT loc = new POINT();
            loc.x = alphaWindow.getX();
            loc.y = alphaWindow.getY();
            POINT srcLoc = new POINT();
            BLENDFUNCTION blend = new BLENDFUNCTION();
            blend.SourceConstantAlpha = (byte) (alpha * 255);
            blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
            user.UpdateLayeredWindow(hWnd, screenDC, loc, size, memDC, srcLoc, 0, blend, WinUser.ULW_ALPHA);
        } finally {
            user.ReleaseDC(null, screenDC);
            if (hBitmap != null) {
                gdi.SelectObject(memDC, oldBitmap);
                gdi.DeleteObject(hBitmap);
            }
            gdi.DeleteDC(memDC);
        }
    } else if (a) {
        BLENDFUNCTION blend = new BLENDFUNCTION();
        blend.SourceConstantAlpha = (byte) (alpha * 255);
        blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
        user.UpdateLayeredWindow(hWnd, null, null, null, null, null, 0, blend, WinUser.ULW_ALPHA);
    }
    if (!alphaWindow.isVisible()) {
        alphaWindow.setVisible(true);
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) GDI32(com.sun.jna.platform.win32.GDI32) BLENDFUNCTION(com.sun.jna.platform.win32.WinUser.BLENDFUNCTION) HDC(com.sun.jna.platform.win32.WinDef.HDC) HWND(com.sun.jna.platform.win32.WinDef.HWND) Raster(java.awt.image.Raster) SIZE(com.sun.jna.platform.win32.WinUser.SIZE) Pointer(com.sun.jna.Pointer) Point(java.awt.Point) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics(java.awt.Graphics) BITMAPINFO(com.sun.jna.platform.win32.WinGDI.BITMAPINFO) PointerByReference(com.sun.jna.ptr.PointerByReference) User32(com.sun.jna.platform.win32.User32) HBITMAP(com.sun.jna.platform.win32.WinDef.HBITMAP) POINT(com.sun.jna.platform.win32.WinDef.POINT) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

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