[python-win32] Context menu troubles
Chris Ness
karbonforms at gmail.com
Fri Mar 2 19:03:14 CET 2012
disclaimer: I have only the faintest idea what I'm doing!
I'm trying to get a windows explorer context menu in my app. I can get
the menu to show, but it does not execute any commands (e.g. 'copy')
my code is based on
http://bcbjournal.org/articles/vol4/0006/Using_the_shell_context_menu.htm and
partly on http://netez.com/2xExplorer/shellFAQ/bas_context.html
I'm new to both win32 and pywin32. Any help or advice will be readily
accepted!
Cheers!
Chris.
DesktopFolder = shell.SHGetDesktopFolder()
if not DesktopFolder:
raise Exception("Failed to get Desktop folder.")
# Get a pidl for the folder the file
# is located in.
FilePath = 'J:\\'
FileName = 'tree.xml'
Eaten, ParentPidl, attr = DesktopFolder.ParseDisplayName(Handle, None, FilePath)
# Get an IShellFolder for the folder
# the file is located in.
ParentFolder = DesktopFolder.BindToObject(ParentPidl, None, shell.IID_IShellFolder)
CM_plus = None
# Get a pidl for the file itself.
Eaten, Pidl, attr = ParentFolder.ParseDisplayName(Handle, None, FileName)
# Get the IContextMenu for the file.
i, CM = ParentFolder.GetUIObjectOf(Handle, [Pidl], shell.IID_IContextMenu, 0)
#else:
# Get the IContextMenu for the folder. ????????
#i, CM = ParentFolder.GetUIObjectOf(Handle, [ParentPidl], shell.IID_IContextMenu, 0)
if not CM:
raise Exception("Unable to get context menu interface.")
else:
# try to obtain a higher level pointer, first 3 then 2
try:
CM_plus = CM.QueryInterface(shell.IID_IContextMenu3, None)
pcmType = 3
except Exception:
try:
CM_plus = CM.QueryInterface(shell.IID_IContextMenu2, None)
pcmType = 2
except Exception:
pass
if CM_plus:
CM.Release() # free initial "v1.0" interface
CM = CM_plus
else: # no higher version supported
pcmType = 1
hMenu = win32gui.CreatePopupMenu()
Flags = CMF_EXPLORE
# Optionally the shell will show the extended
# context menu on some operating systems when
# the shift key is held down at the time the
# context menu is invoked. The following is
# commented out but you can uncommnent this
# line to show the extended context menu.
# Flags |= 0x00000080;
CM.QueryContextMenu(hMenu, 0, 1, 0x7FFF, Flags)
# Show the menu.
x, y = win32gui.GetCursorPos()
flags = ( win32gui.TPM_LEFTALIGN
| win32gui.TPM_LEFTBUTTON
| win32gui.TPM_RIGHTBUTTON
#| win32gui.TPM_RETURNCMD,
)
hr = win32gui.TrackPopupMenu(hMenu, flags, x, y, 0, Handle, None)
if hr != 1:
e = win32api.GetLastError()
s = win32api.FormatMessage(e)
More information about the python-win32
mailing list