Is it possible to run an independent application inside a WinForms application?

人走茶凉 提交于 2019-12-24 12:03:12

问题


I need to control an independent application using a WinForms host application as if the other application were running on a Remote Desktop and my newly developed host application was the Remote Desktop host. The CodeProject article Remote Desktop using C#.NET is inspiring that the likelihood that my task is possible is not zero. It explains how to use the “Microsoft Terminal Services Control Type Library”, or MSTSCLib.dll to do this.

Howevere, I do not want to connect to a remote desktop. If anything, I want to connect to a second desktop on the same machine if that is necessary to independently run the hosted application, or something similar. Is this at all possible with MSTSCLib? If so, what aspects do I need to look at to further develop a design for this?

IMPORTANT NOTICE: The constraint of not having access to the code of the external program no longer exists. The 'guest' programs will be only from a range of specially defined programs.


回答1:


One Of My Friend Did this Work Sometimes Ago !

You Should Do Something Like This :

first import system DLLs :

    [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);

Now declare a Timer And Follow these Codes :

    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); 
    }

Input Parameter Of Run Method Is the path of program that use want to run in your Application

Hope This Help! :)



来源:https://stackoverflow.com/questions/18337463/is-it-possible-to-run-an-independent-application-inside-a-winforms-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!