简体   繁体   English

获取上一个活动窗口:获取以前活动的窗口

[英]Get last active window: Get Previously active window

I am working on an application which needs to get the last active window handle. 我正在开发一个需要获取最后一个活动窗口句柄的应用程序。 Suppose my application is running then I want to get last active window handle that was just previously open just before my application. 假设我的应用程序正在运行,那么我想获取之前刚刚在我的应用程序之前打开的最后一个活动窗口句柄。

@EDIT1: This is not the duplicate question. @ EDIT1:这不是重复的问题。 I need to get the handle of last active window not the current window. 我需要获取最后一个活动窗口的句柄而不是当前窗口。

This is similar to alternate SO question , I would assume you would just track the active window and upon change you would then know the previously active 这类似于备用SO问题 ,我假设您只是跟踪活动窗口,然后在更改时您将知道之前的活动窗口

Edit, this is basically code copied from the question I linked that was looking for current active window but with logic to persist the lastHandle and identify when you have a new lastHandle. 编辑,这基本上是从我链接的问题中复制的代码,该问题是寻找当前活动窗口但是有逻辑来持久化lastHandle并确定何时有新的lastHandle。 It's not a proven, compilable implementation: 它不是经过验证的,可编译的实现:

[DllImport("user32.dll")]
  static extern IntPtr GetForegroundWindow();

static IntPtr lastHandle = IntPtr.Zero;

//This will be called by your logic on when to check, I'm assuming you are using a Timer or similar technique.
IntPtr GetLastActive()
{
  IntPtr curHandle = GetForeGroundWindow();
  IntPtr retHandle = IntPtr.Zero;

  if(curHandle != lastHandle)
  {
    //Keep previous for our check
    retHandle = lastHandle;

    //Always set last 
    lastHandle = curHandle;

    if(retHandle != IntPtr.Zero)
      return retHandle;
  }
}

I needed the same thing of the last handle from the previous window I had open. 我需要从我打开的上一个窗口的最后一个句柄中获得相同的内容。 The answer from Jamie Altizer was close, but I modified it to keep from overwriting the previous window when my application gets focus again. Jamie Altizer的答案很接近,但我修改它以避免在我的应用程序再次获得焦点时覆盖之前的窗口。 Here is the full class I made with the timer and everything. 这是我用计时器和所有东西制作的完整课程。

static class ProcessWatcher
{
    public static void StartWatch()
    {
        _timer = new Timer(100);
        _timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        _timer.Start();
    }

    static void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        setLastActive();
    }

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    public static IntPtr LastHandle
    {
        get
        {
            return _previousToLastHandle;
        }
    }

    private static void setLastActive()
    {
        IntPtr currentHandle = GetForegroundWindow();
        if (currentHandle != _previousHandle)
        {
            _previousToLastHandle = _previousHandle;
            _previousHandle = currentHandle;
        }
    }

    private static Timer _timer;
    private static IntPtr _previousHandle = IntPtr.Zero;
    private static IntPtr _previousToLastHandle = IntPtr.Zero;
}

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

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