SetForegroundWindow only works during visual studio

I am trying to set the process window to the foreground / focus with C # (from an application that does not have focus at the moment when it is running), so I use the user32.dll method 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); } 

every thing works fine, but only if I have a visual studio in 2008 open, I don’t even need to run the application from VS08, it’s enough to open the project. At the moment when I close the project, my application cannot set another window to the foreground. the only result is that in the taskbar another window is highlighted in blue. the moment I am about to open my project with VS08, again its working tone.

I have no idea why ... I can understand that it cannot import dll, but then it will not be high-level, and other win32 functions like static extern bool ShowWindow(IntPtr hWnd, IntPtr status); , work even when the project is closed.

any solutions or tips for this problem?

Edit:

I read the comments for this function and I got the idea that my application has no focus, so I tried this:

  [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); } 

Now I'm looking for a window process that currently has focus, and set this window to "AllowSetForegroundWindow" and try to adjust the focus on my window. But the same problem, when I have a project in VS, open its work, if I do not get only the blue highlight on the taskbar.

while my application is running, I can open / close the vs project, at the moment when it is open, everything works, at the moment when it closed its work, I have no interaction with the VS-project during the execution of my application. srsly i don't understand.

+4
source share
2 answers

I had a problem sending the Alt key because it caused the menu window to open when I selected enter (instead of the OK button, which was what I wanted).

This worked for me:

 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); } 
+7
source

After searching several days on the Internet, I found one possible simple solution to get SetForegroundWindow to work on Windows 7: press the Alt key before calling SetForegroundWindow.

 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); } 

And import win32api

  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(); 
+5
source

Source: https://habr.com/ru/post/1414175/


All Articles