9

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. I need to get the handle of last active window not the current window.

Pandey Vinay
  • 121
  • 1
  • 5

2 Answers2

5

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;
  }
}
Community
  • 1
  • 1
Jamie Altizer
  • 1,540
  • 1
  • 11
  • 16
  • This is not the duplicate question. I need to get the handle of last active window not the current window. – Pandey Vinay Jan 15 '10 at 08:39
  • 2
    @Vinay, I said similar. That question gives the knowledge how to use Win32 APIs thus I assumed the handle returned by GetForegroundWindow() was appropriate for your use. This is not a duplicate but definitely similar or a sub question within a larger one asked. If you are looking for an implementation to copy and paste sorry, but I gave you logic in my answer... track all ACTIVE windows, once you have found a second ACTIVE window, you now have knowledge of the previous. I'll edit with some psuedo code. – Jamie Altizer Jan 15 '10 at 12:59
2

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. 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;
}
Simon The Cat
  • 604
  • 1
  • 8
  • 22