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?