From 97e50d7a35235c79da4e6ff6c1cda248836dbe7e Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Fri, 22 Feb 2013 16:37:58 +0100 Subject: [PATCH 1/2] Mapped RegisterWindowMessage of User32.dll, added User32Util which wraps registerWindowMessage, createWindow and createWindowEx. --- .../com/sun/jna/platform/win32/User32.java | 23 ++++++++- .../sun/jna/platform/win32/User32Util.java | 51 +++++++++++++++++++ .../sun/jna/platform/win32/User32Test.java | 14 +++-- .../jna/platform/win32/User32UtilTest.java | 40 +++++++++++++++ 4 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 contrib/platform/src/com/sun/jna/platform/win32/User32Util.java create mode 100644 contrib/platform/test/com/sun/jna/platform/win32/User32UtilTest.java diff --git a/contrib/platform/src/com/sun/jna/platform/win32/User32.java b/contrib/platform/src/com/sun/jna/platform/win32/User32.java index 3e9715967a..b21b3aa073 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/User32.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/User32.java @@ -1,4 +1,4 @@ -/* Copyright (c) 2007 Timothy Wall, All Rights Reserved +/* Copyright (c) 2007, 2013 Timothy Wall, Markus Karg, All Rights Reserved * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -31,6 +31,7 @@ * @author Todd Fast, todd.fast@sun.com * @author twalljava@dev.java.net * @author Tobias Wolf, wolf.tobias@gmx.net + * @auhtor Markus KARG (markus[at]headcrashing[dot]eu) */ public interface User32 extends StdCallLibrary, WinUser { @@ -38,7 +39,12 @@ public interface User32 extends StdCallLibrary, WinUser { User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS); - /** The cs globalclass. */ + /** + * Handle for message-only window. + */ + public static final HWND HWND_MESSAGE = new HWND(Pointer.createConstant(-3)); + + /** The cs globalclass. */ int CS_GLOBALCLASS = 0x4000; /** The ws ex topmost. */ @@ -1684,4 +1690,17 @@ HDEVNOTIFY RegisterDeviceNotification(HANDLE hRecipient, * error information, call GetLastError. */ boolean UnregisterDeviceNotification(HDEVNOTIFY Handle); + + /** + * Defines a new window message that is guaranteed to be unique throughout the system. The message value can be used when sending or posting messages. + * + * @param string + * The message to be registered. + * + * @return If the message is successfully registered, the return value is a message identifier in the range 0xC000 through 0xFFFF. + *

+ * If the function fails, the return value is zero. To get extended error information, call GetLastError. + *

+ */ + int RegisterWindowMessage(String string); } diff --git a/contrib/platform/src/com/sun/jna/platform/win32/User32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/User32Util.java new file mode 100644 index 0000000000..616253ce31 --- /dev/null +++ b/contrib/platform/src/com/sun/jna/platform/win32/User32Util.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2013 Ralf Hamberger, Markus Karg, All Rights Reserved + * + * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + */ + +package com.sun.jna.platform.win32; +import com.sun.jna.WString; +import com.sun.jna.platform.win32.WinDef.HINSTANCE; +import com.sun.jna.platform.win32.WinDef.HMENU; +import com.sun.jna.platform.win32.WinDef.HWND; +import com.sun.jna.platform.win32.WinDef.LPVOID; + + +/** + * Provides convenient usage of functions defined by {@code User32.dll}. + * + * @author Ralf HAMBERGER + * @author Markus KARG (markus[at]headcrashing[dot]eu) + */ +public final class User32Util { + public static final int registerWindowMessage(final String lpString) { + final int messageId = User32.INSTANCE.RegisterWindowMessage(lpString); + if (messageId == 0) + throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); + return messageId; + } + + public static final HWND createWindow(final String className, final String windowName, final int style, final int x, final int y, final int width, + final int height, final HWND parent, final HMENU menu, final HINSTANCE instance, final LPVOID param) { + return User32Util.createWindowEx(0, className, windowName, style, x, y, width, height, parent, menu, instance, param); + } + + public static final HWND createWindowEx(final int exStyle, final String className, final String windowName, final int style, final int x, final int y, + final int width, final int height, final HWND parent, final HMENU menu, final HINSTANCE instance, final LPVOID param) { + final HWND hWnd = User32.INSTANCE + .CreateWindowEx(exStyle, new WString(className), windowName, style, x, y, width, height, parent, menu, instance, param); + if (hWnd == null) + throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); + return hWnd; + } + + public static final void destroyWindow(final HWND hWnd) { + if (!User32.INSTANCE.DestroyWindow(hWnd)) + throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); + } +} \ No newline at end of file diff --git a/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java b/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java index 22471ecbb9..fb3b45982e 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java @@ -12,12 +12,15 @@ */ package com.sun.jna.platform.win32; -import junit.framework.TestCase; +import static com.sun.jna.platform.win32.User32.INSTANCE; -import java.awt.*; +import java.awt.AWTException; +import java.awt.Robot; import java.awt.event.KeyEvent; -import static com.sun.jna.platform.win32.User32.*; +import junit.framework.TestCase; + +import com.sun.jna.platform.win32.WinUser.LASTINPUTINFO; /** * @author dblock[at]dblock[dot]org @@ -102,4 +105,9 @@ public void testGetLastInputInfo() throws Exception { assertTrue(Kernel32.INSTANCE.GetTickCount() >= plii.dwTime); assertTrue(plii.dwTime > 0); } + + public final void testRegisterWindowMessage() { + final int msg = User32.INSTANCE.RegisterWindowMessage("RM_UNITTEST"); + assertTrue(msg >= 0xC000 && msg <= 0xFFFF); + } } diff --git a/contrib/platform/test/com/sun/jna/platform/win32/User32UtilTest.java b/contrib/platform/test/com/sun/jna/platform/win32/User32UtilTest.java new file mode 100644 index 0000000000..da0d0d7892 --- /dev/null +++ b/contrib/platform/test/com/sun/jna/platform/win32/User32UtilTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2013 Markus Karg, All Rights Reserved + * + * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + */ +package com.sun.jna.platform.win32; + +import junit.framework.TestCase; + +import com.sun.jna.Pointer; + +/** + * @author markus[at]headcrashing[dot]eu + */ +public final class User32UtilTest extends TestCase { + public static final void main(final String[] args) { + junit.textui.TestRunner.run(User32UtilTest.class); + } + + public final void testRegisterWindowMessage() { + final int msg = User32Util.registerWindowMessage("RM_UNITTEST"); + assertTrue(msg >= 0xC000 && msg <= 0xFFFF); + } + + public final void testCreateWindow() { + assertTrue(Pointer.nativeValue(User32Util.createWindow("Message", null, 0, 0, 0, 0, 0, null, null, null, null).getPointer()) > 0); + } + + public final void testCreateWindowEx() { + assertTrue(Pointer.nativeValue(User32Util.createWindowEx(0, "Message", null, 0, 0, 0, 0, 0, null, null, null, null).getPointer()) > 0); + } + + public final void testDestroyWindow() { + User32Util.destroyWindow(User32Util.createWindow("Message", null, 0, 0, 0, 0, 0, null, null, null, null)); + } +} \ No newline at end of file From b2e16a0ca5f81b0c79358c912d09744033850532 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sat, 23 Feb 2013 14:50:20 +0100 Subject: [PATCH 2/2] Added info to CHANGES.md about this pull request. --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index b2c10a150f..83c35fcaad 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -18,6 +18,7 @@ Features * [#183](https://github.com/twall/jna/pull/183): Added `StructureFieldOrderInspector` unit test utility to scan for `Structure` field issues; see: `com.sun.jna.platform.StructureFieldOrderTest.testMethodGetFieldOrder` - [@bhamail](https://github.com/bhamail). * [#187](https://github.com/twall/jna/pull/187): Allow `StructureFieldOrderTest` unit test in platform project to run on Linux - [@bhamail](https://github.com/bhamail). * [#192](https://github.com/twall/jna/pull/192): Added Win32 `SHGetSpecialFolderPath()` and initialization file (.ini) API functions from `kernel32.dll` - [@headcrashing](https://github.com/headcrashing). +* [# ](https://github.com/twall/jna/pull/ ): Added Win32 `RegisterWindowMessage()` and new wrapper `User32Util` for convenient use of `RegisterWindowMessage`, `CreateWindow` and `CreateWindowEx` - [@headcrashing](https://github.com/headcrashing). Release 3.5.1 ====================