The following example opens word and then embeds it into the WinForm form, such as:
C Embed the EXE run program into its own WinForm form-kingmax_res-isport
Note: This method applies only to COM's exe (such as Word,excel), and the. NET codec cannot be embedded in the form with this method.
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;using system.diagnostics;using System.Runtime.InteropServices; namespace windowstest{public partial class Form2:form {process process = null; IntPtr Appwin; private string exename = ""; [DllImport ("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true, CharSet = Charset.unicod E, ExactSpelling = true, CallingConvention = Callingconvention.stdcall)] private static extern long GETW Indowthreadprocessid (Long hWnd, long lpdwprocessid); [DllImport ("user32.dll", SetLastError = True)] private static extern IntPtr FindWindow (String lpclassname, String l Pwindowname); [DllImport ("user32.dll", SetLastError = True)] private static extern long SetParent (IntPtr hwndchild, IntPtr hwndne Wparent); [DllImport ("User32.dll", EntrypoiNT = "Getwindowlonga", SetLastError = True)] private static extern long GetWindowLong (IntPtr hwnd, int nIndex); [DllImport ("user32.dll", EntryPoint = "Setwindowlonga", SetLastError = True)] private static extern long Setwi Ndowlong (IntPtr hwnd, int nIndex, long dwnewlong); private static extern int SetWindowLong (IntPtr hWnd, int nIndex, IntPtr dwnewlong); [DllImport ("user32.dll", SetLastError = True)] private static extern long SetWindowPos (INTPTR hwnd, long Hwndinsert After, long x, long y, long CX, long CY, long wflags); [DllImport ("user32.dll", SetLastError = True)] private static extern bool MoveWindow (INTPTR hwnd, int x, int y, int CX, int CY, BOOL repaint); [DllImport ("user32.dll", EntryPoint = "Postmessagea", SetLastError = True)] private static extern bool PostMessage ( INTPTR hwnd, UINT MSG, long wParam, long LParam); Private Const int swp_noownerzorder = 0x200; Private Const int Swp_noredraw = 0x8; Private Const int swp_nozorder = 0x4; Private Const int Swp_showwindow = 0x0040; Private Const int ws_ex_mdichild = 0x40; Private Const int swp_framechanged = 0x20; Private Const int swp_noactivate = 0x10; Private Const int swp_asyncwindowpos = 0x4000; Private Const int swp_nomove = 0x2; Private Const int swp_nosize = 0x1; Private Const int Gwl_style = (-16); Private Const int ws_visible = 0x10000000; Private Const int wm_close = 0x10; Private Const int ws_child = 0x40000000; public string EXEName {get {return exename; } set {exename = value; }} public Form2 () {InitializeComponent (); private void Button1_Click (object sender, EventArgs e) {this.exename = @ "D:\Program FILES\MICR Osoft Office\Office11\Winword.exe "; try {//Start the process process = System.Diagnostics.Process.Start (this.exe Name); Wait for process to is created and enter idle condition process. WaitForInputIdle (); Get the main handle appwin = process. Mainwindowhandle; } catch (Exception ex) {MessageBox.Show (this, ex). Message, "Error"); }//Put it into the This form SetParent (Appwin, this. Handle); Remove border and whatnot//SetWindowLong (Appwin, Gwl_style, ws_visible); Move the window to overlay it in this window MoveWindow (appwin, 0, 0, this. Width, this. Height, True); private void Form2_formclosed (object sender, Formclosedeventargs e) {try { Process. Kill (); } catch {}} private void Form2_resize (oBject sender, EventArgs e) {if (This.appwin! = IntPtr.Zero) {MoveWindow (APPW In, 0, 0, this. Width, this. Height, True); }//base. OnResize (e); } }}
C # embeds an EXE run program into its own WinForm form