Diese Funktion bringt ein Fenster (Übergabeparameter = System.Windows.Forms.Form) in den Vordergrund des Desktops, auch wenn sich andere Programme zur Zeit im Vordergrund befinden.
using System.Runtime.InteropServices;
Achso .. Lauffähig ab Framework 2
[DllImport("User32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
public static extern int AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, bool fAttach);
[DllImport("User32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("User32.dll")]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hwnd, IntPtr lpdwProcessId);
public static void SetForegroundWindowEx(Form window)
{
IntPtr hndl = window.Handle;
IntPtr threadID1 = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
IntPtr threadID2 = GetWindowThreadProcessId(hndl, IntPtr.Zero);
window.TopMost = true;
if (threadID1 == threadID2)
{
SetForegroundWindow(hndl);
}
else
{
AttachThreadInput(threadID2, threadID1, true);
SetForegroundWindow(hndl);;
AttachThreadInput(threadID2, threadID1, false);
}
}
Kommentare zum Snippet