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.