是否可以在WinForms应用程序中运行独立的应用程序

本文关键字:应用程序 运行 独立 WinForms 是否 | 更新日期: 2023-09-27 18:28:18

我需要使用WinForms主机应用程序控制一个独立的应用程序,就好像另一个应用程序在远程桌面上运行,而我新开发的主机应用程序是远程桌面主机一样。CodeProject的文章《使用C#的远程桌面》启发了我,我的任务可能完成的可能性不是零。它解释了如何使用"Microsoft终端服务控制类型库"或MSTSCLib.dll来执行此操作。

但是,我不想连接到远程桌面。如果有什么不同的话,我想连接到同一台机器上的第二个桌面,如果这是独立运行托管应用程序或类似程序所必需的。MSTSCLib有可能做到这一点吗?如果是这样的话,我需要从哪些方面进行进一步的设计?

重要提示:无法访问外部程序代码的限制不再存在。"来宾"程序将仅来自一系列专门定义的程序。

是否可以在WinForms应用程序中运行独立的应用程序

我的一个朋友有时做了这项工作!

你应该这样做:

第一个导入系统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); 
    }

运行方法的输入参数是要在应用程序中运行的程序的路径

希望有帮助!:)