其它程序内嵌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);}}?