我在嘗試讓這個 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條經(jīng)驗 獲得超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
提交
取消