Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a commander for games to make commands easier
but i can't figure out how to bring the game to the front without restarting it
I'm using c#

What I have tried:

process = Process.Start(@"D:\Games\SteamLibary\steamapps\common\ARK\ShooterGame\Binaries\Win64\Shootergame.exe");
handle = process.MainWindowHandle;
SetForegroundWindow(handle);
Posted
Updated 15-Feb-18 5:42am

You need to import this function like this:

C#
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

process = Process.Start(@"D:\Games\SteamLibary\steamapps\common\ARK\ShooterGame\Binaries\Win64\Shootergame.exe");
handle = process.MainWindowHandle;
SetForegroundWindow(handle); 

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
 
Share this answer
 
Comments
Member 13679701 15-Feb-18 9:54am    
No i need to do it without starting it the program needs to be open allready
Use Process.GetProcessesByName[^] to retrieve a list of the running processes with the specified name.
C#
Process theProcess;
Process[] processes = Process.GetProcessesByName("shootergame");
if (processes.Length == 0)
{
    // The game is not running:
    theProcess = Process.Start(@"...");
}
else
{
    theProcess = processes[0];
}

handle = theProcess.MainWindowHandle;
SetForegroundWindow(handle);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900