No announcement yet.

Tray Application - Window Focus/ZOrder Oddities...

  • Filter
  • Time
  • Show
Clear All
new posts

  • Tray Application - Window Focus/ZOrder Oddities...

    I have a tray application that under certain conditions, I do 1 of 2 things:

    1) Load another dialog for user input, then continue on to shell to an external application
    2) No user input needed, shell to external application

    If the user manually clicks the tray icon and makes a selection via a popup menu, everything works fine - any dialogs I want seem to come to the front when I want, focus works like I want, etc.

    If I have a timer running, that based on certain conditions will try to automatically load the extra dialogs, or shell to the external applications - all dialogs seem to come up behind any other open dialogs most of the time, no matter what I try.

    Anyone have any suggestions for bringing a dialog to the front, and make all focus issues work correctly?

    I tried WS_EX_TOPMOST, but that brought it to the front, and the window that was on top before still retains focus.

    I will try to come up with a 'shell' of an app that fails, at the moment, the app can't be posted and tested as is...
    Adam Drake

  • #2

    see what Semen did...


    • #3
      Thank you - that's perfect.
      Adam Drake


      • #4
        SetForegroundWindow gained some restrictions in some versions of windows, not to mention other issues. Tray icons are tricky, especially if you have a menu because of the two things you have to call. This may not be 100% of what you are having problems with since this code fires on right clicking the tray icon, but may give ideas for both.

        Usually my code looks something like:

        SetForegroundWindow(HWND_APP) ' Needed or Menu Hangs
        SetFocus(0) 'Block Keystrokes to Window when menu open
        ' Select whether to have Restore Enabled
        mi.fMask= %MIIM_STATE
        If IsWindowVisible(HWND_APP)= 0 Then mi.fState= %MF_ENABLED Else mi.fState= %MFS_GRAYED
        SetMenuItemInfo(trayMenu, %WM_TRAYRESTORE, %FALSE, mi)
        TrackPopupMenu(trayMenu, %TPM_RIGHTALIGN Or %TPM_LEFTBUTTON Or %TPM_RIGHTBUTTON, Pt.x, Pt.y, 0, HWND_APP, ByVal 0)
        PostMessage(HWND_APP, %WM_NULL, 0, 0) ' Needed to "prime" message loop or menu hangs
        Do to the Foreground Window weirdness, in one app I tried something like this that worked pretty well:

        prevWindow= GetNextWindow(HWND_APP, %GW_HWNDNEXT)
        SetWindowPos(HWND_APP, prevWindow, 0, 0, 0, 0, %SWP_NOSIZE Or %SWP_NOMOVE Or %SWP_NOACTIVATE)
        SetFocus(0) 'Block Keystrokes to Window
        TrackPopupMenu(trayMenu, %TPM_RIGHTALIGN Or %TPM_LEFTBUTTON Or %TPM_RIGHTBUTTON, Pt.x, Pt.y, 0, HWND_APP, ByVal 0)
        PostMessage(HWND_APP, %WM_NULL, 0, 0)
        I have yet to get a menu that functions like the IM apps like Yahoo IM where they seem to operate their menu off the tray icon without the need for the SetForeGroundWindow, etc and doesn't is custom drawn too. It would be nice if they'd post their secrets.
        Mobile Solutions
        Sys Analyst and Development