Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 2.34 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to change file/folder icon programmatically?
  2. import os
  3. import ctypes
  4. from ctypes import POINTER, Structure, c_wchar, c_int, sizeof, byref
  5. from ctypes.wintypes import BYTE, WORD, DWORD, LPWSTR, LPSTR
  6. import win32api    
  7.  
  8. HICON = c_int
  9. LPTSTR = LPWSTR
  10. TCHAR = c_wchar
  11. MAX_PATH = 260
  12. FCSM_ICONFILE = 0x00000010
  13. FCS_FORCEWRITE = 0x00000002
  14. SHGFI_ICONLOCATION = 0x000001000    
  15.  
  16. class GUID(Structure):
  17.     _fields_ = [
  18.         ('Data1', DWORD),
  19.         ('Data2', WORD),
  20.         ('Data3', WORD),
  21.         ('Data4', BYTE * 8)]
  22.  
  23. class SHFOLDERCUSTOMSETTINGS(Structure):
  24.     _fields_ = [
  25.         ('dwSize', DWORD),
  26.         ('dwMask', DWORD),
  27.         ('pvid', POINTER(GUID)),
  28.         ('pszWebViewTemplate', LPTSTR),
  29.         ('cchWebViewTemplate', DWORD),
  30.         ('pszWebViewTemplateVersion', LPTSTR),
  31.         ('pszInfoTip', LPTSTR),
  32.         ('cchInfoTip', DWORD),
  33.         ('pclsid', POINTER(GUID)),
  34.         ('dwFlags', DWORD),
  35.         ('pszIconFile', LPTSTR),
  36.         ('cchIconFile', DWORD),
  37.         ('iIconIndex', c_int),
  38.         ('pszLogo', LPTSTR),
  39.         ('cchLogo', DWORD)]
  40.  
  41. class SHFILEINFO(Structure):
  42.     _fields_ = [
  43.         ('hIcon', HICON),
  44.         ('iIcon', c_int),
  45.         ('dwAttributes', DWORD),
  46.         ('szDisplayName', TCHAR * MAX_PATH),
  47.         ('szTypeName', TCHAR * 80)]    
  48.  
  49. def seticon(folderpath, iconpath, iconindex):
  50.     """Set folder icon.
  51.  
  52.     >>> seticon(".", "C:\Windows\system32\SHELL32.dll", 10)
  53.  
  54.     """
  55.     shell32 = ctypes.windll.shell32
  56.  
  57.     folderpath = unicode(os.path.abspath(folderpath), 'mbcs')
  58.     iconpath = unicode(os.path.abspath(iconpath), 'mbcs')
  59.  
  60.     fcs = SHFOLDERCUSTOMSETTINGS()
  61.     fcs.dwSize = sizeof(fcs)
  62.     fcs.dwMask = FCSM_ICONFILE
  63.     fcs.pszIconFile = iconpath
  64.     fcs.cchIconFile = 0
  65.     fcs.iIconIndex = iconindex
  66.  
  67.     hr = shell32.SHGetSetFolderCustomSettings(byref(fcs), folderpath,
  68.                                               FCS_FORCEWRITE)
  69.     if hr:
  70.         raise WindowsError(win32api.FormatMessage(hr))
  71.  
  72.     sfi = SHFILEINFO()
  73.     hr = shell32.SHGetFileInfoW(folderpath, 0, byref(sfi), sizeof(sfi),
  74.                                 SHGFI_ICONLOCATION)
  75.     if hr == 0:
  76.         raise WindowsError(win32api.FormatMessage(hr))
  77.  
  78.     index = shell32.Shell_GetCachedImageIndexW(sfi.szDisplayName, sfi.iIcon, 0)
  79.     if index == -1:
  80.         raise WindowsError()
  81.  
  82.     shell32.SHUpdateImageW(sfi.szDisplayName, sfi.iIcon, 0, index)