用JNA单击鼠标

时间:2012-02-15 12:44:30

标签: java jna

我正在尝试使用JNA在窗​​口模拟鼠标点击。

public class App {

public static final int WM_LBUTTONUP = 514;
public static final int WM_LBUTTONDOWN = 513;
public static final int WM_LBUTTONDBLCLK = 0x203;
static int WM_CLOSE = 0x10;
final static String winTitle = "Untitled - Notepad";

public static void main(String[] args) throws InterruptedException {
    User32Extra user32 = (User32Extra) Native.loadLibrary("user32", User32Extra.class, W32APIOptions.DEFAULT_OPTIONS);

    WinDef.HWND hwnd = user32.FindWindow(null, winTitle);
    user32.SetForegroundWindow(hwnd);
    Thread.sleep(1000);

    long y = 77 + (22 << 16);//x + (y << 16)
    WinDef.LPARAM l = new WinDef.LPARAM(y);
    WinDef.WPARAM w = new WinDef.WPARAM(0);
    user32.PostMessage(hwnd, WM_LBUTTONDOWN, w, l);
    Thread.sleep(1000);
    user32.PostMessage(hwnd, WM_LBUTTONUP, w, l);
}
}

它找到窗口并将其带到前面。但鼠标单击不起作用。也发送WM_CLOSE工作。 鼠标点击有什么问题? 测试计算器和记事本。 坐标是相对于窗口的。

1 个答案:

答案 0 :(得分:1)

只是猜测:点击事件不应传递到主窗口,而应传递到目标按钮对象本身。在给定的坐标上,当真正的点击发生时,按钮位于主窗口上方“隐藏”它。