| 1 |
# A sample context menu handler. |
| 2 |
# Adds a 'Hello from Python' menu entry to .py files. When clicked, a |
| 3 |
# simple message box is displayed. |
| 4 |
# |
| 5 |
# To demostrate: |
| 6 |
# * Execute this script to register the context menu. |
| 7 |
# * Open Windows Explorer, and browse to a directory with a .py file. |
| 8 |
# * Right-Click on a .py file - locate and click on 'Hello from Python' on |
| 9 |
# the context menu. |
| 10 |
|
| 11 |
import pythoncom |
| 12 |
from win32com.shell import shell, shellcon |
| 13 |
import win32gui |
| 14 |
import win32con |
| 15 |
|
| 16 |
IContextMenu_Methods = ["QueryContextMenu", "InvokeCommand", "GetCommandString"] |
| 17 |
IShellExtInit_Methods = ["Initialize"] |
| 18 |
|
| 19 |
class ShellExtension: |
| 20 |
_reg_progid_ = "Python.ShellExtension.ContextMenu" |
| 21 |
_reg_name_ = "MessClient" |
| 22 |
_reg_desc_ = "Mess Shell Extension" |
| 23 |
_reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}" |
| 24 |
_com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu] |
| 25 |
_public_methods_ = IContextMenu_Methods + IShellExtInit_Methods |
| 26 |
|
| 27 |
def Initialize(self, folder, dataobj, hkey): |
| 28 |
print "Init", folder, dataobj, hkey |
| 29 |
self.dataobj = dataobj |
| 30 |
|
| 31 |
def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags): |
| 32 |
print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags |
| 33 |
# Query the items clicked on |
| 34 |
format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL |
| 35 |
sm = self.dataobj.GetData(format_etc) |
| 36 |
num_files = shell.DragQueryFile(sm.data_handle, -1) |
| 37 |
if num_files>1: |
| 38 |
msg = "&Hello from Python (with %d files selected)" % num_files |
| 39 |
else: |
| 40 |
fname = shell.DragQueryFile(sm.data_handle, 0) |
| 41 |
msg = "&Hello from Python (with '%s' selected)" % fname |
| 42 |
idCmd = idCmdFirst |
| 43 |
items = [] |
| 44 |
if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0 |
| 45 |
print "CMF_NORMAL..." |
| 46 |
items.append(msg) |
| 47 |
elif uFlags & shellcon.CMF_VERBSONLY: |
| 48 |
print "CMF_VERBSONLY..." |
| 49 |
items.append(msg + " - shortcut") |
| 50 |
elif uFlags & shellcon.CMF_EXPLORE: |
| 51 |
print "CMF_EXPLORE..." |
| 52 |
items.append(msg + " - normal file, right-click in Explorer") |
| 53 |
elif uFlags & CMF_DEFAULTONLY: |
| 54 |
print "CMF_DEFAULTONLY...\r\n" |
| 55 |
else: |
| 56 |
print "** unknown flags", uFlags |
| 57 |
win32gui.InsertMenu(hMenu, indexMenu, |
| 58 |
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
| 59 |
0, None) |
| 60 |
indexMenu += 1 |
| 61 |
for item in items: |
| 62 |
win32gui.InsertMenu(hMenu, indexMenu, |
| 63 |
win32con.MF_STRING|win32con.MF_BYPOSITION, |
| 64 |
idCmd, item) |
| 65 |
indexMenu += 1 |
| 66 |
idCmd += 1 |
| 67 |
|
| 68 |
win32gui.InsertMenu(hMenu, indexMenu, |
| 69 |
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
| 70 |
0, None) |
| 71 |
indexMenu += 1 |
| 72 |
return idCmd-idCmdFirst # Must return number of menu items we added. |
| 73 |
|
| 74 |
def InvokeCommand(self, ci): |
| 75 |
mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci |
| 76 |
win32gui.MessageBox(hwnd, "Hello", "Wow", win32con.MB_OK) |
| 77 |
|
| 78 |
def GetCommandString(self, cmd, typ): |
| 79 |
return "Hello from Python!!" |
| 80 |
|
| 81 |
|
| 82 |
def DllRegisterServer(): |
| 83 |
import _winreg |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
| 88 |
|
| 89 |
# for folders |
| 90 |
folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex") |
| 91 |
folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers") |
| 92 |
folder_subkey2 = _winreg.CreateKey(folder_subkey, ShellExtension._reg_name_) |
| 93 |
_winreg.SetValueEx(folder_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
| 94 |
|
| 95 |
# for files |
| 96 |
file_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "*\\shellex") |
| 97 |
file_subkey = _winreg.CreateKey(file_key, "ContextMenuHandlers") |
| 98 |
file_subkey2 = _winreg.CreateKey(file_subkey, ShellExtension._reg_name_) |
| 99 |
_winreg.SetValueEx(file_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
| 100 |
|
| 101 |
print ShellExtension._reg_desc_, "registration complete." |
| 102 |
|
| 103 |
|
| 104 |
def DllUnregisterServer(): |
| 105 |
import _winreg |
| 106 |
|
| 107 |
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
| 108 |
|
| 109 |
try: |
| 110 |
# for folders |
| 111 |
folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
| 112 |
"Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
| 113 |
# for files |
| 114 |
file_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
| 115 |
"*\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
| 116 |
except WindowsError, details: |
| 117 |
import errno |
| 118 |
if details.errno != errno.ENOENT: |
| 119 |
raise |
| 120 |
print ShellExtension._reg_desc_, "unregistration complete." |
| 121 |
|
| 122 |
if __name__=='__main__': |
| 123 |
from win32com.server import register |
| 124 |
register.UseCommandLine(ShellExtension, |
| 125 |
finalize_register = DllRegisterServer, |
| 126 |
finalize_unregister = DllUnregisterServer) |