1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform;
2.新建一个类文件,方便引用,命名为:exetowinform;
3.Mainform中cs代码如下:
-
exetowinform fr = null;
-
private void button1_Click(object sender, EventArgs e)
-
{
-
OpenFileDialog Oppf = new OpenFileDialog();
-
Oppf.ShowDialog();
-
if (Oppf.FileName != "")
-
{
-
panel1.Controls.Clear();
-
fr = new exetowinform(panel1, "");
-
fr.Start(Oppf.FileName);
-
}
-
}
4.exetowinform类文件代码如下:
-
public class exetowinform
-
{
-
EventHandler appIdleEvent = null;
-
Control ParentCon = null;
-
string strGUID = "";
-
-
public exetowinform(Control C, string Titlestr)
-
{
-
appIdleEvent = new EventHandler(Application_Idle);
-
ParentCon = C;
-
strGUID = Titlestr;
-
}
-
-
-
-
-
public IntPtr Start(string FileNameStr)
-
{
-
if (m_AppProcess != null)
-
{
-
Stop();
-
}
-
try
-
{
-
ProcessStartInfo info = new ProcessStartInfo(FileNameStr);
-
info.UseShellExecute = true;
-
info.WindowStyle = ProcessWindowStyle.Minimized;
-
m_AppProcess = System.Diagnostics.Process.Start(info);
-
m_AppProcess.WaitForInputIdle();
-
Application.Idle += appIdleEvent;
-
}
-
catch
-
{
-
if (m_AppProcess != null)
-
{
-
if (!m_AppProcess.HasExited)
-
m_AppProcess.Kill();
-
m_AppProcess = null;
-
}
-
}
-
return m_AppProcess.Handle;
-
}
-
-
-
-
-
-
void Application_Idle(object sender, EventArgs e)
-
{
-
if (this.m_AppProcess == null || this.m_AppProcess.HasExited)
-
{
-
this.m_AppProcess = null;
-
Application.Idle -= appIdleEvent;
-
return;
-
}
-
-
while (m_AppProcess.MainWindowHandle == IntPtr.Zero)
-
{
-
Thread.Sleep(100);
-
m_AppProcess.Refresh();
-
}
-
Application.Idle -= appIdleEvent;
-
EmbedProcess(m_AppProcess, ParentCon);
-
}
-
-
-
-
-
-
void m_AppProcess_Exited(object sender, EventArgs e)
-
{
-
m_AppProcess = null;
-
}
-
-
-
-
public void Stop()
-
{
-
if (m_AppProcess != null)
-
{
-
try
-
{
-
if (!m_AppProcess.HasExited)
-
m_AppProcess.Kill();
-
}
-
catch (Exception)
-
{
-
}
-
m_AppProcess = null;
-
}
-
}
-
-
-
#region 属性
-
-
-
-
Process m_AppProcess = null;
-
-
-
-
-
public bool IsStarted { get { return (this.m_AppProcess != null); } }
-
-
#endregion 属性
-
-
#region Win32 API
-
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
-
CharSet = CharSet.Unicode, ExactSpelling = true,
-
CallingConvention = CallingConvention.StdCall)]
-
private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
-
-
[DllImport("user32.dll", SetLastError = true)]
-
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
-
-
[DllImport("user32.dll", SetLastError = true)]
-
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
-
-
[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
-
private static extern long GetWindowLong(IntPtr hwnd, int nIndex);
-
-
public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
-
{
-
if (IntPtr.Size == 4)
-
{
-
return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
-
}
-
return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
-
}
-
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
-
public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
-
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
-
public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);
-
-
[DllImport("user32.dll", SetLastError = true)]
-
private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, 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, uint wParam, uint lParam);
-
-
[DllImport("user32.dll", SetLastError = true)]
-
private static extern IntPtr GetParent(IntPtr hwnd);
-
-
[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
-
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
-
-
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;
-
-
private const int SW_HIDE = 0;
-
private const int SW_SHOWNORMAL = 1;
-
private const int SW_NORMAL = 1;
-
private const int SW_SHOWMINIMIZED = 2;
-
private const int SW_SHOWMAXIMIZED = 3;
-
private const int SW_MAXIMIZE = 3;
-
private const int SW_SHOWNOACTIVATE = 4;
-
private const int SW_SHOW = 5;
-
private const int SW_MINIMIZE = 6;
-
private const int SW_SHOWMINNOACTIVE = 7;
-
private const int SW_SHOWNA = 8;
-
private const int SW_RESTORE = 9;
-
private const int SW_SHOWDEFAULT = 10;
-
private const int SW_MAX = 10;
-
-
#endregion Win32 API
-
-
-
-
-
private void EmbedProcess(Process app, Control control)
-
{
-
-
if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;
-
try
-
{
-
-
SetParent(app.MainWindowHandle, control.Handle);
-
}
-
catch (Exception)
-
{ }
-
try
-
{
-
-
SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);
-
SendMessage(app.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, strGUID);
-
}
-
catch (Exception)
-
{ }
-
try
-
{
-
-
MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);
-
}
-
catch (Exception)
-
{ }
-
}
-
-
[DllImport("User32.dll", EntryPoint = "SendMessage")]
-
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
-
-
const int WM_SETTEXT = 0x000C;
-
}
5.最后结果如图: