2013-08-20 233 views
1

我需要使用WinForms主機應用程序控制獨立應用程序,就好像其他應用程序在遠程桌面上運行一樣,而我新開發的主機應用程序是遠程桌面主機。 CodeProject文章Remote Desktop using C#.NET令人鼓舞,我的任務可能的可能性不是零。它解釋瞭如何使用「Microsoft終端服務控制類型庫」或MSTSCLib.dll來執行此操作。是否可以在WinForms應用程序中運行獨立應用程序?

儘管如此,我不想連接到遠程桌面。如果有的話,我想連接到同一臺機器上的第二個桌面,如果這是獨立運行託管應用程序或類似的東西所必需的。這完全可能與MSTSCLib?如果是這樣,我需要考慮哪些方面來進一步爲此設計一個設計?

重要通知:無法訪問外部程序代碼的限制不再存在。 「客人」節目將僅限於一系列特別定義的節目。

+3

你的意思是你想要將他們的UI嵌入你的內部?無需他們的合作,這並不簡單。 (並且它完全與TS無關;您需要Windows API方法來重新授予Windows) – SLaks

+2

簡短回答否,長答案,一切皆有可能...... – Jodrell

+0

@Slaks:這就是爲什麼ProfK希望「成爲本地主機上的TS客戶端」,而不是重新設置窗口:因爲TS *可以在沒有「託管」UI(通常是)的合作下完成。 – Medinoc

回答

1

我的一個朋友做了這項工作有時候是前!

你應該做的事情是這樣的:

首次導入系統DLL:

[DllImport("user32.dll")] 
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

    [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)] 
    private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong); 

    [DllImport("user32.dll", SetLastError = true)] 
    private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint); 

,現在我宣佈一個計時器,並按照下列代碼:

private const int GWL_STYLE = (-16); 
    private const int WS_VISIBLE = 0x10000000; 
    Process p; 
/*Closing Is Timer*/ 
     private void Closing_Tick(object sender, EventArgs e) 
    { 


      p.Refresh(); 
      string a = p.ProcessName;    
       SetParent(p.MainWindowHandle, panel1.Handle); 
       SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE); 
       MoveWindow(p.MainWindowHandle, 0, 0, this.Width, this.Height, true); 


    } 
    void run(string add) 
    { 
     string addres = add; 

     try 
     { 
      p = Process.Start(addres); 
      Thread.Sleep(500); // Allow the process to open it's window 
      SetParent(p.MainWindowHandle, panel1.Handle); 
      SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE); 
      MoveWindow(p.MainWindowHandle, 0, 0, this.Width, this.Height, true); 


     } 
     catch 
     { 
      Closeing.Enabled = false; 
      MessageBox.Show(addres + "\n" + "Not Found", "Error", 
      MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading); 
      Environment.Exit(0); 

     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Closeing.Enabled = true; 
     run(@textBox1.Text); 
    } 

輸入參數運行的方法是程序的路徑使用想要在您的應用程序中運行

Hop e此幫助! :)

相關問題