mvanrijnen 123 Posted October 29, 2021 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
Fr0sT.Brutal 900 Posted October 29, 2021 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
mvanrijnen 123 Posted October 29, 2021 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
MarkShark 27 Posted October 29, 2021 (edited) 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 October 29, 2021 by MarkShark typo fix 1 Share this post Link to post
Remy Lebeau 1393 Posted October 30, 2021 (edited) 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 October 30, 2021 by Remy Lebeau Share this post Link to post
MarkShark 27 Posted October 30, 2021 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
David Heffernan 2345 Posted October 31, 2021 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
toms 29 Posted October 31, 2021 Retrieve the foreground window when the mouse enters the tray icon using an "OnMouseEnter" event. https://stackoverflow.com/questions/49896871/ttrayicon-with-onmouseenter-onmouseleave-events 1 Share this post Link to post
mvanrijnen 123 Posted November 1, 2021 23 hours ago, toms said: Retrieve the foreground window when the mouse enters the tray icon using an "OnMouseEnter" event. https://stackoverflow.com/questions/49896871/ttrayicon-with-onmouseenter-onmouseleave-events thnx, was the most simple solution for me! Share this post Link to post
David Heffernan 2345 Posted November 1, 2021 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
mvanrijnen 123 Posted November 1, 2021 (edited) 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 November 1, 2021 by mvanrijnen Share this post Link to post
toms 29 Posted November 1, 2021 CoolTrayIcon doesn't use a timer but WM_TRAYNOTIFY + WM_MOUSEMOVE Share this post Link to post
mvanrijnen 123 Posted November 1, 2021 Went to the enumwindows solution, more solid. thxn guys for the tips. Share this post Link to post