Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to determine the last active windows from a tray icon notification handler

How to determine the last active windows from a tray icon notification handler

Scheduled Pinned Locked Moved C / C++ / MFC
toolshelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Lohmann
    wrote on last edited by
    #1

    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:

    1. If the dialog is not active, I want to activate it and bring it to front.
    2. 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 NULL
    • GetActiveWindow() 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 )

    A 1 Reply Last reply
    0
    • D Daniel Lohmann

      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:

      1. If the dialog is not active, I want to activate it and bring it to front.
      2. 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 NULL
      • GetActiveWindow() 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 )

      A Offline
      A Offline
      Andreas Saurwein
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • A Andreas Saurwein

        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

        D Offline
        D Offline
        Daniel Lohmann
        wrote on last edited by
        #3

        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 )

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups