1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| import org.eclipse.swt.SWT;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.internal.win32.PROCESS_INFORMATION;
import org.eclipse.swt.internal.win32.STARTUPINFO;
import org.eclipse.swt.internal.win32.TCHAR;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Embedded {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Appli SWT");
final Composite c = new Composite(shell, SWT.EMBEDDED);
String command = "C:\\WINDOWS\\NOTEPAD.EXE";
int /*long*/ hHeap = OS.GetProcessHeap();
TCHAR buffer = new TCHAR(0, command, true);
int byteCount = buffer.length() * TCHAR.sizeof;
int /*long*/ lpCommandLine = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
OS.MoveMemory(lpCommandLine, buffer, byteCount);
// Start process
STARTUPINFO lpStartupInfo = new STARTUPINFO();
lpStartupInfo.cb = STARTUPINFO.sizeof;
PROCESS_INFORMATION lpProcessInformation = new PROCESS_INFORMATION();
boolean success = OS.CreateProcess(0, lpCommandLine, 0, 0,
true, 0, 0, 0, lpStartupInfo, lpProcessInformation);
if (lpCommandLine != 0) OS.HeapFree(hHeap, 0, lpCommandLine);
int handle = getWindowHandleForProcessId(lpProcessInformation.dwProcessId);//OS.FindWindow(null, new TCHAR(0, "Sans titre - Bloc-notes", true));
OS.SetWindowLong(handle, OS.GWL_STYLE, OS.WS_VISIBLE | OS.WS_CLIPCHILDREN | OS.WS_CLIPSIBLINGS);
OS.SetParent(handle, c.handle);
if (lpProcessInformation.hProcess != 0) OS.CloseHandle(lpProcessInformation.hProcess);
if (lpProcessInformation.hThread != 0) OS.CloseHandle(lpProcessInformation.hThread);
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
// Quit the started process
OS.PostMessage(handle, /*WM_QUIT*/0x0012, 0, 0);
display.dispose();
}
private static int /*long*/ getWindowHandleForProcessId(int/*long*/ pid) {
while (true) {
int dhandle = OS.FindWindow(null, null);
if (dhandle > 0) {
while (dhandle > 0) {
// Interested only in main top windows
if (OS.GetParent(dhandle) == 0) {
int[] i = new int[1];
OS.GetWindowThreadProcessId(dhandle, i);
if (i[0] == pid) {
return dhandle;
}
}
dhandle = OS.GetWindow(dhandle, OS.GW_HWNDNEXT);
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} |
Partager