Search in sources :

Example 6 with GDI32

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

the class GDI32Util method getScreenshot.

/**
	 * Takes a screenshot of the given window
	 * 
	 * @param target
	 *            The window to target
	 * @return the window captured as a screenshot, or null if the BufferedImage doesn't construct properly
	 * @throws IllegalStateException
	 *             if the rectangle from GetWindowRect has a width and/or height
	 *             of 0. <br>
	 *             if the device context acquired from the original HWND doesn't
	 *             release properly
	 */
public static BufferedImage getScreenshot(HWND target) {
    RECT rect = new RECT();
    if (!User32.INSTANCE.GetWindowRect(target, rect)) {
        throw new Win32Exception(Native.getLastError());
    }
    Rectangle jRectangle = rect.toRectangle();
    int windowWidth = jRectangle.width;
    int windowHeight = jRectangle.height;
    if (windowWidth == 0 || windowHeight == 0) {
        throw new IllegalStateException("Window width and/or height were 0 even though GetWindowRect did not appear to fail.");
    }
    HDC hdcTarget = User32.INSTANCE.GetDC(target);
    if (hdcTarget == null) {
        throw new Win32Exception(Native.getLastError());
    }
    Win32Exception we = null;
    // device context used for drawing
    HDC hdcTargetMem = null;
    // handle to the bitmap to be drawn to
    HBITMAP hBitmap = null;
    // original display surface associated with the device context
    HANDLE hOriginal = null;
    // final java image structure we're returning.
    BufferedImage image = null;
    try {
        hdcTargetMem = GDI32.INSTANCE.CreateCompatibleDC(hdcTarget);
        if (hdcTargetMem == null) {
            throw new Win32Exception(Native.getLastError());
        }
        hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcTarget, windowWidth, windowHeight);
        if (hBitmap == null) {
            throw new Win32Exception(Native.getLastError());
        }
        hOriginal = GDI32.INSTANCE.SelectObject(hdcTargetMem, hBitmap);
        if (hOriginal == null) {
            throw new Win32Exception(Native.getLastError());
        }
        // draw to the bitmap
        if (!GDI32.INSTANCE.BitBlt(hdcTargetMem, 0, 0, windowWidth, windowHeight, hdcTarget, 0, 0, GDI32.SRCCOPY)) {
            throw new Win32Exception(Native.getLastError());
        }
        BITMAPINFO bmi = new BITMAPINFO();
        bmi.bmiHeader.biWidth = windowWidth;
        bmi.bmiHeader.biHeight = -windowHeight;
        bmi.bmiHeader.biPlanes = 1;
        bmi.bmiHeader.biBitCount = 32;
        bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
        Memory buffer = new Memory(windowWidth * windowHeight * 4);
        int resultOfDrawing = GDI32.INSTANCE.GetDIBits(hdcTarget, hBitmap, 0, windowHeight, buffer, bmi, WinGDI.DIB_RGB_COLORS);
        if (resultOfDrawing == 0 || resultOfDrawing == WinError.ERROR_INVALID_PARAMETER) {
            throw new Win32Exception(Native.getLastError());
        }
        int bufferSize = windowWidth * windowHeight;
        DataBuffer dataBuffer = new DataBufferInt(buffer.getIntArray(0, bufferSize), bufferSize);
        WritableRaster raster = Raster.createPackedRaster(dataBuffer, windowWidth, windowHeight, windowWidth, SCREENSHOT_BAND_MASKS, null);
        image = new BufferedImage(SCREENSHOT_COLOR_MODEL, raster, false, null);
    } catch (Win32Exception e) {
        we = e;
    } finally {
        if (hOriginal != null) {
            // per MSDN, set the display surface back when done drawing
            HANDLE result = GDI32.INSTANCE.SelectObject(hdcTargetMem, hOriginal);
            // failure modes are null or equal to HGDI_ERROR
            if (result == null || WinGDI.HGDI_ERROR.equals(result)) {
                Win32Exception ex = new Win32Exception(Native.getLastError());
                if (we != null) {
                    ex.addSuppressed(we);
                }
                we = ex;
            }
        }
        if (hBitmap != null) {
            if (!GDI32.INSTANCE.DeleteObject(hBitmap)) {
                Win32Exception ex = new Win32Exception(Native.getLastError());
                if (we != null) {
                    ex.addSuppressed(we);
                }
                we = ex;
            }
        }
        if (hdcTargetMem != null) {
            // get rid of the device context when done
            if (!GDI32.INSTANCE.DeleteDC(hdcTargetMem)) {
                Win32Exception ex = new Win32Exception(Native.getLastError());
                if (we != null) {
                    ex.addSuppressed(we);
                }
                we = ex;
            }
        }
        if (hdcTarget != null) {
            if (0 == User32.INSTANCE.ReleaseDC(target, hdcTarget)) {
                throw new IllegalStateException("Device context did not release properly.");
            }
        }
    }
    if (we != null) {
        throw we;
    }
    return image;
}
Also used : RECT(com.sun.jna.platform.win32.WinDef.RECT) HDC(com.sun.jna.platform.win32.WinDef.HDC) Memory(com.sun.jna.Memory) Rectangle(java.awt.Rectangle) DataBufferInt(java.awt.image.DataBufferInt) BufferedImage(java.awt.image.BufferedImage) Win32Exception(com.sun.jna.platform.win32.Win32Exception) WritableRaster(java.awt.image.WritableRaster) BITMAPINFO(com.sun.jna.platform.win32.WinGDI.BITMAPINFO) HBITMAP(com.sun.jna.platform.win32.WinDef.HBITMAP) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) DataBuffer(java.awt.image.DataBuffer)

Example 7 with GDI32

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

the class OpenGL32Test method testGetStringGLVendor.

public void testGetStringGLVendor() {
    // 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_VENDOR);
    System.out.println("GL_VENDOR=" + glString);
    OpenGL32.INSTANCE.wglDeleteContext(hGLRC);
    // destroy the window
    User32.INSTANCE.ReleaseDC(hWnd, hdc);
    User32Util.destroyWindow(hWnd);
    assertNotNull("Could not get GL_VENDOR", 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 8 with GDI32

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

the class OpenGL32Test method testGetStringGLExtensions.

public void testGetStringGLExtensions() {
    // 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_EXTENSIONS);
    System.out.println("GL_EXTENSIONS=" + glString);
    OpenGL32.INSTANCE.wglDeleteContext(hGLRC);
    // destroy the window
    User32.INSTANCE.ReleaseDC(hWnd, hdc);
    User32Util.destroyWindow(hWnd);
    assertNotNull("Could not get GL_EXTENSIONS", 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 9 with GDI32

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

the class OpenGL32Test method testGetCurrentContext.

public void testGetCurrentContext() {
    // 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);
    WinDef.HGLRC currentContext = OpenGL32.INSTANCE.wglGetCurrentContext();
    OpenGL32.INSTANCE.wglDeleteContext(hGLRC);
    // destroy the window
    User32.INSTANCE.ReleaseDC(hWnd, hdc);
    User32Util.destroyWindow(hWnd);
    assertNotNull("Could not get current context", currentContext);
}
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 10 with GDI32

use of com.sun.jna.platform.win32.GDI32 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)

Aggregations

HDC (com.sun.jna.platform.win32.WinDef.HDC)9 HWND (com.sun.jna.platform.win32.WinDef.HWND)7 PIXELFORMATDESCRIPTOR (com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR)6 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)5 HBITMAP (com.sun.jna.platform.win32.WinDef.HBITMAP)3 BITMAPINFO (com.sun.jna.platform.win32.WinGDI.BITMAPINFO)3 BufferedImage (java.awt.image.BufferedImage)3 Memory (com.sun.jna.Memory)2 Pointer (com.sun.jna.Pointer)2 HICON (com.sun.jna.platform.win32.WinDef.HICON)2 RECT (com.sun.jna.platform.win32.WinDef.RECT)2 ICONINFO (com.sun.jna.platform.win32.WinGDI.ICONINFO)2 File (java.io.File)2 Function (com.sun.jna.Function)1 GDI32 (com.sun.jna.platform.win32.GDI32)1 User32 (com.sun.jna.platform.win32.User32)1 Win32Exception (com.sun.jna.platform.win32.Win32Exception)1 HGLRCByReference (com.sun.jna.platform.win32.WinDef.HGLRCByReference)1 POINT (com.sun.jna.platform.win32.WinDef.POINT)1 BITMAP (com.sun.jna.platform.win32.WinGDI.BITMAP)1