您当前的位置: 首页 > 

君子居易

暂无认证

  • 0浏览

    0关注

    210博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

WPF 让窗口激活作为前台最上层窗口的方法

君子居易 发布时间:2020-05-29 08:35:37 ,浏览量:0

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

[DllImport("user32.dll")]
private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

public static void SetWindowToForegroundWithAttachThreadInput(Window window)
{
    var interopHelper = new WindowInteropHelper(window);
    var thisWindowThreadId = GetWindowThreadProcessId(interopHelper.Handle, IntPtr.Zero);
    var currentForegroundWindow = GetForegroundWindow();
    var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);
    
    AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);

    window.Show();
    window.Activate();
     // 去掉和其他线程的输入链接
    AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
    // 用于踢掉其他的在上层的窗口
            window.Topmost = true;
            window.Topmost = false;
}

林德熙大佬原文地址

关注
打赏
1660814979
查看更多评论
立即登录/注册

微信扫码登录

0.0394s