Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegisterWindowMessage #196

Merged
merged 4 commits into from
Feb 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Features
* [#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).
* [#194](https://github.com/twall/jna/pull/194): Added Unit Test for `CLSIDFromProgID()` - [@headcrashing](https://github.com/headcrashing).
* [#196](https://github.com/twall/jna/pull/196): Added Win32 `RegisterWindowMessage()` and new wrapper `User32Util` for convenient use of `RegisterWindowMessage`, `CreateWindow` and `CreateWindowEx` - [@headcrashing](https://github.com/headcrashing).

Release 3.5.1
====================
Expand Down
23 changes: 21 additions & 2 deletions contrib/platform/src/com/sun/jna/platform/win32/User32.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,14 +31,20 @@
* @author Todd Fast, [email protected]
* @author [email protected]
* @author Tobias Wolf, [email protected]
* @auhtor Markus KARG (markus[at]headcrashing[dot]eu)
*/
public interface User32 extends StdCallLibrary, WinUser {

/** The instance. */
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. */
Expand Down Expand Up @@ -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.
* <p>
* If the function fails, the return value is zero. To get extended error information, call GetLastError.
* </p>
*/
int RegisterWindowMessage(String string);
}
51 changes: 51 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/User32Util.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
14 changes: 11 additions & 3 deletions contrib/platform/test/com/sun/jna/platform/win32/User32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}