diff --git a/examples/quick-blur/MainWindow.qml b/examples/quick-blur/MainWindow.qml index 326f753..f252440 100644 --- a/examples/quick-blur/MainWindow.qml +++ b/examples/quick-blur/MainWindow.qml @@ -47,7 +47,7 @@ FramelessWindow { if (!Settings.restoreGeometry(window)) { FramelessHelper.moveWindowToDesktopCenter(); } - FramelessHelper.blurBehindWindow = true; + FramelessHelper.blurBehindWindowEnabled = true; // Finally, show the window after everything is setted. window.visible = true; } diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index ab582d2..e5cd34f 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -62,6 +62,7 @@ struct Win32UtilsHelper Q_GLOBAL_STATIC(Win32UtilsHelper, g_utilsHelper) +static constexpr const wchar_t DUMMY_WINDOW_CLASS_NAME[] = L"FRAMELESSHELPER_DUMMY_WINDOW_CLASS"; static const QString qDwmRegistryKey = QString::fromWCharArray(kDwmRegistryKey); static const QString qPersonalizeRegistryKey = QString::fromWCharArray(kPersonalizeRegistryKey); static const QString qDwmColorKeyName = QString::fromWCharArray(kDwmColorKeyName); @@ -137,6 +138,9 @@ FRAMELESSHELPER_STRING_CONSTANT(GetDpiForSystem) FRAMELESSHELPER_STRING_CONSTANT(DwmGetWindowAttribute) FRAMELESSHELPER_STRING_CONSTANT(ntdll) FRAMELESSHELPER_STRING_CONSTANT(RtlGetVersion) +FRAMELESSHELPER_STRING_CONSTANT(GetModuleHandleW) +FRAMELESSHELPER_STRING_CONSTANT(RegisterClassExW) +FRAMELESSHELPER_STRING_CONSTANT(CreateWindowExW) template class HumbleComPtr @@ -193,6 +197,36 @@ private: T *p = nullptr; }; +[[nodiscard]] static inline HWND ensureDummyWindow() +{ + static const HWND hwnd = []() -> HWND { + const HMODULE instance = GetModuleHandleW(nullptr); + if (!instance) { + qWarning() << Utils::getSystemErrorMessage(kGetModuleHandleW); + return nullptr; + } + WNDCLASSEXW wcex; + SecureZeroMemory(&wcex, sizeof(wcex)); + wcex.cbSize = sizeof(wcex); + wcex.lpfnWndProc = DefWindowProcW; + wcex.hInstance = instance; + wcex.lpszClassName = DUMMY_WINDOW_CLASS_NAME; + const ATOM atom = RegisterClassExW(&wcex); + if (!atom) { + qWarning() << Utils::getSystemErrorMessage(kRegisterClassExW); + return nullptr; + } + const HWND window = CreateWindowExW(0, DUMMY_WINDOW_CLASS_NAME, nullptr, + WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, nullptr, nullptr, instance, nullptr); + if (!window) { + qWarning() << Utils::getSystemErrorMessage(kCreateWindowExW); + return nullptr; + } + return window; + }(); + return hwnd; +} + [[nodiscard]] static inline bool doCompareWindowsVersion(const VersionNumber &targetOsVer) { static const std::optional currentOsVer = []() -> std::optional { @@ -797,7 +831,14 @@ bool Utils::isHighContrastModeEnabled() quint32 Utils::getPrimaryScreenDpi(const bool horizontal) { if (API_SHCORE_AVAILABLE(GetDpiForMonitor)) { - const HMONITOR monitor = MonitorFromPoint(POINT{50, 50}, MONITOR_DEFAULTTOPRIMARY); + const HMONITOR monitor = []() -> HMONITOR { + const HWND window = ensureDummyWindow(); + if (window) { + return MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY); + } + static constexpr const int kTaskBarSize = 100; + return MonitorFromPoint(POINT{kTaskBarSize, kTaskBarSize}, MONITOR_DEFAULTTOPRIMARY); + }(); if (monitor) { UINT dpiX = 0, dpiY = 0; const HRESULT hr = API_CALL_FUNCTION(GetDpiForMonitor, monitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY); diff --git a/src/quick/framelessquickhelper.cpp b/src/quick/framelessquickhelper.cpp index 1dfb770..f16ad97 100644 --- a/src/quick/framelessquickhelper.cpp +++ b/src/quick/framelessquickhelper.cpp @@ -181,7 +181,7 @@ void FramelessQuickHelperPrivate::attachToWindow() // we reach here, and all the modifications from the Qt side will be lost // due to QPA will reset the position and size of the window during it's // initialization process. - QTimer::singleShot(0, this, [this](){ + QTimer::singleShot(200, this, [this](){ if (FramelessConfig::instance()->isSet(Option::CenterWindowBeforeShow)) { moveWindowToDesktopCenter(); } diff --git a/src/widgets/framelesswidgetshelper.cpp b/src/widgets/framelesswidgetshelper.cpp index 78782c5..8f55d1f 100644 --- a/src/widgets/framelesswidgetshelper.cpp +++ b/src/widgets/framelesswidgetshelper.cpp @@ -304,7 +304,7 @@ void FramelessWidgetsHelperPrivate::attachToWindow() // we reach here, and all the modifications from the Qt side will be lost // due to QPA will reset the position and size of the window during it's // initialization process. - QTimer::singleShot(0, this, [this](){ + QTimer::singleShot(200, this, [this](){ if (FramelessConfig::instance()->isSet(Option::CenterWindowBeforeShow)) { moveWindowToDesktopCenter(); }