【发布时间】:2014-02-17 16:14:07
【问题描述】:
我正在尝试通过使用 JNA 来构建一个 java 应用程序来访问窗口消息(例如 WM_POINTERDOWN)。使用此选项,我将把我的应用程序变成一个触摸感应应用程序。到目前为止,我当前的代码收到了这个窗口消息,但可能会覆盖一些其他重要的 Java 本机代码,因此 JFrame 不会以我期望的方式做出反应(例如,在将 JFrame 调整为更大的时候,它会填充新添加的区域黑色)。
这是我的监听器,当有新的窗口消息到达时会调用它:
public MyListener listener = new MyListener() {
public LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam,
LPARAM lParam) {
//handle the window message here
return User32.INSTANCE.DefWindowProc(hWnd, uMsg, uParam, lParam);
}
};
MyListener 接口:
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
public interface MyListener extends StdCallCallback {
public LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
}
在这个 sn-p 中,我用我的侦听器覆盖了 JFrame 的本机函数,该函数通常会从操作系统调用:
HWND hWnd = new HWND();
hWnd.setPointer(Native.getWindowPointer(this));
MyUser32.MYINSTANCE
.SetWindowLong(hWnd, MyUser32.GWLP_WNDPROC, listener);
MyUser32 类:
import com.sun.jna.Callback;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.win32.W32APIOptions;
public interface MyUser32 extends User32 {
public static final MyUser32 MYINSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class, W32APIOptions.UNICODE_OPTIONS);
/**
* Sets a new address for the window procedure (value to be set).
*/
public static final int GWLP_WNDPROC = -4;
/**
* Changes an attribute of the specified window
* @param hWnd A handle to the window
* @param nIndex The zero-based offset to the value to be set.
* @param callback The callback function for the value to be set.
*/
public int SetWindowLong(WinDef.HWND hWnd, int nIndex, Callback callback);
}
也许有人对此有一个好主意。谢谢。
【问题讨论】:
-
确保您的回调实现遵循 chaining to the next callback 的 Windows 约定。