好吧,有时候我在打字,很少有事情会失去焦点,我读了一些解决方案(甚至是VB watch ),但是它们不适用于我。是否有Windows范围内的“句柄”可以处理任何焦点更改?

不管使用哪种语言,C,C++,VB.NET,C#,任何.NET或Windows相关,批处理,PoweShell,VBS脚本...只要我能够监视每个焦点更改并将其登录文件/cmd窗口/可视窗口。

就像是:

   void event_OnWindowsFocusChange(int OldProcID, int NewProcID);

会非常有用。或者也许已经有用于此的工具(我找不到?)

最佳答案

一种方法是使用Windows UI自动化API。它公开了一个全局焦点已更改的事件。这是我想出的一个快速示例(在C#中)。注意,您需要添加对UIAutomationClient和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);
                }
            }
        }
    }
}

10-07 19:01
查看更多