How to determine the last active windows from a tray icon notification handler
-
Hi, I am almost an expert on system programming, so I hope this GUI related question is easy to one of you: I have an app that installs an icon in the system tray. If the user clicks on the icon a dialog appears. Now the user may switch to some other app, so the dialog maybe active (has the focus) and in the foreground or somewhere in the background. If the user now clicks again the tray icon, I want to do the following:
- If the dialog is not active, I want to activate it and bring it to front.
- If the dialog is active, I want to dismiss it.
My problem is to determine inside the tray notification handler if the dialog has been active or not. I already tried the following:
GetForegroundWindow()
returns the handle of the system tray window. (Well, this makes sense, it is the active window during the execution of the notification handler...)GetFocus()
always returns NULLGetActiveWindow()
also returns NULL- Same for
GetLastActivePopup()
It seems that if you click on a tray icon, the system calls the notification handler and afterwards silently reactivates the window that was active before. That's the reason why
GetForegroundWindow()
always returns the system tray window, but the "visibly active window" seems not to loose its focus. However, I need to know which window was active before the icon was clicked. Any ideas :confused: -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D ) -
Hi, I am almost an expert on system programming, so I hope this GUI related question is easy to one of you: I have an app that installs an icon in the system tray. If the user clicks on the icon a dialog appears. Now the user may switch to some other app, so the dialog maybe active (has the focus) and in the foreground or somewhere in the background. If the user now clicks again the tray icon, I want to do the following:
- If the dialog is not active, I want to activate it and bring it to front.
- If the dialog is active, I want to dismiss it.
My problem is to determine inside the tray notification handler if the dialog has been active or not. I already tried the following:
GetForegroundWindow()
returns the handle of the system tray window. (Well, this makes sense, it is the active window during the execution of the notification handler...)GetFocus()
always returns NULLGetActiveWindow()
also returns NULL- Same for
GetLastActivePopup()
It seems that if you click on a tray icon, the system calls the notification handler and afterwards silently reactivates the window that was active before. That's the reason why
GetForegroundWindow()
always returns the system tray window, but the "visibly active window" seems not to loose its focus. However, I need to know which window was active before the icon was clicked. Any ideas :confused: -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )Trace the state in the dialog. In its WM_xxxFOCUS you will be notified which window actually receives the focus if it is not the tray some other app is activated. This still leaves you with the problem when the user clicks on another trayicon first and then on your applications icon. Or clicks on the explorer bar and then on the app icon. Or does it by keyboard. However, you should find a better design for your dialog solution. ...if you're under 8 or younger. Chris Maunder, the Lounge
-
Trace the state in the dialog. In its WM_xxxFOCUS you will be notified which window actually receives the focus if it is not the tray some other app is activated. This still leaves you with the problem when the user clicks on another trayicon first and then on your applications icon. Or clicks on the explorer bar and then on the app icon. Or does it by keyboard. However, you should find a better design for your dialog solution. ...if you're under 8 or younger. Chris Maunder, the Lounge
Thanks Andreas, However, you should find a better design for your dialog solution. I thought a lot about it, but I don't think that a really nice design for this problem even exists. The dialog is deactivated if somebody clicks the icon and there is no chance to examine if it was activated before. I now found a solution which is similar to what you suggested. I catch WM_ACTIVATE in the dialog and remember the current system time if the dialog is deactivated. In the tray icons handler I check the dialog was deactivated immediately before the handler was executed (less than 200 ms). If this is the case, it was deactivated because of clicking on the icon. Not the cleanest design, but works fairly well - even on high system load. Thanks! -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )