Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6d8dfc5

Browse files
committed
feat(icon): enhance window icon fetching with dynamic bitmap sizing and resource cleanup
Improves the get_window_icon function by dynamically determining the bitmap size based on system metrics. Adds resource cleanup to prevent memory leaks and ensures proper handling of device contexts and icons.
1 parentfb55af3 commit6d8dfc5

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

‎src/core/utils/win32/app_icons.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
importlogging
55
importwin32ui
66
importwin32con
7+
importwin32api
78
importctypes
89
importctypes.wintypes
910
fromcore.utils.win32.app_uwpimportget_package
@@ -19,76 +20,86 @@
1920

2021
TARGETSIZE_REGEX=re.compile(r'targetsize-([0-9]+)')
2122

22-
defget_window_icon(hwnd,smooth_level=0):
23-
"""Fetch the icon of the window at higher quality if possible."""
23+
defget_window_icon(hwnd,smooth_level=0):
24+
"""Fetch the icon of the window."""
2425
try:
2526
hicon=win32gui.SendMessage(hwnd,win32con.WM_GETICON,win32con.ICON_BIG,0)
2627
ifhicon==0:
28+
# If big icon is not available, try to get the small icon
2729
hicon=win32gui.SendMessage(hwnd,win32con.WM_GETICON,win32con.ICON_SMALL,0)
2830
ifhicon==0:
31+
# If both small and big icons are not available, get the class icon
2932
ifhasattr(win32gui,'GetClassLongPtr'):
3033
hicon=win32gui.GetClassLongPtr(hwnd,win32con.GCLP_HICON)
3134
else:
3235
hicon=win32gui.GetClassLong(hwnd,win32con.GCL_HICON)
33-
36+
3437
ifhicon:
35-
desired_size=48# Most of UWPs also return 48x48 icons
3638
hdc_handle=win32gui.GetDC(0)
3739
ifnothdc_handle:
3840
raiseException("Failed to get DC handle")
3941
try:
4042
hdc=win32ui.CreateDCFromHandle(hdc_handle)
4143
hbmp=win32ui.CreateBitmap()
42-
hbmp.CreateCompatibleBitmap(hdc,desired_size,desired_size)
44+
system_icon_size=win32api.GetSystemMetrics(win32con.SM_CXICON)
45+
bitmap_size=int(system_icon_size)
46+
hbmp.CreateCompatibleBitmap(hdc,bitmap_size,bitmap_size)
4347
memdc=hdc.CreateCompatibleDC()
44-
memdc.SelectObject(hbmp)
45-
46-
# Call DrawIconEx via ctypes
47-
DrawIconEx=ctypes.windll.user32.DrawIconEx
48-
DrawIconEx.argtypes= [
49-
ctypes.wintypes.HDC,ctypes.c_int,ctypes.c_int,
50-
ctypes.wintypes.HICON,ctypes.c_int,ctypes.c_int,
51-
ctypes.c_uint,ctypes.wintypes.HBRUSH,ctypes.c_uint
52-
]
53-
DI_NORMAL=0x0003
54-
result=DrawIconEx(memdc.GetSafeHdc(),0,0,hicon,
55-
desired_size,desired_size,0,0,DI_NORMAL)
56-
ifresult==0:
48+
# Select the bitmap into the memory device context
49+
memdc.SelectObject(hbmp)
50+
try:
51+
memdc.DrawIcon((0,0),hicon)
52+
exceptException:
5753
returnNone
58-
54+
5955
bmpinfo=hbmp.GetInfo()
6056
bmpstr=hbmp.GetBitmapBits(True)
57+
6158
raw_data=bytes(bmpstr)
6259
img=Image.frombuffer(
6360
'RGBA',
6461
(bmpinfo['bmWidth'],bmpinfo['bmHeight']),
6562
raw_data,'raw','BGRA',0,1
6663
).convert('RGBA')
64+
#target_size = 48 # target size (48x48) wihout DPI, most of uwps are also 48x48
65+
#img = img.resize((target_size, target_size), Image.LANCZOS)
6766
ifsmooth_level==1:
6867
img=img.filter(ImageFilter.SMOOTH)
6968
elifsmooth_level==2:
7069
img=img.filter(ImageFilter.SMOOTH_MORE)
7170
returnimg
7271
finally:
72+
# Cleaning up resources
73+
#logging.debug("Cleaning up")
7374
try:
7475
win32gui.DestroyIcon(hicon)
75-
exceptException:
76+
#logging.debug("Destroyed hicon")
77+
exceptExceptionase:
78+
#logging.debug(f"Error destroying hicon: {e}")
7679
pass
7780
try:
7881
memdc.DeleteDC()
79-
exceptException:
82+
#logging.debug("Deleted memory device context.")
83+
exceptExceptionase:
84+
#logging.debug(f"Error deleting memory device context: {e}")
8085
pass
8186
try:
8287
hdc.DeleteDC()
83-
exceptException:
88+
#logging.debug("Deleted device context.")
89+
exceptExceptionase:
90+
#logging.debug(f"Error deleting device context: {e}")
8491
pass
8592
try:
8693
win32gui.DeleteObject(hbmp.GetHandle())
87-
exceptException:
94+
#logging.debug("Deleted bitmap object.")
95+
exceptExceptionase:
96+
#logging.debug(f"Error deleting bitmap object: {e}")
8897
pass
8998
try:
9099
win32gui.ReleaseDC(0,hdc_handle)
91-
exceptException:
100+
#logging.debug("Released device context handle.")
101+
exceptExceptionase:
102+
#logging.debug(f"Error releasing device context handle: {e}")
92103
pass
93104
else:
94105
try:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp