使用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应用程序工程,使用一个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() + "\n" + title.ToString() + "\n" + className.ToString();  
        }  
    }  
}  

可执行程序:http://pan.baidu.com/s/1skX4bkp

原博客:http://blog.csdn.net/sytzz/article/details/25372103

posted @ 2016-09-21 02:39  Lichina  阅读(790)  评论(0编辑  收藏  举报