Is there an easy way to get at the Shell icons from Python?
def query ( extension):
''' get icon for the given extension '''
try:
path = win32api.RegQueryValue(
HKEY_CLASSES_ROOT,
extension,
)
filedata = win32api.RegQueryValue(
HKEY_CLASSES_ROOT,
'%s\\DefaultIcon'%(path),
)
# how do I load the icon data?
# ExtractIcon?
except win32api.error:
return None
__________________________________
Mike C. Fletcher
Designer, VR Plumber
http://members.home.com/mcfletch
You might want to look at
MEInc/Dist/icon.py in Gordon's win32 installer package.
This file uses LoadResource to extract icons from executable files.
(Note however that you get the raw data, and not an HICON).
Thomas
Incidentally, the IShellFolder/IShellIcon stuff looks like the "right" way
to do this, I just don't see any way to touch it from Python (apparently you
need the functions "SHGetDesktopFolder" etceteras in shell32.dll to get
anywhere with it.
Here's what I've got so far (based on Thomas' work). Suggestions
appreciated...
Mike
def getIcon(srcpath):
import os.path, string
index = None
try:
srcpath, index = map (string.strip, string.split (srcpath,
','))
index = int (index)
except:
pass
print "PATH, INDEX", srcpath, index
srcext = os.path.splitext (srcpath)[1]
if string.lower (srcext) == '.ico':
return None # CopyIcons_FromIco (dstpath, srcpath)
import win32api, win32ui #, win32con
## hdst = win32api.BeginUpdateResource (dstpath, 0)
hsrc = win32api.LoadLibraryEx (srcpath, 0, LOAD_LIBRARY_AS_DATAFILE)
if index is None:
grpname = win32api.EnumResourceNames (hsrc,
RT_GROUP_ICON)[0]
elif index >= 0:
grpname = win32api.EnumResourceNames (hsrc,
RT_GROUP_ICON)[index]
else:
grpname = -index
data = win32api.LoadResource (hsrc, RT_GROUP_ICON, grpname)
for iconname in win32api.EnumResourceNames (hsrc, RT_ICON):
icon = win32ui.GetApp().LoadIcon( iconname )
bitmap = getBitmapFromIcon( icon )
win32api.FreeLibrary (hsrc)
return data
def getBitmapFromIcon( iconresource ):
import win32ui
try:
dc = win32ui.GetActiveWindow().GetDC()
except win32ui.error:
dc = win32ui.CreateWnd().GetDC()
## dc = win32ui.CreateDC()
# create in-memory dc with display params...
mdc = dc.CreateCompatibleDC( None )
bitmap = win32ui.CreateBitmap() bitmap.CreateCompatibleBitmap( mdc,
32, 32 )
mdc.SelectObject( bitmap )
## mdc.DrawIcon( (0,32), iconresource ) # fails :(
# just for testing...
bitmap.SaveBitmapFile( mdc, "test4.bmp" )
return bitmap
if __name__ == "__main__":
import sys
getIcon( 'd:\\bin\\lang\\python\\python.exe' )
Thomas
--
http://www.python.org/mailman/listinfo/python-list
> Incidentally, the IShellFolder/IShellIcon stuff looks like the "right"
way
> to do this, I just don't see any way to touch it from Python (apparently
you
> need the functions "SHGetDesktopFolder" etceteras in shell32.dll to get
> anywhere with it.
These are in win32com.shell.shell.
Note that ExtractIcon/DestroyIcon will be in the next version of
win32gui...
Mark.