使用JNA获取GetForegroundWindow(); [英] Using JNA to get GetForegroundWindow();

查看:601
本文介绍了使用JNA获取GetForegroundWindow();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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):

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));

    }
}

有什么建议吗?谢谢!

推荐答案

如何简单地添加方法调用以将原生GetForegroundWindow与您的界面相匹配,如下所示:

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

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));
   }
}

这篇关于使用JNA获取GetForegroundWindow();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆