Search in sources :

Example 21 with User32

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

the class User32WindowMessagesTest method createWindowAndLoop.

public void createWindowAndLoop(String windowClass) {
    // define new window class
    HMODULE hInst = Kernel32.INSTANCE.GetModuleHandle("");
    WNDCLASSEX wClass = new WNDCLASSEX();
    wClass.hInstance = hInst;
    wClass.lpfnWndProc = new WindowProc() {

        @Override
        public LRESULT callback(HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam) {
            // log(hwnd + " - received a message : " + uMsg);
            switch(uMsg) {
                case WinUser.WM_CREATE:
                    {
                        log(hwnd + " - onCreate: WM_CREATE");
                        return new LRESULT(0);
                    }
                case WinUser.WM_CLOSE:
                    log(hwnd + " WM_CLOSE");
                    User32.INSTANCE.DestroyWindow(hwnd);
                    return new LRESULT(0);
                case WinUser.WM_DESTROY:
                    {
                        log(hwnd + " - on Destroy.");
                        User32.INSTANCE.PostQuitMessage(0);
                        return new LRESULT(0);
                    }
                case WinUser.WM_USER:
                    {
                        log(hwnd + " - received a WM_USER message with code : '" + wParam + "' and value : '" + lParam + "'");
                        if (wParam.intValue() == MSG_SIMPLE_CODE) {
                            assertEqualsForCallbackExecution(MSG_SIMPLE_VAL, lParam.intValue());
                        }
                        if (wParam.intValue() == MSG_HOOKED_CODE) {
                            assertEqualsForCallbackExecution(MSG_HOOKED_VAL, lParam.intValue());
                        }
                        return new LRESULT(0);
                    }
                case WinUser.WM_COPYDATA:
                    {
                        COPYDATASTRUCT copyDataStruct = new COPYDATASTRUCT(new Pointer(lParam.longValue()));
                        ULONG_PTR uMsg1 = copyDataStruct.dwData;
                        Pointer lParam1 = copyDataStruct.lpData;
                        int wParam1 = copyDataStruct.cbData;
                        log(hwnd + " - received a WM_COPYDATA message with code : '" + uMsg1 + "' of size : '" + wParam1 + "'");
                        switch(uMsg1.intValue()) {
                            case DATA_STRUCT_CODE:
                                {
                                    MsgStruct msg = new MsgStruct(lParam1);
                                    // log(hwnd + " - received structured content : " + msg.toString(true));
                                    log(hwnd + " - message is of type MsgStruct with number = " + msg.number + " and message = '" + msg.message + "'");
                                    assertEqualsForCallbackExecution(MSG_STRUCT_NUMBER, msg.number);
                                    assertEqualsForCallbackExecution(MSG_STRUCT_VAL, msg.message);
                                }
                        }
                        return new LRESULT(0);
                    }
                default:
                    return User32.INSTANCE.DefWindowProc(hwnd, uMsg, wParam, lParam);
            }
        }
    };
    wClass.lpszClassName = windowClass;
    // register window class
    User32.INSTANCE.RegisterClassEx(wClass);
    getLastError();
    // create new window
    HWND hWnd = User32.INSTANCE.CreateWindowEx(User32.WS_EX_TOPMOST, windowClass, "My hidden helper window, used only to catch the windows events", 0, 0, 0, 0, 0, null, null, hInst, null);
    getLastError();
    log("window sucessfully created! window hwnd: " + hWnd.getPointer().toString());
    MSG msg = new MSG();
    while (User32.INSTANCE.GetMessage(msg, hWnd, 0, 0) > 0) {
        User32.INSTANCE.TranslateMessage(msg);
        User32.INSTANCE.DispatchMessage(msg);
    }
    User32.INSTANCE.UnregisterClass(windowClass, hInst);
    User32.INSTANCE.DestroyWindow(hWnd);
    log("program exit!");
}
Also used : MSG(com.sun.jna.platform.win32.WinUser.MSG) HWND(com.sun.jna.platform.win32.WinDef.HWND) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) ULONG_PTR(com.sun.jna.platform.win32.BaseTSD.ULONG_PTR) WindowProc(com.sun.jna.platform.win32.WinUser.WindowProc) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) WNDCLASSEX(com.sun.jna.platform.win32.WinUser.WNDCLASSEX) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) COPYDATASTRUCT(com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)

Example 22 with User32

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

the class User32WindowMessagesTest method hookwinProc.

private HHOOK hookwinProc(HWND hwndToHook) {
    HOOKPROC hookProc = new HOOKPROC() {

        /**
			 * Callback method. cf : https://msdn.microsoft.com/en-us/library/windows/desktop/ms644975(v=vs.85).aspx
			 *
			 * nCode [in] : Specifies whether the hook procedure must process the message. If nCode is HC_ACTION, the
			 * hook procedure must process the message. If nCode is less than zero, the hook procedure must pass the
			 * message to the CallNextHookEx function without further processing and must return the value returned by
			 * CallNextHookEx. wParam [in] : Specifies whether the message was sent by the current thread. If the
			 * message was sent by the current thread, it is nonzero; otherwise, it is zero. lParam [in] : A pointer to
			 * a CWPSTRUCT structure that contains details about the message.
			 *
			 */
        // used by introspection from jna.
        @SuppressWarnings("unused")
        public LRESULT callback(int nCode, WPARAM wParam, LPARAM lParam) {
            if (nCode < 0) {
                return User32.INSTANCE.CallNextHookEx(null, nCode, wParam, lParam);
            }
            try {
                WinUser.CWPSTRUCT cwp = new WinUser.CWPSTRUCT(new Pointer(lParam.longValue()));
                switch(cwp.message) {
                    case WinUser.WM_USER:
                        {
                            HWND hWndSource = new HWND(new Pointer(cwp.wParam.longValue()));
                            log(cwp.hwnd + " - Received a message from " + hWndSource + " hooked proc : code= " + cwp.wParam + ", value = " + cwp.lParam);
                            assertEqualsForCallbackExecution(MSG_HOOKED_CODE, cwp.wParam.intValue());
                            assertEqualsForCallbackExecution(MSG_HOOKED_VAL, cwp.lParam.intValue());
                            return new LRESULT(0);
                        }
                }
                // Send message to next hook.
                return User32.INSTANCE.CallNextHookEx(null, nCode, wParam, lParam);
            } catch (Throwable t) {
                t.printStackTrace();
                return new LRESULT(0);
            }
        }
    };
    HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null);
    int threadtoHook = User32.INSTANCE.GetWindowThreadProcessId(hwndToHook, null);
    // Hook of the wndProc
    return User32.INSTANCE.SetWindowsHookEx(WinUser.WH_CALLWNDPROC, hookProc, hInst, threadtoHook);
}
Also used : LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) HINSTANCE(com.sun.jna.platform.win32.WinDef.HINSTANCE) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) HWND(com.sun.jna.platform.win32.WinDef.HWND) Pointer(com.sun.jna.Pointer) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) HOOKPROC(com.sun.jna.platform.win32.WinUser.HOOKPROC)

Example 23 with User32

use of com.sun.jna.platform.win32.User32 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 24 with User32

use of com.sun.jna.platform.win32.User32 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 25 with User32

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

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