[python-win32] win32gui_taskbar.py popup menu doesn't go away
Sean Treadway
seant@superchannel.org
Thu, 24 Jan 2002 18:33:01 +0100
I was searching through the archives of the pythonwin list to look for a
solution to the following problem: When clicking off the popup menu created
by the demo taskbar code, the menu would not disappear.
http://mail.python.org/pipermail/python-list/1999-June/005566.html
> >[1] well, almost - further experimentation shows that it only closes
> >the menu if I click within the Pythonwin window, rather than anywhere
> >on the screen.
>Well, maybe you should set the owner to the desktop?
This didn't work. So digging around the MSDN site I found:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus
_0hdi.asp
Which states that the menu will be destroyed when the owning window changes
focus (the reason why clicking on pythonwin seemed to do the trick). So
before calling TrackPopupMenu(), one must first call SetForegroundWindow().
The sample C code below:
SetForegroundWindow(hDlg);
// Display the menu
TrackPopupMenu( hSubMenu,
TPM_RIGHTBUTTON,
pt.x,
pt.y,
0,
hDlg,
NULL);
PostMessage(hDlg, WM_NULL, 0, 0);
The PostMessage below is to switch the process to the owning window so the
next time the menu is displayed it will not dissapear because it is already
focused. However, I could not reproduce the behavior of having the menu
appear then disappear as the win32 sdk implies.
I suggest that the win32gui_taskbar.py demo code is updated with the
following 2 lines to save "us that didn't know better" a lot of grief:
+ SetForegroundWindow(self.hwnd)
TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0,
self.hwnd, None)
+ PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
Thanks,
Sean