将外部应用程序移动到屏幕的前面

我正在运行的应用程序需要调用单独的应用程序来进行扫描。 我正在通过启动一个新的System.Diagnostics.Process调用另一个应用程序。 一旦我完成了这个过程,我就会调用一个方法来为该应用程序提供重点。 我已经尝试了两种不同的方式来将外部应用程序作为重点,但两者都没有起作用。 有人可以帮忙吗?

这是代码:

 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); //SetForegroundWindow(p.MainWindowHandle); } catch { throw; } } 

第一种情况使用ShowWindowSetWindowPos方法,另一种方法使用SetForegroundWindow方法。 两者都不会起作用……

我使用了错误的方法,或者我在使用的代码中是否有错误? 谢谢大家!

使用SetWindowPos,但是当你不希望窗口成为最顶层的时候再次调用它,第二个参数设置为-2(HWND_NOTOPMOST)而不是-1(HWND_TOPMOST)

尝试将您的过程设置为背景,即:this.SendToBack(); 这只是另一种解决方案,部分修复。