Get last active window: Get Previously active window

前端 未结 2 1014
我在风中等你
我在风中等你 2021-02-06 09:26

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 pre

2条回答
  •  Happy的楠姐
    2021-02-06 10:05

    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

    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. 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;
      }
    }
    

提交回复
热议问题