我在尝试让这个 JNA findwindow 工作时遇到问题,它给我 Windows ID (HWND) 而不是一些疯狂的输出 (native@0xc41bcc)。我通过使用 C# 了解到,在使用 findwindow 时,它会提供找到的窗口的 ID,然后我可以使用该 ID 来移动/调整该窗口的大小/等等。我的代码:public String exec(ITestExecutionServices tes, String[] args) { try { writer = new PrintWriter("c:/temp/the-file-name.txt", "UTF-8"); } catch (FileNotFoundException | UnsupportedEncodingException e1) { e1.printStackTrace(); } try { final User32 user32 = User32.INSTANCE; try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } user32.EnumWindows(new WNDENUMPROC() { @Override public boolean callback(HWND hWnd, Pointer arg1) { byte[] windowText = new byte[512]; user32.GetWindowTextA(hWnd, windowText, 512); String wText = Native.toString(windowText); wText = (wText.isEmpty()) ? "" : wText; if (wText.toLowerCase().contains("- funct test -")) { writer.println("text: " + wText); writer.println("HWND: " + hWnd); //User32.INSTANCE.SetWindowPos(hwnd, new HWND(Pointer.createConstant(HWND_BOTTOM)), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); WinDef.HWND hwndFound = User32.INSTANCE.FindWindow(null, wText); writer.println("hwndFound: " + hwndFound); writer.println("t/f: " + User32.INSTANCE.MoveWindow(hwndFound, 500, 500, 10, 100, true)); //User32.INSTANCE.SetWindowPos(hWnd, new HWND(Pointer.createConstant(HWND_BOTTOM)), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); } return true; } }, null); }
1 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
我认为这可能会有所帮助:
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
而这个功能:
public long getWindowId(String title) {
WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, title);
return Pointer.nativeValue(hwnd.getPointer());
}
添加回答
举报
0/150
提交
取消