1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[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);
}
}
|