I have to bring an already opened window to the foreground and focus it to give inputs from my program. I list all running processes, select the one I need, and bring it foreground, but the problem is that this process has 3 windows. I need only one from the 3, but my solution brings all three in front. All three windows has a different name. Does anyone have any ideas? Maybe get the windows by name somehow?
Process[] processes = Process.GetProcessesByName(ProcessName);
if (processes.Length > 0)
{
Process p = Process.GetCurrentProcess();
int n = 0;
if (processes[0].Id == p.Id)
{
n = 1;
}
IntPtr hWnd = processes[n].MainWindowHandle;
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, SW_RESTORE);
}
SetForegroundWindow(hWnd);