Skip to content

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

添加窗口边框颜色 Accent 功能, 修复 Win 11 Mica 系列特效异常的 Bug, 修复多余的 Win7 Aero 效果渲染.

XiaoYouChR6155989d2846
diff --git a/app/common/methods.py b/app/common/methods.py
index 8a2872a2..b6284798 100644
--- a/app/common/methods.py
+++ b/app/common/methods.py
@@ -25,6 +25,22 @@ from app.common.signal_bus import signalBus
plugins = []
+
+def isBorderAccentColorOpen():
+ """ Check whether the border accent color is open """
+ from qframelesswindow.utils.win32_utils import isGreaterEqualWin10
+
+ if not isGreaterEqualWin10():
+ return False
+
+ from winreg import OpenKey, HKEY_CURRENT_USER, KEY_READ, QueryValueEx, CloseKey
+
+ key = OpenKey(HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\DWM", 0, KEY_READ)
+ value, _ = QueryValueEx(key, "ColorPrevalence")
+ CloseKey(key)
+
+ return bool(value)
+
def isAbleToShowToast():
return sys.platform == 'win32' and sys.getwindowsversion().build >= 16299 # 高于 Win10 1709
diff --git a/app/common/windows_window_effect.py b/app/common/windows_window_effect.py
new file mode 100644
index 00000000..a349c3bf
--- /dev/null
+++ b/app/common/windows_window_effect.py
@@ -0,0 +1,111 @@
+import warnings
+from ctypes import c_int
+from ctypes.wintypes import DWORD
+from sys import getwindowsversion
+
+from PySide6.QtGui import QColor
+from _ctypes import byref, pointer
+from qframelesswindow.utils.win32_utils import isGreaterEqualWin11, isCompositionEnabled, isGreaterEqualWin8_1
+from qframelesswindow.windows import WindowsWindowEffect
+from qframelesswindow.windows.c_structures import MARGINS, WINDOWCOMPOSITIONATTRIB, ACCENT_STATE
+
+
+class WindowsWindowEffect(WindowsWindowEffect):
+
+ def addShadowEffect(self, hWnd):
+ """ Add DWM shadow to window
+
+ Parameters
+ ----------
+ hWnd: int or `sip.voidptr`
+ Window handle
+ """
+ if not isCompositionEnabled():
+ return
+
+ hWnd = int(hWnd)
+ margins = MARGINS(1, 0, 0, 0) # 防止 Windows 7 渲染多余的 Aero 效果
+ self.DwmExtendFrameIntoClientArea(hWnd, byref(margins))
+
+ def setAeroEffect(self, hWnd):
+ """ Add the aero effect to the window
+
+ Parameters
+ ----------
+ hWnd: int or `sip.voidptr`
+ Window handle
+ """
+ hWnd = int(hWnd)
+ if isGreaterEqualWin8_1():
+ self.winCompAttrData.Attribute = WINDOWCOMPOSITIONATTRIB.WCA_ACCENT_POLICY.value
+ self.accentPolicy.AccentState = ACCENT_STATE.ACCENT_ENABLE_BLURBEHIND.value
+ self.SetWindowCompositionAttribute(hWnd, pointer(self.winCompAttrData))
+ else:
+ margins = MARGINS(-1, -1, -1, -1)
+ self.DwmExtendFrameIntoClientArea(hWnd, byref(margins))
+
+ def setBorderAccentColor(self, hWnd, color:QColor):
+ """ Set the border color of the window"""
+
+ # if not isGreaterEqualWin10():
+ # return
+
+ hWnd = int(hWnd)
+ colorref = DWORD(color.red() | (color.green() << 8) | (color.blue() << 16))
+ self.DwmSetWindowAttribute(hWnd,
+ 34,
+ byref(colorref),
+ 4)
+
+ def removeBorderAccentColor(self, hWnd):
+ """ Remove the border color of the window"""
+
+ # if not isGreaterEqualWin10():
+ # return
+
+ hWnd = int(hWnd)
+ self.DwmSetWindowAttribute(hWnd,
+ 34,
+ byref(DWORD(0xFFFFFFFF)),
+ 4)
+
+ def setMicaEffect(self, hWnd, isDarkMode=False, isAlt=False, isBlur=False):
+ """ Add the mica effect to the window (Win11 only)
+
+ Parameters
+ ----------
+ hWnd: int or `sip.voidptr`
+ Window handle
+
+ isDarkMode: bool
+ whether to use dark mode mica effect
+
+ isAlt: bool
+ whether to enable mica alt effect
+ """
+ if not isGreaterEqualWin11():
+ warnings.warn("The mica effect is only available on Win11")
+ return
+
+ hWnd = int(hWnd)
+ margins = MARGINS(16777215, 16777215, 0, 0)
+ self.DwmExtendFrameIntoClientArea(hWnd, byref(margins))
+
+ self.winCompAttrData.Attribute = WINDOWCOMPOSITIONATTRIB.WCA_ACCENT_POLICY.value
+ self.accentPolicy.AccentState = ACCENT_STATE.ACCENT_ENABLE_HOSTBACKDROP.value
+ self.SetWindowCompositionAttribute(hWnd, pointer(self.winCompAttrData))
+
+ if isDarkMode:
+ self.winCompAttrData.Attribute = WINDOWCOMPOSITIONATTRIB.WCA_USEDARKMODECOLORS.value
+ self.SetWindowCompositionAttribute(hWnd, pointer(self.winCompAttrData))
+
+ if getwindowsversion().build < 22523:
+ self.DwmSetWindowAttribute(hWnd, 1029, byref(c_int(1)), 4)
+ else:
+ if isBlur:
+ _ = 3
+ else:
+ _ = 4 if isAlt else 2
+ self.DwmSetWindowAttribute(hWnd, 38, byref(c_int(_)), 4)
+
+ self.DwmSetWindowAttribute(hWnd, 20, byref(c_int(1*isDarkMode)), 4)
\ No newline at end of file
diff --git a/app/view/main_window.py b/app/view/main_window.py
index 39aeb780..8b1f4e8e 100644
--- a/app/view/main_window.py
+++ b/app/view/main_window.py
@@ -1,8 +1,7 @@
# coding: utf-8
-import ctypes
import pickle
import sys
-from ctypes import byref, c_int
+from ctypes.wintypes import MSG
from pathlib import Path
import darkdetect
@@ -10,15 +9,17 @@ from PySide6.QtCore import QSize, QThread, Signal, QTimer, QPropertyAnimation, Q
from PySide6.QtGui import QIcon, QDragEnterEvent, QDropEvent, QKeySequence, QDesktopServices
from PySide6.QtWidgets import QApplication, QGraphicsOpacityEffect
from loguru import logger
-from qfluentwidgets import FluentIcon as FIF, setTheme, Theme
+from qfluentwidgets import FluentIcon as FIF, setTheme, Theme, themeColor
from qfluentwidgets import NavigationItemPosition, MSFluentWindow, SplashScreen
+from qframelesswindow.utils.win32_utils import isGreaterEqualWin10
from .setting_interface import SettingInterface
from .task_interface import TaskInterface
from ..common.config import cfg, Headers, attachmentTypes, FEEDBACK_URL
from ..common.custom_socket import GhostDownloaderSocketServer
-from ..common.methods import getLinkInfo, bringWindowToTop, addDownloadTask, showMessageBox
+from ..common.methods import getLinkInfo, bringWindowToTop, addDownloadTask, showMessageBox, isBorderAccentColorOpen
from ..common.signal_bus import signalBus
+from ..common.windows_window_effect import WindowsWindowEffect
from ..components.add_task_dialog import AddTaskOptionDialog
from ..components.custom_tray import CustomSystemTrayIcon
from ..components.update_dialog import checkUpdate
@@ -52,6 +53,14 @@ class MainWindow(MSFluentWindow):
def __init__(self):
super().__init__()
+
+ # replace WindowsWindowEffect
+ if isGreaterEqualWin10():
+ self.windowEffect = WindowsWindowEffect(self.winId())
+
+ if isBorderAccentColorOpen():
+ self.windowEffect.setBorderAccentColor(self.winId(), themeColor())
+
self.initWindow()
# create sub interface
@@ -185,22 +194,27 @@ class MainWindow(MSFluentWindow):
if cfg.backgroundEffect.value == 'Acrylic':
self._isMicaEnabled = True
+ self.setStyleSheet("background-color: transparent")
self.windowEffect.setAcrylicEffect(self.winId(), "00000030" if _ else "FFFFFF30")
elif cfg.backgroundEffect.value == 'Mica':
self._isMicaEnabled = True
+ self.setStyleSheet("background-color: transparent")
self.windowEffect.setMicaEffect(self.winId(), _)
elif cfg.backgroundEffect.value == 'MicaBlur':
self._isMicaEnabled = True
- self.windowEffect.setMicaEffect(self.winId(), _)
- self.windowEffect.DwmSetWindowAttribute(self.winId(), 38, byref(c_int(3)), 4)
+ self.windowEffect.setMicaEffect(self.winId(), _, isBlur=True)
+ self.setStyleSheet("background-color: transparent")
elif cfg.backgroundEffect.value == 'MicaAlt':
self._isMicaEnabled = True
- self.windowEffect.setMicaEffect(self.winId(), _, True)
+ self.windowEffect.setMicaEffect(self.winId(), _, isAlt=True)
+ self.setStyleSheet("background-color: transparent")
elif cfg.backgroundEffect.value == 'Aero':
self._isMicaEnabled = True
self.windowEffect.setAeroEffect(self.winId())
+ self.setStyleSheet("background-color: transparent")
elif cfg.backgroundEffect.value == 'None':
self._isMicaEnabled = False
+ self.setStyleSheet("")
def initNavigation(self):
# add navigation items
@@ -281,13 +295,21 @@ class MainWindow(MSFluentWindow):
def nativeEvent(self, eventType, message):
# 处理窗口重复打开事件
if eventType == "windows_generic_MSG":
- msg = ctypes.wintypes.MSG.from_address(message.__int__())
+ msg = MSG.from_address(message.__int__())
# WIN_USER = 1024
if msg.message == 1024 + 1:
self.show()
return True, 0
+ if msg.message == 7:
+ if isBorderAccentColorOpen():
+ self.windowEffect.setBorderAccentColor(self.winId(), themeColor())
+
+ if msg.message == 8:
+ if isBorderAccentColorOpen():
+ self.windowEffect.removeBorderAccentColor(self.winId())
+
return super().nativeEvent(eventType, message)
def dragEnterEvent(self, event: QDragEnterEvent):