none
Displaying non-windows exe application inside windows form RRS feed

  • Question

  • How can I embed a non-native application (application installed in windows 7)in a windows form using c#.

    What I have tried so far:

    public partial class Form1 : Form 
    
    { 
    Process process = new Process();
    
     [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    
        [DllImport("user32.dll", SetLastError = true)]
        internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        public Form1()
        {
            InitializeComponent();
            // panel1.Container.Add(process);
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = false;
             string path = Path.Combine(Path.GetFullPath(@"C:\Program Files (x86)\Plan-G v3.2.0"), "Plan-G3.exe");
             process = Process.Start(path);
    
             Debug.WriteLine(process.MainWindowHandle);
             while (process.MainWindowHandle == IntPtr.Zero)
            {
                 Thread.Sleep(1000); // Don't hog the CPU
                process.Refresh(); // You need this since `MainWindowHandle` is cached
    
    
            }
    
            Form1.SetForegroundWindow(process.MainWindowHandle);
            SetForegroundWindow(process.MainWindowHandle);
    
            SetParent(process.MainWindowHandle, panel1.Handle);
    
            MoveWindow(process.MainWindowHandle, 0, 0, panel1.Width - 90, panel1.Height, true);
    
    
        }
    }

    Friday, October 26, 2018 5:49 AM

Answers

All replies

  • I have also tried:

    public partial class Form1 : Form
        {
            [DllImport("user32.dll")]
            static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
    
            [DllImport("user32.dll")]
            private static extern IntPtr GetForegroundWindow();
    
            [DllImport("kernel32.dll")]
            static extern uint GetCurrentThreadId();
            [DllImport("user32.dll")]
            static extern bool AttachThreadInput(uint idAttach, uint idAttachTo,
       bool fAttach);
    
            [DllImport("user32.dll", SetLastError = true)]
            static extern bool BringWindowToTop(IntPtr hWnd);
    
            [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
            static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
            [DllImport("user32.dll", SetLastError = true)]
            static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    
            [DllImport("user32.dll", SetLastError = true)]
            internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
            Process process = new Process();
            private static void ForceForegroundWindow(IntPtr hWnd)
    
            {
    
                uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
    
                uint appThread = GetCurrentThreadId();
    
                const int SW_SHOW = 5;
    
                if (foreThread != appThread)
    
                {
    
                    AttachThreadInput(foreThread, appThread, true);
    
                    BringWindowToTop(hWnd);
    
                    ShowWindow(hWnd, SW_SHOW);
    
                    AttachThreadInput(foreThread, appThread, false);
    
                }
    
                else
    
                {
    
                    BringWindowToTop(hWnd);
    
                    ShowWindow(hWnd, SW_SHOW);
    
                }
    
            }
            public Form1()
            {
                InitializeComponent();
    
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.UseShellExecute = false;
                string path = Path.Combine(Path.GetFullPath(@"C:\Program Files (x86)\Plan-G v3.2.0"), "Plan-G3.exe");
                process = Process.Start(path);
    
                Debug.WriteLine(process.MainWindowHandle);
                while (process.MainWindowHandle == IntPtr.Zero)
                {
                    Thread.Sleep(1000); // Don't hog the CPU
                    process.Refresh(); // You need this since `MainWindowHandle` is cached
    
    
                }
    
                //Form1.SetForegroundWindow(process.MainWindowHandle);
                ForceForegroundWindow(process.MainWindowHandle);
    
                SetParent(process.MainWindowHandle, this.Handle);
    
                MoveWindow(process.MainWindowHandle, 0, 0, this.Width - 90, this.Height, true);
                 
    
    
            }
        }

    I am able to get this to work sometimes when i start with debugging and the application opens within the form. But when i start without debugging, the application always opens outside of the form. I don't understand why this happens.

    Is what I am trying to accomplish possible? Based on other forum answers, it seems it might not be
    
    Thank you

    Friday, October 26, 2018 5:52 AM
  • This forum is for questions and discussions about the forums themselves. I would ask your question here:

    https://social.msdn.microsoft.com/Forums/en-us/home?forum=csharpgeneral

    https://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, October 26, 2018 12:32 PM
    Moderator