Skip to content

Commit 7151fec

Browse files
committed
加上设置窗口到最前的辅助方法
1 parent 35eb871 commit 7151fec

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/Shared/HandyControl_Shared/Tools/Helper/WindowHelper.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,44 @@ public static Thickness WindowMaximizedPadding
152152
public static IntPtr GetHandle(this Window window) => new WindowInteropHelper(window).EnsureHandle();
153153

154154
public static HwndSource GetHwndSource(this Window window) => HwndSource.FromHwnd(window.GetHandle());
155+
156+
/// <summary>
157+
/// 让窗口激活作为前台最上层窗口
158+
/// </summary>
159+
/// <param name="window"></param>
160+
public static void SetWindowToForegroundWithAttachThreadInput(Window window)
161+
{
162+
// [WPF 让窗口激活作为前台最上层窗口的方法 - lindexi - 博客园](https://www.cnblogs.com/lindexi/p/12749671.html)
163+
var interopHelper = new WindowInteropHelper(window);
164+
// 以下 Win32 方法可以在 https://github.com/kkwpsv/lsjutil/tree/master/Src/Lsj.Util.Win32 找到
165+
var thisWindowThreadId = InteropMethods.GetWindowThreadProcessId(interopHelper.Handle, out _);
166+
var currentForegroundWindow = InteropMethods.GetForegroundWindow();
167+
var currentForegroundWindowThreadId =
168+
InteropMethods.GetWindowThreadProcessId(currentForegroundWindow, out _);
169+
170+
// [c# - Bring a window to the front in WPF - Stack Overflow](https://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf )
171+
// [SetForegroundWindow的正确用法 - 子坞 - 博客园](https://www.cnblogs.com/ziwuge/archive/2012/01/06/2315342.html )
172+
/*
173+
  1.得到窗口句柄FindWindow
174+
    2.切换键盘输入焦点AttachThreadInput
175+
    3.显示窗口ShowWindow(有些窗口被最小化/隐藏了)
176+
    4.更改窗口的Z Order,SetWindowPos使之最上,为了不影响后续窗口的Z Order,改完之后,再还原
177+
    5.最后SetForegroundWindow
178+
*/
179+
180+
InteropMethods.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);
181+
182+
window.Show();
183+
window.Activate();
184+
// 去掉和其他线程的输入链接
185+
InteropMethods.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
186+
187+
// 用于踢掉其他的在上层的窗口
188+
if (window.Topmost != true)
189+
{
190+
window.Topmost = true;
191+
window.Topmost = false;
192+
}
193+
}
155194
}
156195
}

src/Shared/HandyControl_Shared/Tools/Interop/InteropMethods.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ internal static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddr
4343
[DllImport(InteropValues.ExternDll.User32, SetLastError = true, CharSet = CharSet.Auto)]
4444
internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
4545

46+
[DllImport(InteropValues.ExternDll.User32, SetLastError = true, CharSet = CharSet.Auto)]
47+
internal static extern bool AttachThreadInput(in uint currentForegroundWindowThreadId,
48+
in uint thisWindowThreadId, bool isAttach);
49+
50+
[DllImport(InteropValues.ExternDll.User32, SetLastError = true, CharSet = CharSet.Auto)]
51+
internal static extern IntPtr GetForegroundWindow();
52+
4653
[DllImport(InteropValues.ExternDll.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
4754
internal static extern IntPtr OpenProcess(InteropValues.ProcessAccess dwDesiredAccess,
4855
[MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId);

0 commit comments

Comments
 (0)