[英]JNA ClientToScreen?
在弄清楚如何将 ClientToScreen winapi 函数与 JNA 一起使用时遇到问题。
我仍然得到 0, 0 窗口句柄坐标的输出。 我正在引用这个,但我确定我没有做对https://msdn.microsoft.com/en-us/library/windows/desktop/dd183434(v=vs.85).aspx
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);
}
}
@technomage 是对的。 您需要在ClientToScreen()
rect 参数中使用WinDef.POINT
而不是int
。
如果有人正在为该问题寻找可行的解决方案,则在下面。 每 3 秒,它都会记录窗口内部客户端坐标桌面位置。
在User32ForClientRect
接口中,我们使用 JNA 加载user32.dll
并定义了一些方法。 我们不使用 JNA 接口中已经实现的 User32 方法的原因很简单。 在我们的案例中我们需要的方法没有在 JNA 中实现。
FindWindow
- 它包含在 JNA 中
检索窗口句柄HWND
检索窗口宽度、高度。 到rect.right, rect.bottom
检索窗口内部客户端桌面位置。 左/上角坐标
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);
}
}
您需要将LPPOINT
传递给ClientToScreen()
,而不是int
,或者更具体地说是一个指向POINT
结构的指针(JNA 中的WinDef.POINT
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.