Java JNA 聚焦特定窗口

标签 java window focus jna

我试图让我的应用程序能够聚焦另一个窗口(在这种情况下是记事本)

我的类(class)是这样的

 public static class Win32WindowUtils {

  public interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
        HWND GetParent(HWND hWnd);
        HWND FindWindow(String lpClassName, String lpWindowName);
        HWND SetFocus(HWND hWnd);
        HWND FindWindowEx(HWND hwndParent, HWND hwndChildAfter, String lpszClass, String lpszWindow);
        int GetWindowText(HWND hWnd, char[] lpString, int nMaxCount);
    }

    private static final int WIN_TITLE_MAX_SIZE = 512;

    public static HWND GetWindowHandle(String strSearch, String strClass) {
        char[] lpString = new char[WIN_TITLE_MAX_SIZE];
        String strTitle;
        int iFind = -1;
        HWND hWnd = User32.INSTANCE.FindWindow(strClass, null);
        while(hWnd != null) {
            User32.INSTANCE.GetWindowText(hWnd, lpString, WIN_TITLE_MAX_SIZE);
            strTitle = new String(lpString);
            strTitle = strTitle.toUpperCase();
            iFind = strTitle.indexOf(strSearch);
            if(iFind != -1) {
                return hWnd;
            }
            hWnd = (User32.INSTANCE).FindWindowEx(null, hWnd, strClass, null);
        }
        return hWnd;
    }
}

我使用以下方式调用它:

User32.INSTANCE.SetFocus(Win32WindowUtils.GetWindowHandle(windowTitle, null));

注意:

public String windowTitle = "Unbennant - Editor";

遗憾的是什么都没有发生,我不知道为什么

最佳答案

下面的代码片段遍历机器中打开的所有窗口,当找到具有特定标题的 Notepad++ 窗口时停止,然后将其置于焦点。将其置于焦点时,它按三次回车键。

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.StdCallLibrary;

public class TryWithHWND {
public interface User32 extends StdCallLibrary {
    User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

    boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);

    WinDef.HWND SetFocus(WinDef.HWND hWnd);

    int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);

    boolean SetForegroundWindow(WinDef.HWND hWnd);
}

public static void main(String[] args) {
    final User32 user32 = User32.INSTANCE;
    user32.EnumWindows(new WNDENUMPROC() {
        int count = 0;

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

            // get rid of this if block if you want all windows regardless
            // of whether
            // or not they have text
            if (wText.isEmpty()) {
                return true;
            }

            System.out.println("Found window with text " + hWnd
                    + ", total " + ++count + " Text: " + wText);
            if (wText
                    .equals("C:\\Users\\Avi.J\\Desktop\\Datasource and Mq setup commands.txt - Notepad++")) {
                user32.SetForegroundWindow(hWnd);
                return false;
            }
            return true;
        }
    }, null);
    // user32.SetFocus(hWnd);
    try {
        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_ENTER);
        r.keyRelease(KeyEvent.VK_ENTER);
        r.keyPress(KeyEvent.VK_ENTER);
        r.keyRelease(KeyEvent.VK_ENTER);
        r.keyPress(KeyEvent.VK_ENTER);
        r.keyRelease(KeyEvent.VK_ENTER);
        r.keyPress(KeyEvent.VK_ENTER);
        r.keyRelease(KeyEvent.VK_ENTER);
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

希望这有帮助,我相信发送击键的部分也可以由 User32 库完成。

关于Java JNA 聚焦特定窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35393786/

相关文章:

java - 我想用鼠标输入在窗口上绘制像素

java - 带有图像列的 CellTable

java - 老年代对象的统计?

java.io.FileNotFoundException : Acess Denied using jexcel

Javascript 文本字段值关注

javascript - html - 如何使文件列表仅在单击列表上的其他文件时失去焦点

html - 当输入 div 获得焦点时显示 div

java - 拍照并命名

java - 获取 LIMIT 子句中预期的错误非负整数值

c# - 外部窗口上的图像叠加