Launch another app from unity withour lose focus (silent run)

Hi!

Here is little task description.
I have two applications app1 and app2.
app1 is running in fullscreen mode and launching app2 using Process(), but when app2 is started, app1 focus is lost and app1 is minimized.

How to prevent app1 lose focus and minimize, when app2 is launched?

So in best case app1 running in fullscreen, then launching app2 and app1 still in fullscreenmode without focus lost.

Thanks in advance!

So I’ve found the solution.

  1. Run app1 with keys -popupwindow -screen-height %full_height% -screen-width %full_width%
  2. In app1 create process with minimized or hidden WindowStyle option in Process.StartInfo.
  3. In app2 add next code:

“VideosSelector” - Caption on main form of app1.

using UnityEngine;
using System.Collections;
using UnityEngine.VR;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
using System.Threading;
using System.Collections.Generic;
using System.Text;

public class CMDFilesToPlay : MonoBehaviour {

    void Awake()
    {
        var videosSelectors = FindWindowsWithText("VideosSelector");
        foreach (IntPtr hwnd in videosSelectors)
        {
            Thread.Sleep(100);
            SetForegroundWindow(hwnd);
            break;
        }
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport("user32.dll")]
    private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

    // Delegate to filter which windows to include
    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    /// <summary> Get the text for the window pointed to by hWnd </summary>
    public static string GetWindowText(IntPtr hWnd)
    {
        int size = GetWindowTextLength(hWnd);
        if (size > 0)
        {
            var builder = new StringBuilder(size + 1);
            GetWindowText(hWnd, builder, builder.Capacity);
            return builder.ToString();
        }

        return String.Empty;
    }

    /// <summary> Find all windows that match the given filter </summary>
    /// <param name="filter"> A delegate that returns true for windows
    ///    that should be returned and false for windows that should
    ///    not be returned </param>
    public static IEnumerable<IntPtr> FindWindows(EnumWindowsProc filter)
    {
        IntPtr found = IntPtr.Zero;
        List<IntPtr> windows = new List<IntPtr>();

        EnumWindows(delegate(IntPtr wnd, IntPtr param)
        {
            if (filter(wnd, param))
            {
                // only add the windows that pass the filter
                windows.Add(wnd);
            }

            // but return true here so that we iterate all windows
            return true;
        }, IntPtr.Zero);

        return windows;
    }

    /// <summary> Find all windows that contain the given title text </summary>
    /// <param name="titleText"> The text that the window title must contain. </param>
    public static IEnumerable<IntPtr> FindWindowsWithText(string titleText)
    {
        return FindWindows(delegate(IntPtr wnd, IntPtr param)
        {
            return GetWindowText(wnd).Contains(titleText);
        });
    }
}