简体   繁体   English

JNA 客户端到屏幕?

[英]JNA ClientToScreen?

Having issues figuring out how to use the ClientToScreen winapi function with JNA.在弄清楚如何将 ClientToScreen winapi 函数与 JNA 一起使用时遇到问题。

I'm still getting 0, 0 output for the coordinates of the window handle.我仍然得到 0, 0 窗口句柄坐标的输出。 Am referencing this but im sure im not doing it right https://msdn.microsoft.com/en-us/library/windows/desktop/dd183434(v=vs.85).aspx我正在引用这个,但我确定我没有做对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 is right. @technomage 是对的。 You need to use WinDef.POINT instead of int in your ClientToScreen() rect parameter.您需要在ClientToScreen() rect 参数中使用WinDef.POINT而不是int

If anyone looking for a working solution for the question, it's below.如果有人正在为该问题寻找可行的解决方案,则在下面。 In every 3rd sec, it's logging the window inner client coordinates desktop position.每 3 秒,它都会记录窗口内部客户端坐标桌面位置。

Explanation.解释。

In the User32ForClientRect interface, we load user32.dll with JNA and define a few methods.User32ForClientRect接口中,我们使用 JNA 加载user32.dll并定义了一些方法。 The reason behind we not using the already implemented User32 methods from the JNA interface is simple.我们不使用 JNA 接口中已经实现的 User32 方法的原因很简单。 The methods that we need in our case are not implemented in JNA.在我们的案例中我们需要的方法没有在 JNA 中实现。

Methods:方法:

  • FindWindow - it's included in JNA FindWindow - 它包含在 JNA 中

    Retreives the window handle HWND检索窗口句柄HWND


  • GetClientRect

    Retreives the window widht, height.检索窗口宽度、高度。 to rect.right, rect.bottomrect.right, rect.bottom


  • ClientToScreen

    Retreives the window inner client desktop position.检索窗口内部客户端桌面位置。 Left/Top corner coordinates左/上角坐标


Example:例子:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM