使用C#解决部分Win8.1系统窗体每隔几秒失去焦点的问题

使用了Win8.1 With Update 1后,发现重新启动系统后,当前激活的窗体总是每隔几秒失去焦点。过0.5~1秒焦点回来。导致输入无法正常工作,严重影响使用心情和效率。

在网上找了非常久,也没找到对应的解决的方法。大多提供的是关闭计划任务中禁用阿里巴巴的自己主动更新任务(http://www.paopaoche.net/gonglue/21442.html)。

但是这种方法对我来说并无论用,并且那种是1小时执行一次。我的系统是每隔几秒就会出现一次。

忍受了1周,忍无可忍,于是决定自己解决。

窗体失去焦点。无非就是别的窗体将焦点抢占过去,假设能找到是什么程序抢占了窗体焦点,禁用之就能够解决。

由于是解决Windows问题。使用微软自家的C#解决这个问题。

打开VS创建C# Windows应用程序project,使用一个Lable显示信息。一个Timer定时获取当前激活窗体(毫秒级)。而且将信息显示到Lable就可以。当前台窗体焦点改变。从Lable中能够看到当前前台程序。


终于发现,是Broadcom 802.11 Network Adapter Wireless Network Tray Applet抢占了窗体焦点。

网上对其作用解释为:安装在一些使用无线网卡的戴尔计算机上。

它产生一个系统托盘图标,通过它,用户能够直接訪问无线网卡的各种配置功能。

看来没什么作用。将其在任务管理器启动项中禁用,重新启动系统,无线网卡功能正常,问题完美解决。


附上监控程序部分逻辑代码(未使用不论什么编码规范,未加不论什么凝视),窗体代码使用窗体设计器生成就可以。

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


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();//获取当前激活窗体

        [DllImport("user32", SetLastError = true)]
        public static extern int GetWindowText(
        IntPtr hWnd, //窗体句柄
        StringBuilder lpString, //标题
        int nMaxCount  //最大值
        );

        [DllImport("user32.dll")]
        private static extern int GetClassName(
            IntPtr hWnd, //句柄
            StringBuilder lpString, //类名
            int nMaxCount //最大值
        );

        public Form1()
        {
            InitializeComponent();

            timer1.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            IntPtr myPtr = GetForegroundWindow();

            // 窗体标题
            StringBuilder title = new StringBuilder(255);
            GetWindowText(myPtr, title, title.Capacity);

            // 窗体类名
            StringBuilder className = new StringBuilder(256);
            GetClassName(myPtr, className, className.Capacity);

            label1.Text = myPtr.ToString() + "
" + title.ToString() + "
" + className.ToString();
        }
    }
}

原文地址:https://www.cnblogs.com/tlnshuju/p/6901916.html