2016-01-13 31 views
0

我想编写一个和桌面一样的程序(就像第二个桌面),我的意思是我想运行进程GUI而不是在浏览器进程中,我想运行它在我的表格上。 我发现我可以使用User32.dll,并找到了相应的代码。 这里是代码:在我自己的c#窗体上运行扩展程序GUI

private void LoadApplication(string path, IntPtr handle) 
    { 
     Stopwatch sw = new Stopwatch(); 
     sw.Start(); 
     int timeout = 10 * 1000;  // Timeout value (10s) in case we want to cancel the task if it's taking too long. 

     Process p = Process.Start(path); 

     //p.WaitForInputIdle(); 
     IntPtr Handle = new IntPtr(); 
     for (int i = 0; Handle == IntPtr.Zero && i < 300; i++) 
     { 
      Handle = p.MainWindowHandle; 
      Thread.Sleep(10); 
     } 

     while (Handle == IntPtr.Zero) 
     { 
      System.Threading.Thread.Sleep(10); 
      p.Refresh(); 

      if (sw.ElapsedMilliseconds > timeout) 
      { 
       sw.Stop(); 
       return; 
      } 
     } 

     SetParent(Handle, handle);  // Set the process parent window to the window we want 
     SetWindowPos(Handle, 0, 0, 0, 0, 0, 0x0001 | 0x0040);  // Place the window in the top left of the parent window without resizing it 
    } 

如果我运行代码到CMD的路径,Internet Explorer或记事本,甚至它的正常工作,但是当我尝试用Firefox运行它或它的CALC.EXE打开程序但不是在我的表单上 - 在explorer.exe 也许我做错了什么?或者也许还有其他方法可以根据我的需要做'虚拟桌面'?

感谢名单

+0

一些节目,如Firefox(与多数浏览器的这个问题)将启动多个进程幕后当你启动它们时,它创建的一个进程将成为你实际看到的窗口。这就是他们如何实现强制所有新帧出现在一个窗口或另一个窗口中的能力。因此,对于这些类型的流程,您将无法获得与他们自己的工作相同的控制级别,以便按照他们想要的方式显示事物。 –

+0

还有另一种方法将gui移动到表单中?也许其他语言? –

回答

0

我找到了解决办法!!!!你运行程序后,睡眠线程±3000秒,然后运行该代码等作为我:

private void LoadApplication(string path, IntPtr handle) 
    { 
     Stopwatch sw = new Stopwatch(); 
     sw.Start(); 
     int timeout = 10 * 1000;  // Timeout value (10s) in case we want to cancel the task if it's taking too long. 
     Process p = Process.Start(path); 
     Thread.Sleep(3000); 
     Process[] proc = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(path)); 
     for(int j = 0 ;j<proc.Length;j++) 
     { 
      p = proc[j]; 
      //p.WaitForInputIdle(); 
      IntPtr Handle = new IntPtr(); 
      Handle = p.MainWindowHandle; 
      if(Handle == IntPtr.Zero) 
      { 
       continue; 
      } 

      while (Handle == IntPtr.Zero) 
      { 
       System.Threading.Thread.Sleep(10); 
       p.Refresh(); 

       if (sw.ElapsedMilliseconds > timeout) 
       { 
        sw.Stop(); 
        return; 
       } 
      } 

      SetParent(Handle, handle);  // Set the process parent window to the window we want 

     } 

    } 
相关问题