关于电脑在输入时跳窗口/窗口失去焦点 的解决方法

最近在公司电脑用输入法时,总是会失去焦点,窗口内容在输入一半时就会跳到别的窗口,于是写了个软件监控一下到底是哪个小可爱在抢夺窗口的控制权。

环境:winform4.7.2

代码如下:在界面上记得加个label,并修改名称为lblProcessInfo

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        // 导入Windows API
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        public static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint processId);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int Width, int Height, uint Flags);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetConsoleWindow();

        // 置顶窗口标志
        private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        private static readonly uint SWP_NOMOVE = 0x0002;
        private static readonly uint SWP_NOSIZE = 0x0001;

        public MainForm()
        {
            InitializeComponent();
            // 设置窗口为置顶
            SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

            // 启动定时器,每隔一秒检查一次焦点窗口
            Timer timer = new Timer();
            timer.Interval = 1000; // 每秒一次
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            // 获取当前前景窗口句柄
            IntPtr foregroundHwnd = GetForegroundWindow();

            // 获取前景窗口的进程 ID
            uint processId;
            GetWindowThreadProcessId(foregroundHwnd, out processId);

            try
            {
                // 获取进程对象
                Process process = Process.GetProcessById((int)processId);

                // 获取应用程序的详细信息
                string processName = process.ProcessName;
                string filePath = process.MainModule.FileName;

                // 更新窗体上的标签信息
                lblProcessInfo.Text = $"窗口句柄: {foregroundHwnd}\n" +
                                      $"进程 ID: {processId}\n" +
                                      $"应用程序名称: {processName}\n" +
                                      $"执行路径: {filePath}";
            }
            catch (Exception ex)
            {
                // 异常处理,无法获取进程信息
                lblProcessInfo.Text = $"无法获取进程信息: {ex.Message}";
            }
        }

        // 窗体加载事件
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // 设置窗体大小和初始位置
            this.Size = new System.Drawing.Size(400, 300);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "焦点窗口监控器";
        }
    }
}

然后继续做输入,果然发现了,是WPS2016在捣乱,真是个小可爱,果断卸载,解决问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值