Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JNA客户端到屏幕?_Java_Jna - Fatal编程技术网

Java JNA客户端到屏幕?

Java JNA客户端到屏幕?,java,jna,Java,Jna,在了解如何将ClientToScreen winapi函数与JNA一起使用时遇到问题 我仍然得到0,0窗口句柄坐标的输出。 我引用了这个,但我肯定我做得不对 您需要将LPPOINT传递给,而不是int,或者更具体地说,传递一个指向点结构的指针(WinDef.POINT)。您需要在ClientToScreen()rect参数中使用WinDef.POINT而不是int 如果有人想为这个问题找到一个有效的解决方案,请看下面。 每隔3秒,它就会记录窗口内部客户端坐标的桌面位置 解释。 在User32Fo

在了解如何将ClientToScreen winapi函数与JNA一起使用时遇到问题

我仍然得到0,0窗口句柄坐标的输出。 我引用了这个,但我肯定我做得不对


您需要将
LPPOINT
传递给,而不是
int
,或者更具体地说,传递一个指向
结构的指针(
WinDef.POINT
)。您需要在
ClientToScreen()
rect参数中使用
WinDef.POINT
而不是
int

如果有人想为这个问题找到一个有效的解决方案,请看下面。 每隔3秒,它就会记录窗口内部客户端坐标的桌面位置

解释。 在
User32ForClientRect
接口中,我们使用JNA加载
user32.dll
,并定义了一些方法。 我们不使用JNA接口中已经实现的User32方法的原因很简单。我们在本例中需要的方法没有在JNA中实现

方法:
  • -它包含在JNA中

    检索窗口句柄HWND


  • 把窗宽、窗高收回来。到右下角,右下角


  • 检索窗口内部客户端桌面位置。左/上角坐标


例子:
我觉得这太简短了。应该有更深入的解释。@Momoro感谢您的反馈,我已经更新了详细的解释和链接。更好:)
    public interface User32Ex extends W32APIOptions {
    User32Ex instance = (User32Ex) Native.loadLibrary("user32", User32Ex.class, DEFAULT_OPTIONS);
    boolean GetCursorPos(long[] lpPoint);
    WinDef.HWND WindowFromPoint(long point);
    boolean GetClientRect(WinDef.HWND hWnd, WinDef.RECT rect);
    boolean ClientToScreen(WinDef.HWND hWnd, int pt);
}



public void debug() throws InterruptedException {
    while (true) {
        long[] getPos = new long[1];
        User32Ex.instance.GetCursorPos(getPos);
        WinDef.HWND hwnd = User32Ex.instance.WindowFromPoint(getPos[0]);

        WinDef.RECT rect = new WinDef.RECT();
        User32Ex.instance.GetClientRect(hwnd, rect);
        User32Ex.instance.ClientToScreen(hwnd, rect.left);
        User32Ex.instance.ClientToScreen(hwnd, rect.right);

        System.out.println(rect.toRectangle().toString());
        Thread.sleep(1500);
    }
}
package application.playground;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

import java.awt.*;

public class ClientRectExample {
    interface User32ForClientRect extends StdCallLibrary {
        User32ForClientRect INSTANCE = Native.loadLibrary("user32", User32ForClientRect.class,
                W32APIOptions.DEFAULT_OPTIONS);
        WinDef.HWND FindWindow(String lpClassName, String lpWindowName);
        boolean GetClientRect(WinDef.HWND hWnd, WinDef.RECT rect);
        boolean ClientToScreen(WinDef.HWND hWnd, WinDef.POINT lpPoint);
    }

    public static void main(String[] args) {
        while (true) {
            try {
                Rectangle rectangle = ClientRectExample.getClientRect("WindowName");
                System.out.println(rectangle);
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static Rectangle getClientRect(String startOfWindowName) {
        WinDef.HWND hWnd = User32ForClientRect.INSTANCE.FindWindow(null, startOfWindowName);
        WinDef.POINT getPos = new WinDef.POINT();
        WinDef.RECT rect = new WinDef.RECT();
        User32ForClientRect.INSTANCE.GetClientRect(hWnd, rect);
        User32ForClientRect.INSTANCE.ClientToScreen(hWnd, getPos);

        return new Rectangle(getPos.x, getPos.y, rect.right, rect.bottom);
    }
}