0

Guided by post

How can I run another application within a panel of my C# program?

I'm running the application in the panel of Webform. However top left corner of the application is not aligned with the top left corner of the panel.

Whatever the size of the panel is, it always shows big gaps between the panel edge and the app window. enter image description here

Any ideas on how can I align the side app in the panel?

Code:

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


private void Form1_Load(object sender, EventArgs e)
{
    ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
    psi.WindowStyle = ProcessWindowStyle.Minimized;
    Process p = Process.Start(psi);
    Thread.Sleep(500);
    SetParent(p.MainWindowHandle, panel1.Handle);
    CenterToScreen();
    psi.WindowStyle = ProcessWindowStyle.Normal;
}
Tsoll
  • 21
  • 3
  • Follow notes and code here: [Unhook Window into its original State](https://stackoverflow.com/q/65842979/7444103) – Jimi Jul 26 '23 at 04:58

1 Answers1

0

Thanks to Jimi, it was really simple:

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

[DllImport("user32.dll")]
internal static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

private void Form1_Load(object sender, EventArgs e)
{
   ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
   Process p = Process.Start(psi);
   Thread.Sleep(500);
   SetParent(p.MainWindowHandle, panel1.Handle);
   MoveWindow(p.MainWindowHandle, 0, 0, panel1.Width, panel1.Height, true);
}
Tsoll
  • 21
  • 3