OpenSimTools / RaceDirector

The sim racer's Swiss Army knife.
MIT License
3 stars 0 forks source link

Focus game window #75

Open paoloambrosio opened 2 years ago

paoloambrosio commented 2 years ago

Relates to #50.

Focus game window before applying pit strategy.

paoloambrosio commented 2 years ago

Tried this but it didn't work reliably.

Dependency:

<ItemGroup>
  <PackageReference Include="PInvoke.User32" Version="0.7.124" />
</ItemGroup>

Code:

using System.Diagnostics;
using PInvoke;

// ...

if (Process.GetProcessesByName("AC2-Win64-Shipping").Take(1)
    .Where(process => User32.GetForegroundWindow() != process.MainWindowHandle)
    .Any(process => !User32.SetForegroundWindow(process.MainWindowHandle)))
{
    Console.WriteLine("FAILED TO SET FOREGROUND WINDOW");
}