Search in sources :

Example 16 with User32

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

the class OpenGL32Test method testGetStringGLRenderer.

public void testGetStringGLRenderer() {
    // create a dummy window
    HWND hWnd = User32Util.createWindow("Message", null, 0, 0, 0, 0, 0, null, null, null, null);
    HDC hdc = User32.INSTANCE.GetDC(hWnd);
    // set a compatible pixel format
    PIXELFORMATDESCRIPTOR.ByReference pfd = new PIXELFORMATDESCRIPTOR.ByReference();
    pfd.nVersion = 1;
    pfd.dwFlags = WinGDI.PFD_DRAW_TO_WINDOW | WinGDI.PFD_SUPPORT_OPENGL | WinGDI.PFD_DOUBLEBUFFER;
    pfd.iPixelType = WinGDI.PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = WinGDI.PFD_MAIN_PLANE;
    GDI32.INSTANCE.SetPixelFormat(hdc, GDI32.INSTANCE.ChoosePixelFormat(hdc, pfd), pfd);
    // create the OpenGL context
    WinDef.HGLRC hGLRC = OpenGL32.INSTANCE.wglCreateContext(hdc);
    OpenGL32.INSTANCE.wglMakeCurrent(hdc, hGLRC);
    String glString = OpenGL32.INSTANCE.glGetString(GL.GL_RENDERER);
    System.out.println("GL_RENDERER=" + glString);
    OpenGL32.INSTANCE.wglDeleteContext(hGLRC);
    // destroy the window
    User32.INSTANCE.ReleaseDC(hWnd, hdc);
    User32Util.destroyWindow(hWnd);
//assertNotNull("Could not get GL_RENDERER", glString);
}
Also used : HDC(com.sun.jna.platform.win32.WinDef.HDC) HWND(com.sun.jna.platform.win32.WinDef.HWND) PIXELFORMATDESCRIPTOR(com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR)

Example 17 with User32

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

the class GDI32Test method testGetObject.

public void testGetObject() 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();
        // test GetObject method
        BITMAP bmp = new BITMAP();
        int nWrittenBytes = GDI32.INSTANCE.GetObject(iconInfo.hbmColor, bmp.size(), bmp.getPointer());
        bmp.read();
        if (nWrittenBytes <= 0)
            throw new Exception("Detection of bitmap information failed: " + Kernel32Util.getLastErrorMessage());
        // verify that bitmap information was successfully detected
        assertEquals(32, bmp.bmHeight.intValue());
        assertEquals(32, bmp.bmWidth.intValue());
    } 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) BITMAP(com.sun.jna.platform.win32.WinGDI.BITMAP) HICON(com.sun.jna.platform.win32.WinDef.HICON) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) File(java.io.File)

Example 18 with User32

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

the class GDI32UtilTest method testGetScreenshot.

@Test
public void testGetScreenshot() {
    HWND desktopWindow = User32.INSTANCE.GetDesktopWindow();
    assertNotNull("Failed to obtain desktop window handle", desktopWindow);
    BufferedImage image = GDI32Util.getScreenshot(desktopWindow);
    // Since this test involves taking a whole-desktop screenshot
    // we can't be sure what the image will be exactly.
    // We'll validate that the image is "good" 
    // by checking for 20 distinct colors.
    // BufferedImages normally start life as one uniform color 
    // so if that's not the case then some data was indeed copied over as a result of the getScreenshot() function.
    List<Integer> distinctPixels = new ArrayList<Integer>();
    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < image.getHeight(); y++) {
            int pixel = image.getRGB(x, y);
            if (!distinctPixels.contains(pixel)) {
                distinctPixels.add(pixel);
            }
            if (distinctPixels.size() > 20) {
                break;
            }
        }
    }
    assertTrue("Number of distinct pixels was not above 20.", distinctPixels.size() > 20);
}
Also used : HWND(com.sun.jna.platform.win32.WinDef.HWND) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 19 with User32

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

the class User32Util method GetRawInputDeviceList.

public static final List<RAWINPUTDEVICELIST> GetRawInputDeviceList() {
    IntByReference puiNumDevices = new IntByReference(0);
    RAWINPUTDEVICELIST placeholder = new RAWINPUTDEVICELIST();
    int cbSize = placeholder.sizeof();
    // first call is with NULL so we query the expected number of devices
    int returnValue = User32.INSTANCE.GetRawInputDeviceList(null, puiNumDevices, cbSize);
    if (returnValue != 0) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    int deviceCount = puiNumDevices.getValue();
    RAWINPUTDEVICELIST[] records = (RAWINPUTDEVICELIST[]) placeholder.toArray(deviceCount);
    returnValue = User32.INSTANCE.GetRawInputDeviceList(records, puiNumDevices, cbSize);
    if (returnValue == (-1)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    if (returnValue != records.length) {
        throw new IllegalStateException("Mismatched allocated (" + records.length + ") vs. received devices count (" + returnValue + ")");
    }
    return Arrays.asList(records);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) RAWINPUTDEVICELIST(com.sun.jna.platform.win32.WinUser.RAWINPUTDEVICELIST)

Example 20 with User32

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

the class OpenGL32Util method countGpusNV.

/**
     * Count GPUs
     * @return the number of available GPUs
     */
public static int countGpusNV() {
    // create a dummy window
    HWND hWnd = User32Util.createWindow("Message", null, 0, 0, 0, 0, 0, null, null, null, null);
    HDC hdc = User32.INSTANCE.GetDC(hWnd);
    // set a compatible pixel format
    PIXELFORMATDESCRIPTOR.ByReference pfd = new PIXELFORMATDESCRIPTOR.ByReference();
    pfd.nVersion = 1;
    pfd.dwFlags = WinGDI.PFD_DRAW_TO_WINDOW | WinGDI.PFD_SUPPORT_OPENGL | WinGDI.PFD_DOUBLEBUFFER;
    pfd.iPixelType = WinGDI.PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = WinGDI.PFD_MAIN_PLANE;
    GDI32.INSTANCE.SetPixelFormat(hdc, GDI32.INSTANCE.ChoosePixelFormat(hdc, pfd), pfd);
    // create the OpenGL context to get function address
    WinDef.HGLRC hGLRC = OpenGL32.INSTANCE.wglCreateContext(hdc);
    OpenGL32.INSTANCE.wglMakeCurrent(hdc, hGLRC);
    Pointer funcPointer = OpenGL32.INSTANCE.wglGetProcAddress("wglEnumGpusNV");
    Function fncEnumGpusNV = (funcPointer == null) ? null : Function.getFunction(funcPointer);
    OpenGL32.INSTANCE.wglDeleteContext(hGLRC);
    // destroy the window
    User32.INSTANCE.ReleaseDC(hWnd, hdc);
    User32Util.destroyWindow(hWnd);
    // abort if the nVidia extensions are not present
    if (fncEnumGpusNV == null)
        return 0;
    // enumerate nVidia adapters
    HGLRCByReference hGPU = new HGLRCByReference();
    for (int i = 0; i < 16; i++) {
        Boolean ok = (Boolean) fncEnumGpusNV.invoke(Boolean.class, new Object[] { Integer.valueOf(i), hGPU });
        if (!ok.booleanValue())
            return i;
    }
    return 0;
}
Also used : HDC(com.sun.jna.platform.win32.WinDef.HDC) HWND(com.sun.jna.platform.win32.WinDef.HWND) PIXELFORMATDESCRIPTOR(com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR) Pointer(com.sun.jna.Pointer) HGLRCByReference(com.sun.jna.platform.win32.WinDef.HGLRCByReference) Function(com.sun.jna.Function) HGLRCByReference(com.sun.jna.platform.win32.WinDef.HGLRCByReference)

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