我需要实现在form中嵌套另外的exe程序,现在做的程序只能嵌套txt文件进form,对于exe程序不能嵌套进form中,不清楚具体原因,我写的代码如下: (主要程序为红色字体)(.exe程序运行界面完全在form中,最大化、最小化、移动都在form内部变化---对于txt文档已实现!)
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;
[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); [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
private static extern long SetWindowLong(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 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, long wParam, long lParam); [DllImport("user32.dll ", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
Process process = null;
IntPtr appWin;
private string exeName = "";
//在控件改变大小的时候,调用:
private void Form1_Load(object sender, EventArgs e)
{
exeName = @"C:\Documents and Settings\Administrator\桌面\JPSKB.exe";//程序路径
try
{
// Start the process
process = System.Diagnostics.Process.Start(this.exeName);
// Wait for process to be created and enter idle condition
process.WaitForInputIdle();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
// Get the main handle
appWin = process.MainWindowHandle;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Error");
}
// Put it into this form
SetParent(appWin, this.splitContainer1.Panel1.Handle);
// Remove border and whatnot
//SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
// Move the window to overlay it on this window
MoveWindow(appWin, 0, 0,this.Width, this.Height, true);
}
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;
[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); [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
private static extern long SetWindowLong(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 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, long wParam, long lParam); [DllImport("user32.dll ", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
Process process = null;
IntPtr appWin;
private string exeName = "";
//在控件改变大小的时候,调用:
private void Form1_Load(object sender, EventArgs e)
{
exeName = @"C:\Documents and Settings\Administrator\桌面\JPSKB.exe";//程序路径
try
{
// Start the process
process = System.Diagnostics.Process.Start(this.exeName);
// Wait for process to be created and enter idle condition
process.WaitForInputIdle();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
// Get the main handle
appWin = process.MainWindowHandle;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Error");
}
// Put it into this form
SetParent(appWin, this.splitContainer1.Panel1.Handle);
// Remove border and whatnot
//SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
// Move the window to overlay it on this window
MoveWindow(appWin, 0, 0,this.Width, this.Height, true);
}
解决方案 »
- checkbox勾选问题
- 项目表的每个字段应该怎么写呀?
- SplitContainer右侧里的窗体里的textbox里的内容没法用光标定位
- 求助
- 晕,OleDbCommand居然不能直接查询Access数据库的GUID字段
- 为何datagrid我设置了AllowSorting=false还是能排序呢?
- ComboBox绑定ArrayList后,怎么才能让其TEXT框为空
- 使用showModelessDialog这个怎么才能更改其在页面标头上显示的“网页对话框”这几个字?
- 简单的dataGrid问题,在线等待中。。。
- 想买本C#的书,有使用和收藏价值,100元左右。大谢。
- 如何产生包含公私钥的pfx格式证书
- 火急!!!如何用js给tabled的属性width赋值?
你这个不能嵌套有可能是因为你要嵌套的程序还没有启动完成你就调用SetParent API函数了,尝试一下延时看能否解决。或者看看Process类有没有检测程序是否启动完成的方法。
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.WorkingDirectory = Environment.CurrentDirectory; process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.Arguments = 你需要的参数,最主要的是Mplayer的-wid 选项。具体的,可以google或者看Mplayer的官方文档。