从python调用Windows资源管理器上下文菜单功能

时间:2019-07-10 08:05:51

标签: python

是否可以从python调用Windows资源管理器上下文菜单操作?例如,当您右键单击Windows资源管理器中的文件时,您会看到编辑文件的选项,并打印e.t.c.我写了一个我认为可以做到的脚本,但是效果不如我所愿。还有可能这样做,但浏览器窗口不可见,然后再将其关闭吗?这是我当前的代码:

from win32com.shell import shell, shellcon
import win32gui
import win32con

desktop_folder = shell.SHGetDesktopFolder()

file_path = 'C:\\Projects\\Python'
file_name = 'tutankhamun.jpg'
full_path = 'C:\\Projects\\Python\\tutankhamun.jpg'

hwnd = win32gui.GetForegroundWindow() 

print (hwnd)

eaten, parent_pidl, attr = desktop_folder.ParseDisplayName(hwnd, None, file_path)
parent_folder = desktop_folder.BindToObject(parent_pidl, None, shell.IID_IShellFolder)


eaten, pidl, attr = parent_folder.ParseDisplayName(hwnd, None, file_name)
i, CM = parent_folder.GetUIObjectOf(hwnd, [pidl], shell.IID_IContextMenu, 0)

if not CM:
    raise Exception('Unable to get context menu')
else:
    CI = (0, #mask
            hwnd, #hwnd
            'Edit with Photos', #verb
            '', #Parameters
            file_path, #Directory
            win32con.SW_HIDE, #Show (SW_HIDE)
            0, #Hotkey
            None #Icon
    )
    hMenu = win32gui.CreatePopupMenu() 
    #object_methods = [method_name for method_name in dir(hMenu) if callable(getattr(hMenu, method_name))]
    #print (object_methods)
    MIN_SHELL_ID = 1 
    MAX_SHELL_ID = 30000 
    CM.QueryContextMenu(hMenu, 0, MIN_SHELL_ID, MAX_SHELL_ID, shellcon.CMF_NORMAL) 
    CM.InvokeCommand(CI)

谢谢

0 个答案:

没有答案