如何监控重点的变化? [英] How to monitor focus changes?

查看:132
本文介绍了如何监控重点的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧有时我打字,很少碰巧的东西偷焦点,我读了一些解决方案(甚至是VB手表),但它们并不适用于我。是否有任何Windows的宽'把手',它处理所有焦点的变化?

Well Sometimes I am typing and very rarely it happens that something steals focus, I read some solution (even a VB watch) but they don't apply to me. Is there any windows-wide 'handle' which handles ANY focus changes?

不要紧,其中语言,C,C ++,VB.NET,C#.NET任何或窗口有关,批次,P​​oweShell,VBS脚本......只要我能够监控每焦点更改和其记录到文件/ cmd窗口/可视窗口。

It doesn't matter in which language, C, C++, VB.NET, C#, Anything .NET or windows related, Batch, PoweShell, VBS Script... As Long as I am able to monitor every focus change and log it into a file/cmd window/visual window.

是这样的:

   void event_OnWindowsFocusChange(int OldProcID, int NewProcID);

将是非常有用的。或者,也许有这个工具已经(我不能找到?)

would be very usefull. Or maybe there are tools for this already (which I can't find?)

推荐答案

的一种方法是使用Windows UI自动化API。它暴露了全球关注的焦点更改事件。这里是我想出了一个快速的样品(在C#)。请注意,您需要将引用添加到UIAutomationClient和UIAutomationTypes。

One way would be to use the windows UI Automation API. It exposes a global focus changed event. Here is a quick sample I came up with (in C#). Note, you need to add references to UIAutomationClient and UIAutomationTypes.

using System.Windows.Automation;
using System.Diagnostics;

namespace FocusChanged
{
    class Program
    {
        static void Main(string[] args)
        {
            Automation.AddAutomationFocusChangedEventHandler(OnFocusChangedHandler);
            Console.WriteLine("Monitoring... Hit enter to end.");
            Console.ReadLine();
        }

        private static void OnFocusChangedHandler(object src, AutomationFocusChangedEventArgs args)
        {
            Console.WriteLine("Focus changed!");
            AutomationElement element = src as AutomationElement;
            if (element != null)
            {
                string name = element.Current.Name;
                string id = element.Current.AutomationId;
                int processId = element.Current.ProcessId;
                using (Process process = Process.GetProcessById(processId))
                {
                    Console.WriteLine("  Name: {0}, Id: {1}, Process: {2}", name, id, process.ProcessName);
                }
            }
        }
    }
}

这篇关于如何监控重点的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆