To run the application that I run, you need to call a separate application. I call another application by running the new System.Diagnostics.Process . As soon as I get this process, I call the method to give this application focus. I tried two different ways to give this external application focus, but none of them work. Can anybody help?
Here is the code:
using System.Runtime.InteropServices; [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, uint windowStyle); [DllImport("user32.dll")] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags); [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); private static void GiveSpecifiedAppTheFocus(int processID) { try { Process p = Process.GetProcessById(processID); ShowWindow(p.MainWindowHandle, 1); SetWindowPos(p.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, 3);
The first script uses the ShowWindow and SetWindowPos , the other method uses the SetForegroundWindow method. None of them will work ...
Am I using the wrong methods or am I having an error in the code I'm using? Thanks everyone!
source share