我希望创建一个 C# 应用程序,根据当前具有焦点的应用程序来更改内容。因此,如果用户使用 Firefox,我的应用程序就会知道这一点。 Chrome、Visual Studio、TweetDeck 等也是如此。
这可能吗?如果可能的话,我将如何实现它?
我有一种感觉,我的要求太多了——但值得一试。
提前谢谢了。
这可以在纯 .NET 中使用自动化框架这是 WPF 的一部分。添加参考UIAutomationClient
and UIAutomationTypes
并使用Automation.AddAutomationFocusChangedEventHandler, e.g.:
public class FocusMonitor
{
public FocusMonitor()
{
AutomationFocusChangedEventHandler focusHandler = OnFocusChanged;
Automation.AddAutomationFocusChangedEventHandler(focusHandler);
}
private void OnFocusChanged(object sender, AutomationFocusChangedEventArgs e)
{
AutomationElement focusedElement = sender as AutomationElement;
if (focusedElement != null)
{
int processId = focusedElement.Current.ProcessId;
using (Process process = Process.GetProcessById(processId))
{
Debug.WriteLine(process.ProcessName);
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)