-2

操作系统:Windows 7

你能告诉我如何自动识别在后台运行的java程序,现在哪个窗口是活动的Windows?

由谷歌翻译

4

1 回答 1

1

RanRag,感谢您的链接

要确定哪个程序属于活动窗口,您必须:

1) 从JNA - 下载页面下载 jna.jar 和 platform.jar

2)将库连接到项目

3)添加代码:

private static class Psapi {
        static { Native.register("psapi"); }
        public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
    }

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

    private static class User32DLL {
        static { Native.register("user32"); }
        public static native int GetWindowThreadProcessId(WinDef.HWND hWnd, PointerByReference pref);
        public static native WinDef.HWND GetForegroundWindow();
        public static native int GetWindowTextW(WinDef.HWND hWnd, char[] lpString, int nMaxCount);
    }

    private static final int MAX_TITLE_LENGTH = 1024;

    public void nameApplication() {

        Psapi ps = new Psapi();
        Kernel32 kr = new Kernel32();
        User32DLL us = new User32DLL();

        char[] buffer = new char[MAX_TITLE_LENGTH * 2];
        String nameApp;

        us.GetWindowTextW(us.GetForegroundWindow(), buffer, MAX_TITLE_LENGTH);
        System.out.println("Active window title: " + Native.toString(buffer));

        PointerByReference pointer = new PointerByReference();
        us.GetWindowThreadProcessId(us.GetForegroundWindow(), pointer);
        Pointer process = kr.OpenProcess(kr.PROCESS_QUERY_INFORMATION | kr.PROCESS_VM_READ, false, pointer.getValue());
        ps.GetModuleBaseNameW(process, null, buffer, MAX_TITLE_LENGTH);
        System.out.println("Active window process: " + Native.toString(buffer););

    }
于 2012-05-22T18:47:59.873 回答