2016-01-13 28 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 

     } 

    } 
相關問題