从Java将第三个窗口强制到前面

从Java将第三个窗口强制到前面,java,focus,keyevent,user32,hwnd,Java,Focus,Keyevent,User32,Hwnd,我正在开发一个Java应用程序,我想从一个我无法访问代码的具体窗口中获取并保存一个屏幕截图。为此,我有以下功能: public void nextStep(){ final MyUser32 user32 = MyUser32.INSTANCE; user32.EnumWindows(new MyUser32.WNDENUMPROC() { public boolean callback(HWND hwnd, Pointer us

我正在开发一个Java应用程序,我想从一个我无法访问代码的具体窗口中获取并保存一个屏幕截图。为此,我有以下功能:

    public void nextStep(){
        final MyUser32 user32 = MyUser32.INSTANCE;
        user32.EnumWindows(new MyUser32.WNDENUMPROC() {

            public boolean callback(HWND hwnd, Pointer userData) {
                byte[] windowText = new byte[512];
                user32.GetWindowTextA(hwnd, windowText, 512);
                String wText = Native.toString(windowText);

            if(wText.contains(MyString)){
                System.out.println("Window hdwn: " + hwnd + " - Text: " + wText);
                User32.INSTANCE.ShowWindow(hwnd, WinUser.SW_MAXIMIZE);
                User32.INSTANCE.SetForegroundWindow(hwnd);
               //I ve tried with setFocus as well and does not work either
                capture();
            }
            return true;
        }
    }, null);
}
捕获方法如下所示:

public void capture(){
    Robot robot;
    try {
        robot = new Robot();
    } catch (AWTException e){
        throw new IllegalArgumentException("No robot");
    }
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyRelease(KeyEvent.VK_UP);
    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_ALT);
    try {
        capt = (BufferedImage) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.imageFlavor);
        ImageIO.write(capt, "png", new File("captura.png"));
    } catch (HeadlessException | UnsupportedFlavorException | IOException e) {
        e.printStackTrace();
    }
}
public interface MyUser32 extends StdCallLibrary {
    MyUser32 INSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class);
    interface WNDENUMPROC extends StdCallCallback {
        boolean callback(HWND hwnd, Pointer arg);
    }
    boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
    int GetWindowTextA(HWND hwnd, byte[] lpString, int nMaxCount);
}
MyUser32类如下所示:

public void capture(){
    Robot robot;
    try {
        robot = new Robot();
    } catch (AWTException e){
        throw new IllegalArgumentException("No robot");
    }
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyRelease(KeyEvent.VK_UP);
    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_ALT);
    try {
        capt = (BufferedImage) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.imageFlavor);
        ImageIO.write(capt, "png", new File("captura.png"));
    } catch (HeadlessException | UnsupportedFlavorException | IOException e) {
        e.printStackTrace();
    }
}
public interface MyUser32 extends StdCallLibrary {
    MyUser32 INSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class);
    interface WNDENUMPROC extends StdCallCallback {
        boolean callback(HWND hwnd, Pointer arg);
    }
    boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
    int GetWindowTextA(HWND hwnd, byte[] lpString, int nMaxCount);
}
如果变量MyString有一个值,比如说,绘制这部分代码的作用是:

-列出所有活动窗口

-对于标题中包含字符串绘制的每个字符串:

*打印其信息,如标题和hwnd

*把它最大化地带到最前面

*再次将其设置为前景窗口,是吗

*应用捕获方法,该方法执行以下操作:

首先按alt+up,使聚焦窗口最大化,然后按alt+Capt.Screen,仅捕获聚焦窗口并将其保存在剪贴板中。最后,它将捕获保存为captura.png

它适用于大多数窗口,如paint或google chrome,但不适用于我感兴趣的捕获窗口。有时它会把它带到前面,有时它不会,没有任何明显的原因,但它永远不会被最大化或捕获,即使alt+up和alt+captScreen在我的物理键盘上工作得非常好,窗口的信息总是打印得很好

有人知道我如何强制第三个窗口获得焦点吗?我想问题是存在的,但我可能错了。此外,我没有任何其他窗口运行,是保持重点强制


非常感谢

为什么不使用AspectJ来允许您更改程序的行为呢。所以,你可以在你的程序中有一个按钮,向另一个程序发送一条消息,然后强制它最大化并在前面,或者,让它在那一点上截屏。谢谢你的快速回答,我对AspectJ一无所知,我只是在谷歌上搜索它。我脑海中的第一个疑问是要发送哪条消息,因为现在我正在使用user32库,该库应该管理这些东西,但它不起作用。此外,我的问题是,一旦窗口出现,我无法用keyevent函数捕捉它本身,程序只是中断保存从剪贴板获得的数据。我想在这个窗口上进行OCR,这就是为什么我需要捕获窗口本身。谢谢