Java调用Win32实现操作其他程序窗口

1、用到的包

①jna-4.4.0.jar和jna-platform-4.4.0(下载地址)

②JNative.jar(下载地址)

2、使用方式:向QQ输入文本为例

public class QQTest {
	public static void main(String[] args) {
		// 第一个参数是Windows窗体的窗体类,第二个参数是窗体的标题。
		HWND hwnd = User32.INSTANCE.FindWindow(null, "QQ");
		if (hwnd == null) {
			System.out.println("QQ is not running");
		} else {
			User32.INSTANCE.ShowWindow(hwnd, 9); // SW_RESTORE
			User32.INSTANCE.SetForegroundWindow(hwnd); // bring to front
			WinDef.RECT qqwin_rect = new WinDef.RECT();
			User32.INSTANCE.GetWindowRect(hwnd, qqwin_rect);
			int qqwin_width = qqwin_rect.right - qqwin_rect.left;
			int qqwin_height = qqwin_rect.bottom - qqwin_rect.top;
			User32.INSTANCE.MoveWindow(hwnd, 0, 0, qqwin_width, qqwin_height, true);
			User32.INSTANCE.PostMessage(hwnd, User32.WM_CLOSE, null, null);
			String username = "11111111111";
			for (Character c : username.toCharArray())
				sendChar(c);
			@formatter:off
			User32.INSTANCE.GetForegroundWindow(); // 获取现在前台窗口
			WinDef.RECT qqwin_rect = new WinDef.RECT();
			User32.INSTANCE.GetWindowRect(hwnd, qqwin_rect);
			int qqwin_width = qqwin_rect.right - qqwin_rect.left;
			int qqwin_height = qqwin_rect.bottom - qqwin_rect.top;
			User32.INSTANCE.MoveWindow(hwnd, 700, 100, qqwin_width, qqwin_height, true);
			for (int i = 700; i > 100; i -= 10) {
				User32.INSTANCE.MoveWindow(hwnd, i, 100, qqwin_width, qqwin_height, true);
				try {
					Thread.sleep(10);
				} catch (Exception e) {
				}
			}
			User32.INSTANCE.PostMessage(hwnd, WinUser.WM_CLOSE, null, null);
		@formatter:on
		}
	}
	private static WinUser.INPUT input = new WinUser.INPUT();
	private static void sendChar(char ch) {
		input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
		input.input.setType("ki");
		input.input.ki.wScan = new WinDef.WORD(0);
		input.input.ki.time = new WinDef.DWORD(0);
		input.input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
		// Press
		input.input.ki.wVk = new WinDef.WORD(Character.toUpperCase(ch)); // 0x41
		input.input.ki.dwFlags = new WinDef.DWORD(0); // keydown
		User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());
		// Release
		input.input.ki.wVk = new WinDef.WORD(Character.toUpperCase(ch)); // 0x41
		input.input.ki.dwFlags = new WinDef.DWORD(2); // keyup
		User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());
	}
}
2、自定义User32接口发送文本(原Use32接口只允许十进制或十六进制数字)

public interface MyUser32 extends StdCallLibrary, WinUser {
	static MyUser32 INSTANCE = (MyUser32) Native
			.loadLibrary("user32", MyUser32.class, W32APIOptions.DEFAULT_OPTIONS);
	int SendMessage(HWND hWnd, int Msg, int wParam, String lParam);
	// http://msdn.microsoft.com/en-us/library/windows/desktop/ms633573(v=vs.85).aspx
	interface WindowProc extends StdCallCallback {
		LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam);
	}
	// http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx
	WindowProc SetWindowLongPtr(HWND hWnd, int nIndex, WindowProc dwNewLong);
	// http://msdn.microsoft.com/en-us/library/windows/desktop/ms633584(v=vs.85).aspx
	WindowProc GetWindowLongPtr(HWND hWnd, int nIndex);
}



你可能感兴趣的:(Java)