1

I currently have a multi monitor app on the electron engine which opens a different window with separate content on each monitor. It uses 2 separate processes with the same name to display these windows, "home".

The app can then be used to launch different windows. One of these is called "game". This game opens a control style window on the main monitor, and then the actual game on the second monitor. Both of these are different processes both called "game".

We currently have a small C# console app that can be called and passed a string to try and bring these apps to the front which looks as below:

class Program
{
    [DllImport("user32.dll")]
    private static extern IntPtr SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    static void Main(string[] args)
    {
        BringToFront(args[0]);
    }

    private static void BringToFront(string title)
    {
        // Get a handle to the Calculator application.
        Process[] processes = Process.GetProcessesByName(title);

        if (processes.Length > 1)
        {
            foreach(Process process in processes)
            {
                IntPtr mainWindowHandle = process.MainWindowHandle;
                if (mainWindowHandle == GetForegroundWindow()) return;
                SetForegroundWindow(mainWindowHandle);
            }
        }
    }
}

The problem is that this only ever brings 1 window to the front, not both. I have researched this and found that it is due to the rules surrounding SetForegroundWindow, and unless I'm mistaken, when we set the first foreground window we are then not allowed to set any others as the console app/process that called the console app is no longer the foreground process.

Is there any way to accomplish bringing 2 separate windows to the front using a C# console app?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jack
  • 175
  • 2
  • 12
  • I wouldnt want them both in focus. SetForegroundWindow is the only way that I could find to bring a window back to the front of whatever monitor it was open on, and because they are fullscreen apps they need to hide the taskbar (which SetForegroundWindow does). The point of doing it to both was it would set the first as foreground and therefore it would fullscreen and gain focus. The second would then have SetForegroundWindow ran against it and would then take focus away and be fullscreen in it's monitor. I just need both to be brought to the front and be full screen. – Jack Mar 30 '16 at 13:34
  • Sorry deleted my comment because i realized what you wanted to do. Would this help http://stackoverflow.com/questions/3750113/launch-an-application-and-send-it-to-second-monitor? The windows position parts. – Jacobr365 Mar 30 '16 at 13:39
  • I think I tried it before but I will give it another shot to see if i misconfigured it. Thanks. – Jack Mar 30 '16 at 13:42

0 Answers0