`
xblia
  • 浏览: 84982 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

其它程序内嵌swt实例_002

 
阅读更多

private void executeProg(String fileName) throws Exception {
		int hHeap = OS.GetProcessHeap();
		TCHAR buffer = new TCHAR(0, fileName, true);
		int byteCount = buffer.length() * TCHAR.sizeof;
		int lpFile = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
		OS.MoveMemory(lpFile, buffer, byteCount);
		SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
		info.cbSize = SHELLEXECUTEINFO.sizeof;
		info.lpFile = lpFile;
		// 隐藏启动
		info.nShow = OS.SW_HIDE;
		boolean result = OS.ShellExecuteEx(info);
		if (lpFile != 0)
			OS.HeapFree(hHeap, 0, lpFile);
		if (result == false)
			throw new Exception("启动失败!");
	}

	protected void startNotePad() throws Exception {
		// "notepad.exe"为待启动的程序名
		executeProg("notepad.exe");

		// 等待NotePad.exe启动并且初始化完毕,需要根据实际情况调整sleep的时间
		Thread.sleep(1000);

		// "Notepad"为被嵌套程序窗口的ClassName(Win32级别),可以使用Spy++等工具查看
		int notepadHwnd = OS.FindWindow(new TCHAR(0, "Notepad", true), null);

		// &~WS_BORDER去掉内嵌程序边框,这样看起来更像一个内嵌的程序。如果需要显示边框,则将这两行代码删除
		int oldStyle = OS.GetWindowLong(notepadHwnd, OS.GWL_STYLE);
		OS.SetWindowLong(notepadHwnd, OS.GWL_STYLE, oldStyle & ~OS.WS_BORDER);

		// composite为承载被启动程序的控件
		OS.SetParent(notepadHwnd, composite.handle);
		// 窗口最大化
		OS.SendMessage(notepadHwnd, OS.WM_SYSCOMMAND, OS.SC_MAXIMIZE, 0);
	}

	protected void startCMD() throws Exception {
		// "notepad.exe"为待启动的程序名
		executeProg("cmd.exe");

		// 等待NotePad.exe启动并且初始化完毕,需要根据实际情况调整sleep的时间
		Thread.sleep(1000);

		// "Notepad"为被嵌套程序窗口的ClassName(Win32级别),可以使用Spy++等工具查看
		int notepadHwnd = OS.FindWindow(
				new TCHAR(0, "ConsoleWindowClass", true), null);

		// &~WS_BORDER去掉内嵌程序边框,这样看起来更像一个内嵌的程序。如果需要显示边框,则将这两行代码删除
		int oldStyle = OS.GetWindowLong(notepadHwnd, OS.GWL_STYLE);
		OS.SetWindowLong(notepadHwnd, OS.GWL_STYLE, oldStyle & ~OS.WS_BORDER);

		// composite为承载被启动程序的控件
		OS.SetParent(notepadHwnd, composite.handle);
		// 窗口最大化
		OS.SendMessage(notepadHwnd, OS.WM_SYSCOMMAND, OS.SC_MAXIMIZE, 0);
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics