I have a System.Diagnostics.Process variable named _program.
I understand that not every process have a user interface, so it will never have a focus (I think).
But, supposing that this process has a interface, is it possible to set focus to it ? Maybe I need to use Process.StandardInput?
Asked
Active
Viewed 413 times
0

Vinícius Velloso
- 3
- 1
-
2Possible duplicate of [Correct way (in .NET) to switch the focus to another application](https://stackoverflow.com/questions/2315561/correct-way-in-net-to-switch-the-focus-to-another-application) – Oct 10 '18 at 18:27
1 Answers
3
Use PInvoke to call a native function to set the foreground window:
using System.Diagnostics;
using System.Runtime.InteropServices;
[DllImport("User32.dll")]
private static extern Int32 SetForegroundWindow(nint hWnd);
void YourMethod()
{
Process p = ... // However you create your process
SetForegroundWindow(p.MainWindowHandle); // Set this process's main window as the foreground window
}

Hirashi3630
- 15
- 5

MikeH
- 4,242
- 1
- 17
- 32