| 1 |
# Derived from Python24\Lib\site-packages\win32comext\shell\demos\servers\context_menu.py |
| 2 |
# >> a bit more complex << |
| 3 |
|
| 4 |
# Acknowledgements to Mark Hammond for his "Python for Windows extensions" |
| 5 |
# http://python.net/crew/mhammond/ |
| 6 |
|
| 7 |
# ... and Michael Dunn for his |
| 8 |
# "The Complete Idiot's Guide to Writing Shell Extensions"-series at "The Code Project" |
| 9 |
# http://www.codeproject.com/shell/shellextguideindex.asp |
| 10 |
|
| 11 |
# You're great guys! |
| 12 |
|
| 13 |
|
| 14 |
import pythoncom |
| 15 |
from win32com.shell import shell, shellcon |
| 16 |
import win32gui, win32gui_struct |
| 17 |
import win32con |
| 18 |
import win32api, winerror |
| 19 |
|
| 20 |
IContextMenu_Methods = ["QueryContextMenu", "InvokeCommand", "GetCommandString"] |
| 21 |
IShellExtInit_Methods = ["Initialize"] |
| 22 |
|
| 23 |
class ShellExtension: |
| 24 |
_reg_progid_ = "Mess.ShellExtension.ContextMenu" |
| 25 |
_reg_name_ = "MessClient" |
| 26 |
_reg_desc_ = "Mess Shell Extension" |
| 27 |
_reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}" |
| 28 |
_com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu] |
| 29 |
_public_methods_ = IContextMenu_Methods + IShellExtInit_Methods |
| 30 |
|
| 31 |
def Initialize(self, pidlFolder, dataobj, hkey): |
| 32 |
print "Init", pidlFolder, dataobj, hkey |
| 33 |
self.dataobj = dataobj |
| 34 |
|
| 35 |
def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags): |
| 36 |
|
| 37 |
# debug |
| 38 |
print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags |
| 39 |
|
| 40 |
# clicking on a directory background leaves us with dataobj == None |
| 41 |
if self.dataobj: |
| 42 |
|
| 43 |
# query the items clicked on |
| 44 |
format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL |
| 45 |
sm = self.dataobj.GetData(format_etc) |
| 46 |
|
| 47 |
# how many items are selected |
| 48 |
num_files = shell.DragQueryFile(sm.data_handle, -1) |
| 49 |
|
| 50 |
# grab all items |
| 51 |
self.files_selected = [] |
| 52 |
for i in range(0, num_files): |
| 53 |
fname = shell.DragQueryFile(sm.data_handle, i) |
| 54 |
if fname == 0: continue |
| 55 |
self.files_selected.append(fname) |
| 56 |
|
| 57 |
|
| 58 |
# calculate items to be added to context menu |
| 59 |
items = [] |
| 60 |
|
| 61 |
# CMF_NORMAL: e.g. files on desktop |
| 62 |
# CMF_VERBSONLY: e.g. shortcuts |
| 63 |
# CMF_EXPLORE: regular files/folders via explorer |
| 64 |
|
| 65 |
# Check == here, since CMF_NORMAL=0 |
| 66 |
if ((uFlags & 0x000F) == shellcon.CMF_NORMAL) or (uFlags & shellcon.CMF_VERBSONLY) or (uFlags & shellcon.CMF_EXPLORE): |
| 67 |
if self.dataobj: |
| 68 |
items.append("Add to A&rchive") |
| 69 |
items.append("Named Shortcut &1") |
| 70 |
items.append("Named Shortcut &2") |
| 71 |
elif uFlags & CMF_DEFAULTONLY: |
| 72 |
print "CMF_DEFAULTONLY...\r\n" |
| 73 |
items.append("hello world - 1!") |
| 74 |
else: |
| 75 |
print "** unknown flags", uFlags |
| 76 |
items.append("hello world - 2!") |
| 77 |
|
| 78 |
|
| 79 |
# build context menu(s) |
| 80 |
idCmd = idCmdFirst |
| 81 |
|
| 82 |
# add seperator line |
| 83 |
win32gui.InsertMenu(hMenu, indexMenu, |
| 84 |
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
| 85 |
idCmd, None) |
| 86 |
indexMenu += 1 |
| 87 |
idCmd += 1 |
| 88 |
|
| 89 |
# create main menu entries |
| 90 |
for item in items: |
| 91 |
win32gui.InsertMenu(hMenu, indexMenu, |
| 92 |
win32con.MF_STRING|win32con.MF_BYPOSITION, |
| 93 |
idCmd, item) |
| 94 |
indexMenu += 1 |
| 95 |
idCmd += 1 |
| 96 |
|
| 97 |
# create submenu entries |
| 98 |
hSubmenu = win32gui.CreatePopupMenu() |
| 99 |
win32gui.InsertMenu(hSubmenu, 0, |
| 100 |
win32con.MF_BYPOSITION, |
| 101 |
idCmd, "&Settings...") |
| 102 |
idCmd += 1 |
| 103 |
|
| 104 |
# insert the submenu into a new container menu entry |
| 105 |
item, extras = win32gui_struct.PackMENUITEMINFO( |
| 106 |
wID = idCmd, |
| 107 |
hSubMenu = hSubmenu, |
| 108 |
text = "&Mess" |
| 109 |
) |
| 110 |
win32gui.InsertMenuItem(hMenu, indexMenu, 1, item) |
| 111 |
indexMenu += 1 |
| 112 |
idCmd += 1 |
| 113 |
|
| 114 |
# add seperator line |
| 115 |
win32gui.InsertMenu(hMenu, indexMenu, |
| 116 |
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
| 117 |
idCmd, None) |
| 118 |
indexMenu += 1 |
| 119 |
idCmd += 1 |
| 120 |
|
| 121 |
return idCmd-idCmdFirst # Must return number of menu items we added. |
| 122 |
|
| 123 |
def GetCommandString(self, cmdId, typ): |
| 124 |
#if cmdId == 0: |
| 125 |
# return "Hello from Python!!" |
| 126 |
#else: |
| 127 |
# return "abc" |
| 128 |
return self._reg_name_ + "_" + cmdId; |
| 129 |
|
| 130 |
def InvokeCommand(self, ci): |
| 131 |
mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci |
| 132 |
|
| 133 |
# If lpVerb really points to a string, ignore this function call and bail out. |
| 134 |
if win32api.HIWORD( verb ) != 0: |
| 135 |
return winerror.E_INVALIDARG |
| 136 |
|
| 137 |
function_id = win32api.LOWORD(verb) |
| 138 |
# TODO: establish mapping with building the context menus in "QueryContextMenu" |
| 139 |
if function_id == 1: |
| 140 |
method = "Add to archive" |
| 141 |
elif function_id == 2: |
| 142 |
method = "Shortcut 1" |
| 143 |
elif function_id == 3: |
| 144 |
method = "Shortcut 2" |
| 145 |
else: |
| 146 |
method = "Unknown" |
| 147 |
|
| 148 |
# do action(s) |
| 149 |
if hasattr(self, 'files_selected'): |
| 150 |
win32gui.MessageBox(hwnd, "\n".join(self.files_selected), method, win32con.MB_OK) |
| 151 |
else: |
| 152 |
win32gui.MessageBox(hwnd, "No selection!", method, win32con.MB_OK) |
| 153 |
|
| 154 |
|
| 155 |
def DllRegisterServer(): |
| 156 |
import _winreg |
| 157 |
|
| 158 |
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
| 159 |
|
| 160 |
# for directory background |
| 161 |
background_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Directory\\Background\\shellex") |
| 162 |
background_subkey = _winreg.CreateKey(background_key, "ContextMenuHandlers") |
| 163 |
background_subkey2 = _winreg.CreateKey(background_subkey, ShellExtension._reg_name_) |
| 164 |
_winreg.SetValueEx(background_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
| 165 |
|
| 166 |
# for folders |
| 167 |
folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex") |
| 168 |
folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers") |
| 169 |
folder_subkey2 = _winreg.CreateKey(folder_subkey, ShellExtension._reg_name_) |
| 170 |
_winreg.SetValueEx(folder_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
| 171 |
|
| 172 |
# for files |
| 173 |
file_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "*\\shellex") |
| 174 |
file_subkey = _winreg.CreateKey(file_key, "ContextMenuHandlers") |
| 175 |
file_subkey2 = _winreg.CreateKey(file_subkey, ShellExtension._reg_name_) |
| 176 |
_winreg.SetValueEx(file_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
| 177 |
|
| 178 |
print ShellExtension._reg_desc_, "registration complete." |
| 179 |
|
| 180 |
|
| 181 |
def DllUnregisterServer(): |
| 182 |
import _winreg |
| 183 |
|
| 184 |
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
| 185 |
|
| 186 |
try: |
| 187 |
# for directory background |
| 188 |
background_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
| 189 |
"Directory\\Background\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
| 190 |
# for folders |
| 191 |
folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
| 192 |
"Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
| 193 |
# for files |
| 194 |
file_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
| 195 |
"*\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
| 196 |
except WindowsError, details: |
| 197 |
import errno |
| 198 |
if details.errno != errno.ENOENT: |
| 199 |
raise |
| 200 |
print ShellExtension._reg_desc_, "unregistration complete." |
| 201 |
|
| 202 |
if __name__=='__main__': |
| 203 |
from win32com.server import register |
| 204 |
register.UseCommandLine(ShellExtension, |
| 205 |
finalize_register = DllRegisterServer, |
| 206 |
finalize_unregister = DllUnregisterServer) |