windows 使用JNA获取GetForegroundWindow();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5767104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:42:34  来源:igfitidea点击:

Using JNA to get GetForegroundWindow();

javawindowswinapijna

提问by Daniel Loureiro

I asked a similar question on a previous thread (https://stackoverflow.com/questions/5206633/java-find-out-what-application-window-is-in-focus)but I was guided to use JNI, and I'm not having much success with it... I've read some tutorials and while some work fine, others don't I still can't get the information I need, which is the title of the window on the foreground.

我在上一个线程(https://stackoverflow.com/questions/5206633/java-find-out-what-application-window-is-in-focus)上问了一个类似的问题,但我被引导使用 JNI,我' 并没有取得太大的成功......我已经阅读了一些教程,虽然有些工作正常,但其他人没有我仍然无法获得我需要的信息,这是前台窗口的标题。

Now I'm looking into JNA but I can't figure out how to access GetForegroundWindow() ... I think I can print the text once I get the handle to the window using this code (found on another thread):

现在我正在研究 JNA,但我不知道如何访问 GetForegroundWindow() ......我想我可以在使用此代码(在另一个线程上找到)获得窗口句柄后打印文本:

import com.sun.jna.*;
import com.sun.jna.win32.*;

public class jnatest {
    public interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

        int GetWindowTextA(PointerType hWnd, byte[] lpString, int nMaxCount);
    }

    public static void main(){
        byte[] windowText = new byte[512];

        PointerType hwnd = //GetForegroundWindow() (?)...
        User32.INSTANCE.GetWindowTextA(hwnd, windowText, 512);
        System.out.println(Native.toString(windowText));

    }
}

Any suggestions? Thanks!

有什么建议?谢谢!

回答by Hovercraft Full Of Eels

How about simply adding a method call to match the native GetForegroundWindow to your interface, something like so:

如何简单地添加一个方法调用来匹配本机 GetForegroundWindow 到您的界面,如下所示:

import com.sun.jna.*;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.*;

public class JnaTest {
   public interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
      HWND GetForegroundWindow();  // add this
      int GetWindowTextA(PointerType hWnd, byte[] lpString, int nMaxCount);
   }

   public static void main(String[] args) throws InterruptedException {
      byte[] windowText = new byte[512];

      PointerType hwnd = User32.INSTANCE.GetForegroundWindow(); // then you can call it!
      User32.INSTANCE.GetWindowTextA(hwnd, windowText, 512);
      System.out.println(Native.toString(windowText));
   }
}

回答by Shrikant Sharat

If getting the window title is all you want to do, you don't have to explicitly load the user32library. JNA comes with it, in the platform.jarfile (at least in v3.4 it does).

如果您只想获取窗口标题,则不必显式加载user32库。JNA 随附在platform.jar文件中(至少在 v3.4 中是这样)。

I got this working here:

我在这里工作:

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.User32;

public class JnaApp {

    public static void main(String[] args) {
        System.out.println("title is " + getActiveWindowTitle());
    }

    private static String getActiveWindowTitle() {
        HWND fgWindow = User32.INSTANCE.GetForegroundWindow();
        int titleLength = User32.INSTANCE.GetWindowTextLength(fgWindow) + 1;
        char[] title = new char[titleLength];
        User32.INSTANCE.GetWindowText(fgWindow, title, titleLength);
        return Native.toString(title);
    }

}

See more on User32's Javadoc. Its got almost all the functions in the user32 library.

User32 的 Javadoc上查看更多信息。它获得了 user32 库中几乎所有的功能。