2013-08-20 152 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此帮助! :)

相关问题