forked from Answeror/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeticon.py
63 lines (51 loc) · 1.52 KB
/
geticon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from qt.QtGui import (
QPixmap,
QIcon
)
from win32com.shell import shell, shellcon
import win32con
import win32gui
import pywintypes
import os
def extract_file_icon(path):
largeicons, smallicons = win32gui.ExtractIconEx(path, 0)
fIcon, xHotspot, yHotspot, hbmMask, hbmColor = win32gui.GetIconInfo(largeicons[0])
pixmap = QPixmap.fromWinHBITMAP(hbmColor, QPixmap.PremultipliedAlpha)
icon = QIcon(pixmap)
hbmColor.close()
hbmMask.close()
for hIcon in largeicons + smallicons:
win32gui.DestroyIcon(hIcon)
return icon
def splitparts(path):
rv = []
while path:
path, tail = os.path.split(path)
if not tail:
break
rv.append(tail)
return rv[::-1]
def translate_path(path):
if path.lower().startswith('\\systemroot\\'):
parts = splitparts(path)
parts[0] = os.environ['SystemRoot']
path = os.path.join(*parts)
return path
def get_file_icon_by_shell(path):
path = translate_path(path)
flags, (hIcon, iIcon, dwAttributes, displayName, typeName) = shell.SHGetFileInfo(
path,
0,
shellcon.SHGFI_ICON|shellcon.SHGFI_SMALLICON|shellcon.SHGFI_SYSICONINDEX|shellcon.SHGFI_TYPENAME|shellcon.SHGFI_USEFILEATTRIBUTES,
)
if hIcon == win32con.NULL:
return None
pixmap = QPixmap.fromWinHICON(hIcon)
win32gui.DestroyIcon(hIcon)
icon = QIcon(pixmap)
return icon
def get_file_icon(path):
try:
return get_file_icon_by_shell(path)
except pywintypes.error:
pass