首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JNA ClientToScreen?

JNA ClientToScreen?
EN

Stack Overflow用户
提问于 2015-10-05 05:49:46
回答 2查看 642关注 0票数 3

弄清楚如何在ClientToScreen中使用JNA winapi函数的问题。

我仍然得到窗口句柄坐标的0,0输出。我引用了这一点,但我确信我没有做对https://msdn.microsoft.com/en-us/library/windows/desktop/dd183434(v=vs.85).aspx

代码语言:javascript
复制
    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);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2020-02-20 07:56:59

@technomage是对的。您需要在ClientToScreen() rect参数中使用WinDef.POINT而不是int

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

解释一下。

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

方法:

检索窗口句柄HWND

检索窗口的宽度、高度。to rect.right,rect.bottom

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

示例:

代码语言:javascript
复制
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);
    }
}
票数 4
EN

Stack Overflow用户

发布于 2015-10-06 19:05:15

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32938784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档