我需要实现在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);
         }

解决方案 »

  1.   

    exe也可以的呀 除非那个exe没有Handle
    你这个不能嵌套有可能是因为你要嵌套的程序还没有启动完成你就调用SetParent API函数了,尝试一下延时看能否解决。或者看看Process类有没有检测程序是否启动完成的方法。
      

  2.   

    通过FindWindow函数来找窗口Handle吧,用Spy++查看窗口类
      

  3.   

    Setparent API 谷歌下,有很多资料。
      

  4.   

    另外参考下:
         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;
      

  5.   

    运行cmd.exe和notepad.exe(txt应用程序)可以实现嵌套,对于暴风影音,和我们需要的嵌套的一个绿色软件都不能实现,连大小都未能改变。
      

  6.   

    嘿嘿,偷笑中,已实现MPlayer嵌入Winform播放和控制自如啊。 主要是Mplayer的传递的参数问题。除了我上面提供的那几句之外,需要另外加上:
     process.StartInfo.Arguments = 你需要的参数,最主要的是Mplayer的-wid 选项。具体的,可以google或者看Mplayer的官方文档。
      

  7.   

    方法已经告诉你了。而且基本的代码都已经告诉你,就剩下Mplayer的命令行参数了,这个你最好自己找,然后定制你所需要的命令行参数,对你以后也有帮助。你就自己动动手吧。嘿嘿。