SetForegroundWindow仅在visual studio打开时工作

我正在尝试用c#设置一个进程窗口到前台/焦点(从那个时候没有焦点的应用程序),因此我使用user32.dll static extern bool SetForegroundWindow(IntPtr hWnd)方法:

 [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd); public void setFocus() { SetForegroundWindow(process.MainWindowHandle); } 

一切都工作得很好,但只有当我打开Visual Studio 2008时,我甚至不需要从VS08启动应用程序,它足以让项目打开它。 当我关闭项目时,我的应用程序无法将另一个窗口设置为前景。 唯一的结果是,在任务栏中,另一个窗口突出显示为蓝色。 我将再次使用VS08打开我的项目它的工作正常。

我没有丝毫想法为什么……我的问题可能是他不能导入dll但是它不会高亮,而其他win32函数就像static extern bool ShowWindow(IntPtr hWnd, IntPtr status); 即使项目结束也在工作。

针对这个问题的任何解决方案或提示?

编辑:

我读了这个函数的评论,我知道我的应用程序没有焦点,所以我尝试了这个:

  [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll")] static extern bool AllowSetForegroundWindow(int procID); [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", SetLastError = true)] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); public void setAUTFocus() { IntPtr hWnd = GetForegroundWindow(); uint processID = 0; uint threadID = GetWindowThreadProcessId(hWnd, out processID); int intID = (int)processID; AllowSetForegroundWindow(intID); SetForegroundWindow(process.MainWindowHandle); } 

现在我正在搜索当前具有焦点的窗口进程,并为此窗口设置“AllowSetForegroundWindow”并尝试将焦点设置在我的窗口上。 但同样的问题,我在VS的项目开启其工作的那一刻,如果不是我只在任务栏中获得蓝色突出显示。

在我的应用程序运行期间,我可以打开/关闭vs项目,它打开一切正常工作的那一刻,它关闭它不工作的那一刻,我在运行我的应用程序时没有与VS项目的交互。 srsly我不明白。

我在发送Alt键时遇到了问题,因为当我选择回车时强制窗口菜单打开(而不是我想要的OK按钮)。

这对我有用:

 public static void ActivateWindow(IntPtr mainWindowHandle) { //check if already has focus if (mainWindowHandle == GetForegroundWindow()) return; //check if window is minimized if (IsIconic(mainWindowHandle)) { ShowWindow(mainWindowHandle, Restore); } // Simulate a key press keybd_event(0, 0, 0, 0); SetForegroundWindow(mainWindowHandle); } 

在互联网上搜索了几天之后,我找到了一个简单的解决方案,让SetForegroundWindow可以在Windows 7上运行:在调用SetForegroundWindow之前按Alt键。

 public static void ActivateWindow(IntPtr mainWindowHandle) { //check if already has focus if (mainWindowHandle == GetForegroundWindow()) return; //check if window is minimized if (IsIconic(mainWindowHandle)) { ShowWindow(mainWindowHandle, Restore); } // Simulate a key press keybd_event((byte)ALT, 0x45, EXTENDEDKEY | 0, 0); //SetForegroundWindow(mainWindowHandle); // Simulate a key release keybd_event((byte)ALT, 0x45, EXTENDEDKEY | KEYUP, 0); SetForegroundWindow(mainWindowHandle); } 

而win32apiimport

  private const int ALT = 0xA4; private const int EXTENDEDKEY = 0x1; private const int KEYUP = 0x2; private const uint Restore = 9; [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool IsIconic(IntPtr hWnd); [DllImport("user32.dll")] private static extern int ShowWindow(IntPtr hWnd, uint Msg); [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow();