今天在調試awt的robot鼠標定位問題,每次移動的位置都不一樣,最後定位到robot在java1.8下有bug,系統也不能隨便升級java版本,沒辦法只能放棄這種方式。
後來又選中了jna、jna-platform的方式調用,下面是大概的步驟,供日後和大家參考:
1、引入依賴jar包
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>4.5.1</version>
</dependency>
2、示例代碼
WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, "postman");
if (hwnd == null) {
System.out.println("TSITSMonitor is not running");
} else {
WinDef.RECT win_rect = new WinDef.RECT();
User32.INSTANCE.GetWindowRect(hwnd, win_rect);
int win_width = win_rect.right - win_rect.left;
int win_height = win_rect.bottom - win_rect.top;
User32.INSTANCE.MoveWindow(hwnd, 300, 100, win_width, win_height, true);
}
postman爲窗口標題
注意點:以上方式是winapi的調用模式,對dotnet開發的窗體不好用,失敗!