How to run external application in panel when SetParent() does not work C#

562 views Asked by At

I am trying to trigger and application (in this case SAP2000) inside a C# program so that the application is shown in a panel. I have searched in different posts and it seems that the 'SetParent()' strategy is the most suitable for this purpose. However it is also true that some users have reported to have some problems with it and different approaches of the 'SetParent()' strategy have been defined. Below you can find mine:

Despite high 'delay' values defined in 'proc.WaitForInputIdle(1000)' and 'Thread.Sleep(10000)', the application (SAP2000) not always is shown in the panel and when it does the application buttons can be pressed but they don't react (see picture).

My question is: is 'SetParent()' a suitable method to run this kind of 'heavy' applications in a C# program panel? Or is it necessary another strategy for this case? If so, I would really appreciate any advice to deal with this issue.

Thanks in advance. Regards

using System;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace Prueba_OAPI_CSi_SAP
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Process proc = Process.Start("C:\\Program Files\\Computers and Structures\\SAP2000 22\\SAP2000.exe");
            if (proc != null)
            {
                proc.WaitForInputIdle(1000);
                while (proc.MainWindowHandle == IntPtr.Zero)
                {
                    Thread.Sleep(10000);
                    proc.Refresh();
                }

                OpcionesTeclado.SetParent(proc.MainWindowHandle, panel2.Handle);
                OpcionesTeclado.MessageBeep(1);
                this.BringToFront();
            }
        }

    public class OpcionesTeclado
    {               
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        public static extern bool MessageBeep(int uType);
    }
}

enter image description here

0

There are 0 answers