Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Win32] Retrieve icon for file type?

248 views
Skip to first unread message

Mike Fletcher

unread,
Apr 26, 2000, 3:00:00 AM4/26/00
to Python Listserv (E-mail)
I'm attempting to use the Windows registry to retrieve (and convert to a
PNG/JPEG) the Windows icons for given files (so that a Windows-based server
can display file-type-specific icons). I do not seem to be able to find the
function shellapi.ExtractIcon (
http://msdn.microsoft.com/library/psdk/winui/icons_7h2m.htm ) in the Win32
extensions.

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


Thomas Heller

unread,
Apr 26, 2000, 3:00:00 AM4/26/00
to Mike Fletcher, Python Listserv (E-mail)
> I'm attempting to use the Windows registry to retrieve (and convert to a
> PNG/JPEG) the Windows icons for given files (so that a Windows-based
server
> can display file-type-specific icons). I do not seem to be able to find
the
> function shellapi.ExtractIcon (
> http://msdn.microsoft.com/library/psdk/winui/icons_7h2m.htm ) in the Win32
> extensions.

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

Mike Fletcher

unread,
Apr 26, 2000, 3:00:00 AM4/26/00
to Thomas Heller, Python Listserv (E-mail)
Well, I think I'm almost there, but I can't figure out how to create a
colour device context, and drawing a colour icon into the black-and-white
context just gives me a failed message. Anyone know any tricks.

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


Mark Hammond

unread,
Apr 27, 2000, 3:00:00 AM4/27/00
to
"Mike Fletcher" <mfl...@tpresence.com> wrote in message
news:CC59868A0A76D311B1F50090277B321C1F32B6@VRTMAIL...

> 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.


0 new messages