[Win32] Retrieve icon for file type?

Mike Fletcher mfletch at tpresence.com
Wed Apr 26 17:09:45 EDT 2000


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' )

-----Original Message-----
From: Thomas Heller [mailto:thomas.heller at ion-tof.com]
Sent: Wednesday, April 26, 2000 12:22 PM
To: Mike Fletcher; Python Listserv (E-mail)
Subject: Re: [Win32] Retrieve icon for file type?


> 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


-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list