Skip to content

The only downloader you need. 下载器的集大成者。

refactor(taskbar): Win32 结构体提升到模块级

XiaoYouChR52b0e9993788
diff --git a/app/platform/taskbar.py b/app/platform/taskbar.py
index fc08d220..077c3b0c 100644
--- a/app/platform/taskbar.py
+++ b/app/platform/taskbar.py
@@ -2,7 +2,7 @@ from __future__ import annotations
import ctypes
from ctypes import (
- byref, c_byte, c_int, c_short, c_uint64, c_ulong, c_ushort, c_void_p, c_wchar_p,
+ byref, c_byte, c_int, c_int16, c_int32, c_uint8, c_uint16, c_uint32, c_uint64, c_ulong, c_void_p, c_wchar_p,
)
from functools import cache, partial
@@ -11,6 +11,30 @@ from loguru import logger
from app.models.task import TaskStatus
+class GUID(ctypes.Structure):
+ _fields_ = [("Data1", c_uint32), ("Data2", c_uint16), ("Data3", c_uint16), ("Data4", c_uint8 * 8)]
+
+
+class PROPERTYKEY(ctypes.Structure):
+ _fields_ = [("fmtid", GUID), ("pid", c_uint32)]
+
+
+class PROPVARIANT_VALUE(ctypes.Union):
+ # 占位成员对应 SDK 中最大的 BLOB 成员 {ULONG, 指针},否则 SetValue 复制时越界读
+ _fields_ = [("boolVal", c_int16), ("pwszVal", c_wchar_p), ("reserved", c_void_p * 2)]
+
+
+class PROPVARIANT(ctypes.Structure):
+ _fields_ = [("vt", c_uint16), ("r1", c_uint16), ("r2", c_uint16), ("r3", c_uint16),
+ ("value", PROPVARIANT_VALUE)]
+
+
+class WINDOWPLACEMENT(ctypes.Structure):
+ _fields_ = [("length", c_uint32), ("flags", c_uint32), ("showCmd", c_uint32),
+ ("ptMinPosition", c_int32 * 2), ("ptMaxPosition", c_int32 * 2),
+ ("rcNormalPosition", c_int32 * 4)]
+
+
def setWindowsProgress(hwnd: int, status: TaskStatus | None, progress: float | None) -> None:
"""系统不支持时静默跳过,不抛异常。"""
taskbarList = createTaskbarList()
@@ -30,26 +54,13 @@ def setWindowsProgress(hwnd: int, status: TaskStatus | None, progress: float | N
def setWindowAppId(hwnd: int, appId: str | None) -> None:
"""系统不支持时静默跳过,不抛异常。"""
- from ctypes import HRESULT, WINFUNCTYPE, oledll, wintypes
-
- class GUID(ctypes.Structure):
- _fields_ = [("Data1", wintypes.DWORD), ("Data2", wintypes.WORD),
- ("Data3", wintypes.WORD), ("Data4", wintypes.BYTE * 8)]
-
- class PROPERTYKEY(ctypes.Structure):
- _fields_ = [("fmtid", GUID), ("pid", wintypes.DWORD)]
-
- class PROPVARIANT_VALUE(ctypes.Union):
- _fields_ = [("boolVal", c_short), ("pwszVal", c_wchar_p)]
-
- class PROPVARIANT(ctypes.Structure):
- _fields_ = [("vt", c_ushort), ("r1", c_ushort), ("r2", c_ushort), ("r3", c_ushort), ("value", PROPVARIANT_VALUE)]
+ from ctypes import HRESULT, WINFUNCTYPE, oledll
VT_EMPTY, VT_BOOL, VT_LPWSTR = 0, 11, 31
store = ctypes.c_void_p()
iid = GUID(0x886D8EEB, 0x8CF2, 0x4446, (0x8D, 0x02, 0xCD, 0xBA, 0x1D, 0xBD, 0xCF, 0x99))
try:
- oledll.shell32.SHGetPropertyStoreForWindow(wintypes.HWND(hwnd), ctypes.byref(iid), ctypes.byref(store))
+ oledll.shell32.SHGetPropertyStoreForWindow(c_void_p(hwnd), ctypes.byref(iid), ctypes.byref(store))
except OSError as e:
logger.warning("无法取得窗口属性: {}", e)
return
@@ -84,22 +95,14 @@ def addTaskbarButton(hwnd: int) -> None:
def minimizeWithoutActivating(hwnd: int, x: int, y: int, width: int, height: int) -> None:
# Qt 对 WA_ShowWithoutActivating 的窗口会把 SW_SHOWMINIMIZED 改成 SW_SHOWNOACTIVATE
- from ctypes import wintypes
-
addTaskbarButton(hwnd)
-
- class WINDOWPLACEMENT(ctypes.Structure):
- _fields_ = [("length", wintypes.UINT), ("flags", wintypes.UINT), ("showCmd", wintypes.UINT),
- ("ptMinPosition", wintypes.POINT), ("ptMaxPosition", wintypes.POINT),
- ("rcNormalPosition", wintypes.RECT)]
-
SW_SHOWMINNOACTIVE = 7
user32 = ctypes.windll.user32
placement = WINDOWPLACEMENT()
placement.length = ctypes.sizeof(WINDOWPLACEMENT)
user32.GetWindowPlacement(hwnd, ctypes.byref(placement))
placement.showCmd = SW_SHOWMINNOACTIVE
- placement.rcNormalPosition = wintypes.RECT(x, y, x + width, y + height)
+ placement.rcNormalPosition = (c_int32 * 4)(x, y, x + width, y + height)
user32.SetWindowPlacement(hwnd, ctypes.byref(placement))