1

I want to open an application inside a Panel in a Form.
The name of the application is Stellarium. When the application first opens, it presents a splash screen, as in picture 1:

enter image description here

After a while, the main program opens as in picture 2:

enter image description here

The problem is this: when I run the code below, the application opens and the splash screen in picture 1 is placed inside the Panel, but the main application (picture 2) opens outside the Panel.
What should I do to open the application inside the Panel?

My code:

private void Notepad_Button_Click(object sender, EventArgs e)
{
    var psi = new ProcessStartInfo(@"C:\Program Files\Stellarium\stellarium.exe") { 
        WindowStyle = ProcessWindowStyle.Minimized 
    };

    using (var p = Process.Start(psi))
    {
        p.WaitForInputIdle();
       
        var handle = p.MainWindowHandle;
        
        if (handle != IntPtr.Zero)
        {
            SetParent(handle, panel1.Handle);
            MoveWindow(handle, 0, 0, panel1.Width, panel1.Height, true);
        }
    }
}

When I add a timer like the code below, the application opens inside the Panel.
But using a Timer is not the right solution, in my opinion.
Thanks you for your support.

private void Notepad_Button_Click(object sender, EventArgs e)
{
    var psi = new ProcessStartInfo(@"C:\Program Files\Stellarium\stellarium.exe") { WindowStyle = ProcessWindowStyle.Minimized };
    using (var p = Process.Start(psi))
    {
        p.WaitForInputIdle();
       
        var handle = p.MainWindowHandle;
        System.Threading.Thread.Sleep(20000);

        if (handle != IntPtr.Zero)
        {
            SetParent(handle, panel1.Handle);
            MoveWindow(handle, 0, 0, panel1.Width, panel1.Height, true);
        }
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
Selim Boz
  • 29
  • 3
  • Did you create a new account for this question? It's the same as [Print an image of the content of a Panel excluding any external overlapping Window](https://stackoverflow.com/q/68890833/7444103). -- There are different ways to generate or receive a notification when a Window of a specific Process or a Window of any Process with specific *properties* is created and shown on screen. I often use UIAutomation's `WindowPattern.WindowOpenedEvent`, an event that is raised when any new Window in the System is first shown. – Jimi Aug 24 '21 at 17:43
  • An example of a WindowWatcher [here](https://stackoverflow.com/a/55028688/7444103) See also the other answer, it could be used for polling. - Another here: [Capture Button Click event inside a MessageBox in another application](https://stackoverflow.com/a/58233065/7444103) - WMI events are also available. – Jimi Aug 24 '21 at 17:51
  • @Jimi thank you. I will work for what you say. – Selim Boz Aug 24 '21 at 19:39

0 Answers0