Jump to content
mvanrijnen

Traybar application, get the last active windows on desktop

Recommended Posts

I'm trying to catch the last active window in a traybar application.

So as soon as the user rightclicks my traybaricon, the popupmenu shows, at that (or just before), i need to have the last active window (in windows, not a window of my application).

I tried a few things with GetNextWindow(Application.MainWindowHandle, GW_HWNDPREV) and a few other options. but without success, anyone has a good tip in the right direction?

 

Share this post


Link to post

I don't get it. You want a window that your app's window lays on? But you have traybar app so there should be no window visible.

Share this post


Link to post

No, sorry i'm not clear 🙂

 

The traybar app would need to know, when its activated which window in windows was active. 

So if i have notepad open and selected, and rightclick my traybar app, i need to have the handle to the notepad window,

if i have also another app open (e.g. calc) and selected,  and rightclick my traybar app, i need to have the handle to the calc app,

 

 

Share this post


Link to post

I had to do this for a password management tray app I wrote for personal use.  Here's the technique I ended up with (with lots of help from stack overflow and others.)

 

Step 1:

  Use EnumWindows with a callback to build up a list of window handles.  They will be in the current z-order. It needs to check if the window is visible, unowned, and has WS_EX_APPWINDOW in its style before adding it to your list.

Step 2:

  Loop through the window handles above and find the first one in the list with a thread process id different than your current program.  This is done using GetWindowThreadProcessId on each window handle from your enum list and comparing to the results of GetCurrentThreadID.

 

That window handle should be the main window of the app that was last active.   I would perform the above whenever my tray app was activated. I'm guessing there are exceptions for "stay on top" apps and such but it seemed to work for my needs.  I hope that helps or at least gives a clue!

 

-Mark

 

Update note:  Once I get the window handle of the last activated app I would also call GetLastActivePopup on it (as I needed to know if it had a modal popup active.)

 

Edited by MarkShark
typo fix
  • Like 1

Share this post


Link to post
On 10/29/2021 at 7:51 AM, MarkShark said:

I had to do this for a password management tray app I wrote for personal use.  Here's the technique I ended up with (with lots of help from stack overflow and others.)

Why not simply use GetForegroundWindow() or even GetGUIThreadInfo() instead?

Edited by Remy Lebeau

Share this post


Link to post

Remy,  all the techniques I tried caused my application to be the foreground one.  I'm guessing that's what the OP found as well.  That might be how the TRzTrayIcon works or possibly I just missed something obvious back then. 

Share this post


Link to post

I think that when windows are activated they are also told which window is being deactivated to make this window activate. Perhaps that's what you need. 

Share this post


Link to post
12 minutes ago, mvanrijnen said:

thnx, was the most simple solution for me!

 

It uses a timer to poll and therefore is likely to be unreliable 

Share this post


Link to post
3 minutes ago, David Heffernan said:

It uses a timer to poll and therefore is likely to be unreliable 

yes, looking into that now. 

i'm testing a combinantion of things written here in this thread.

 

Edited by mvanrijnen

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×