有没有办法在c#中检测请求前景窗口?我尝试做的是当用户将焦点切换到另一个窗口时暂停进程(而不是进行检测的进程),然后在切换回来时恢复进程。
我设法检测用户何时切换焦点,但由于该过程暂停,我不知道如何检测他们是否尝试切换回来。
编辑:我编写了进程1,它是执行检测和暂停的进程
过程2是我检测用户是否关注它的外部过程(来自过程1)。如果用户从进程2切换他们的焦点,则暂停进程2.当他们切换回来时,恢复它(从进程1)。我不知道的是,如果进程2被暂停,如何检测用户是否会切换回来。
答案 0 :(得分:0)
根据受监控的应用程序框架,您可以使用UI自动化获取焦点更改事件,例如(从MSDN复制):
`AutomationFocusChangedEventHandler focusHandler = null;
/// <summary>
/// Create an event handler and register it.
/// </summary>
public void SubscribeToFocusChange()
{
focusHandler = new AutomationFocusChangedEventHandler(OnFocusChange);
Automation.AddAutomationFocusChangedEventHandler(focusHandler);
}
/// <summary>
/// Handle the event.
/// </summary>
/// <param name="src">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
private void OnFocusChange(object src, AutomationFocusChangedEventArgs e)
{
// TODO Add event handling code.
// The arguments tell you which elements have lost and received focus.
System.Windows.Automation.AutomationElement element = src as System.Windows.Automation.AutomationElement;
if(element.Current.ProcessId == "MontioredApplicationProcessId")
{
//Monitored application has focus!
}
else
//Monitored application does not have focus
}
/// <summary>
/// Cancel subscription to the event.
/// </summary>
public void UnsubscribeFocusChange()
{
if (focusHandler != null)
{
Automation.RemoveAutomationFocusChangedEventHandler(focusHandler);
}
}`