How to open a process in Windows form

I would like to open a process in my windows form application.

For example, I would like for a user to click on a button in one of the Windows form containers to open mstsc.exe.

And if he clicks on the button, he will open IE on another container,

[DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
       private void button1_Click(object sender, EventArgs e)
    {
        Process p = Process.Start("mstsc.exe", @"c:\VPN4.rdp");
        Thread.Sleep(3000);
        p.StartInfo.CreateNoWindow = true;    // new
        SetParent(p.MainWindowHandle, this.panel1.Handle);
        p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;   //new

    }

It opens the process, but not in the form of a window,

+1
source share
2 answers

You can start another process and put it in your own.

Try using a form handle instead of panels. Here is a quick example

private void toolStripButton1_Click(object sender, EventArgs e)
{
  ProcessStartInfo info = new ProcessStartInfo();
  info.FileName = "notepad";
  info.UseShellExecute = true;
  var process = Process.Start(info);

  Thread.Sleep(2000);

  SetParent(process.MainWindowHandle, this.Handle);
}

, , , , , , MainWindowHandle - , .

FindWindowEx API Windows . , . Stackoverflow google . "# FindWindowEx"

+4
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "notepad";
info.UseShellExecute = true;
Process.Start(info);
+1

Source: https://habr.com/ru/post/1650027/


All Articles