Note: Beware of two annoying behaviors when you use a popup menu with a tray icon:
After calling SetForegroundWindow, you will find that the first time the popup menu is displayed, it works ok but on the subsequent times, the popup menu will show up and close immediately. This behavior is "intentional", to quote from MSDN. The task switch to the program that is the owner of the tray icon in the near future is necessary. You can force this task switch by posting any message to the window of the program. Just use PostMessage, not SendMessage!

this is some words in iczelion's toturial about tray icon.he say after TrackPopupMenu,we should use  invoke PostMessage,hWnd,WM_NULL,0,0 ,but without this ,it also work well.i want to know why?
his code like this:

            if lParam==WM_RBUTTONDOWN
                invoke GetCursorPos,addr pt
                invoke SetForegroundWindow,hWnd
                invoke TrackPopupMenu,hPopupMenu,TPM_RIGHTALIGN,pt.x,pt.y,NULL,hWnd,NULL
                ;invoke PostMessage,hWnd,WM_NULL,0,0
            .elseif lParam==WM_LBUTTONDBLCLK
                invoke SendMessage,hWnd,WM_COMMAND,IDM_RESTORE,0

Posted on 2011-08-06 00:45:14 by geegle
I don't know :shrug: but according to MSDN that's the way to do it. Maybe it's some kind of odd behavior for an older version of Windows?
Posted on 2011-08-06 13:06:59 by JimmyClif
I dug out some of my old code and a message to myself stated:

// There's a "bug" (Microsoft calls it a feature) in Windows 95 that
// requires calling SetForegroundWindow. To find out more, search
// for Q135788 in MSDN.

So yes, it's old behavior although I can't say if it's ever been fixed yet...
Posted on 2011-08-06 19:25:30 by p1ranha
Posted on 2011-08-07 09:19:15 by ti_mo_n