加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用JNA获取GetForegroundWindow();

发布时间:2020-12-14 05:37:51 所属栏目:Java 来源:网络整理
导读:我曾经问过一个类似的问题( https://stackoverflow.com/questions/5206633/java-find-out-what-application-window-is-in-focus),但是我被引导使用JNI,而且我没有太多的成功…我已经阅读了一些教程,而一些工作正常,其他人还没有无法获取我需要的信息,这是前
我曾经问过一个类似的问题( https://stackoverflow.com/questions/5206633/java-find-out-what-application-window-is-in-focus),但是我被引导使用JNI,而且我没有太多的成功…我已经阅读了一些教程,而一些工作正常,其他人还没有无法获取我需要的信息,这是前台窗口的标题.

现在我正在研究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));

    }
}

有什么建议么?谢谢!

解决方法

如何简单地添加一个方法调用来匹配本地的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,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,512);
      System.out.println(Native.toString(windowText));
   }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读