用Java获取活动窗口信息

用Java获取活动窗口信息,第1张

用Java获取活动窗口信息

减轻您的痛苦,并使用JNA。您将需要下载
Win32 API的 jna.jarjna -platform.jar
。该PInvoke的
wiki和MSDN是找到合适的系统调用是有用的。

无论如何,这是用于打印当前活动窗口的标题和过程的代码。

import static enumeration.EnumerateWindows.Kernel32.*;import static enumeration.EnumerateWindows.Psapi.*;import static enumeration.EnumerateWindows.User32DLL.*;import com.sun.jna.Native;import com.sun.jna.Pointer;import com.sun.jna.platform.win32.WinDef.HWND;import com.sun.jna.ptr.PointerByReference;public class EnumerateWindows {    private static final int MAX_TITLE_LENGTH = 1024;    public static void main(String[] args) throws Exception {        char[] buffer = new char[MAX_TITLE_LENGTH * 2];        GetWindowTextW(GetForegroundWindow(), buffer, MAX_TITLE_LENGTH);        System.out.println("Active window title: " + Native.toString(buffer));        PointerByReference pointer = new PointerByReference();        GetWindowThreadProcessId(GetForegroundWindow(), pointer);        Pointer process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pointer.getValue());        GetModulebaseNameW(process, null, buffer, MAX_TITLE_LENGTH);        System.out.println("Active window process: " + Native.toString(buffer));    }    static class Psapi {        static { Native.register("psapi"); }        public static native int GetModulebaseNameW(Pointer hProcess, Pointer hmodule, char[] lpbaseName, int size);    }    static class Kernel32 {        static { Native.register("kernel32"); }        public static int PROCESS_QUERY_INFORMATION = 0x0400;        public static int PROCESS_VM_READ = 0x0010;        public static native int GetLastError();        public static native Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, Pointer pointer);    }    static class User32DLL {        static { Native.register("user32"); }        public static native int GetWindowThreadProcessId(HWND hWnd, PointerByReference pref);        public static native HWND GetForegroundWindow();        public static native int GetWindowTextW(HWND hWnd, char[] lpString, int nMaxCount);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5129697.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存