[python-win32] Context menu troubles

Chris Ness karbonforms at gmail.com
Fri Mar 2 20:03:27 CET 2012


Thanks for reply Tim.

I had decided not to use the command ID technique until I got things 
working, so TPM_RETURNCMD was commented out when I defines flags. From 
my understanding of MSDN and pywin32, this should make commands 
immediately execute and cause TrackPopupMenu to return an HRESULT?

Also forgot to mention, using python3.2 32bit, pywon32 build 217 on 
windows 7

I've simplified/condensed it a bit, stripped comments and changed 
TrackPopupMenu return variable to something more appropriate.

Chris.

     DesktopFolder = shell.SHGetDesktopFolder()
     if not DesktopFolder:
         raise Exception("Failed to get Desktop folder.")
     FilePath = 'J:\\'
     FileName = 'tree.xml'
     Eaten, ParentPidl, attr = DesktopFolder.ParseDisplayName(Handle, None, FilePath)
     ParentFolder = DesktopFolder.BindToObject(ParentPidl, None, shell.IID_IShellFolder)
     CM_plus = None
     Eaten, Pidl, attr = ParentFolder.ParseDisplayName(Handle, None, FileName)
     i, CM = ParentFolder.GetUIObjectOf(Handle, [Pidl], shell.IID_IContextMenu, 0)
     hMenu = win32gui.CreatePopupMenu()
     Flags = CMF_EXPLORE
     CM.QueryContextMenu(hMenu, 0, 1, 0x7FFF, Flags)
     x, y =  win32gui.GetCursorPos()
     flags = win32gui.TPM_LEFTALIGN
     hr = win32gui.TrackPopupMenu(hMenu, flags, x, y, 0, Handle, None)
     if hr != 1:
         e = win32api.GetLastError()
         s = win32api.FormatMessage(e) #<-------- "command completed successfully"


On 02/03/2012 18:42, Tim Roberts wrote:
> Chris Ness wrote:
>> 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')
> Right, because you haven't told it to.  You implemented the stuff form
> the articles pretty closely until the call to TrackPopupMenu, then you
> went awry.  TrackPopupMenu does not actually return a boolean.  It
> returns 0 if the call fails, but if the call succeeds, it returns the
> command ID of the menu item that was selected, and you have to tell the
> shell to go invoke that command.
>
> So, if it returns a value other than 0, you need to create a
> CMINVOKECOMMANDINFO structure, fill it in, and pass it to
> CM.InvokeCommand.  That will trigger the real action.
>



More information about the python-win32 mailing list