简体   繁体   English

检测到前景以暂停窗口的请求

[英]Detecting a request to foreground a suspended window

Is there a way to detect a request foreground a window in c#? 有没有一种方法可以在C#中检测到窗口的请求前景? What I'm trying to do is to suspend a process (not the process doing the detecting) when the user switches their focus to another window, and then resume the process when they switch back. 我正在尝试做的是在用户将焦点切换到另一个窗口时挂起一个进程(而不是进行检测的进程),然后在切换回另一个窗口时恢复该进程。

I managed to detect when the user switches their focus away, but since the process is suspended, I don't know how to detect if they try to switch back. 我设法检测出用户何时将焦点移开,但是由于该过程已暂停,因此我不知道如何检测用户是否尝试回切。

Edit: I wrote process 1 which is the process doing the detecting and suspending 编辑:我编写了过程1,这是执行检测和挂起的过程

Process 2 is the external process that I'm detecting whether the user has focus on it or not (from process 1). 流程2是我正在检测用户是否关注它的外部流程(来自流程1)。 If the user switches their focus from process 2, suspend process 2. And when they switch back, resume it (from process 1). 如果用户从流程2切换了焦点,则暂停流程2。当用户切换回该焦点时,请从流程1恢复。 What I don't know is how to detect if the user attwmpts to switches back if process 2 is suspended. 我不知道的是,如果进程2被挂起,如何检测用户是否要求切换回去。

Depending on the monitored application's framework, you may be able to get focus change events for it by using UI Automation, something like (copied from MSDN): 根据受监视的应用程序的框架,您可以使用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);
    }
}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM