弄清楚如何在ClientToScreen中使用JNA winapi函数的问题。
我仍然得到窗口句柄坐标的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);
}
}发布于 2020-02-20 07:56:59
@technomage是对的。您需要在ClientToScreen() rect参数中使用WinDef.POINT而不是int。
如果有人在寻找这个问题的有效解决方案,可以在下面找到。每隔3秒,它就会记录窗口内部的客户端坐标桌面位置。
解释一下。
在User32ForClientRect接口中,我们使用JNA加载user32.dll并定义一些方法。我们不使用已经实现的来自JNA接口的User32方法的原因很简单。在我们的例子中,我们需要的方法并没有在JNA中实现。
方法:
FindWindow -它包含在JNA中检索窗口句柄HWND
检索窗口的宽度、高度。to 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);
}
}发布于 2015-10-06 19:05:15
https://stackoverflow.com/questions/32938784
复制相似问题