zoukankan      html  css  js  c++  java
  • WPF 让窗口激活作为前台最上层窗口的方法

    原文参照林大佬的博客WPF 让窗口激活作为前台最上层窗口的方法

    我只提供下,我使用的代码

    [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;
    }
  • 相关阅读:
    前端生成pdf文件之pdfmake.js
    vim 安装
    linux基础学习
    python 编码处理
    Ubuntu 下配置 SSH服务全过程及问题解决
    yum 安装
    Ubuntu安装MySQL
    Linux各发行版本及其软件包管理方法
    轻松学习LINUX系列教程推出
    常用命令
  • 原文地址:https://www.cnblogs.com/ZXdeveloper/p/12979902.html
Copyright © 2011-2022 走看看