From 45ad1b8878e786f12db0c1803318900be506f4df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Thu, 31 Dec 2020 19:15:27 +0100 Subject: [PATCH 1/2] Restore the ability to have fully transparent windows on Windows Besides its original purpose, commit 6343059b "Fix Windows transparency behavior to support fully-opaque regions (#1621)" also included some changes considered cleanups, one of them was: * Remove the `CreateRectRgn` call, since we want the entire window's region to have blur behind it, and `DwnEnableBlurBehindWindow` does that by default. But the original code actually disabled the blur effect for the whole window by creating an empty region for it, because that allows for the window to be truely fully transparent. With the blur effect in place, the areas meant to be transparent either blur the things behind it (until Windows 8) or are darkened (since Windows 8). This also means that on Windows 8 and newer, the resulting colors are darker than intended in translucent areas when the blur effect is enabled. This restores the behaviour from winit <0.24 and fixes #1814. Arguably, one might want to expose the ability to control the blur region, but that is outside the scope of this commit. --- src/platform_impl/windows/window.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 0eb7616094..e65e64c5f3 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -25,6 +25,7 @@ use winapi::{ ole2, oleidl::LPDROPTARGET, shobjidl_core::{CLSID_TaskbarList, ITaskbarList2}, + wingdi::{CreateRectRgn, DeleteObject}, winnt::LPCWSTR, winuser, }, @@ -743,14 +744,18 @@ unsafe fn init( // making the window transparent if attributes.transparent && !pl_attribs.no_redirection_bitmap { + // Empty region for the blur effect, so the window is fully transparent + let region = CreateRectRgn(0, 0, -1, -1); + let bb = dwmapi::DWM_BLURBEHIND { - dwFlags: dwmapi::DWM_BB_ENABLE, + dwFlags: dwmapi::DWM_BB_ENABLE | dwmapi::DWM_BB_BLURREGION, fEnable: 1, - hRgnBlur: ptr::null_mut(), + hRgnBlur: region, fTransitionOnMaximized: 0, }; dwmapi::DwmEnableBlurBehindWindow(real_window.0, &bb); + DeleteObject(region as _); if attributes.decorations { let opacity = 255; From b5edd245e47df9c37216bb70831c56f5fd92def4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Sun, 3 Jan 2021 16:28:52 +0100 Subject: [PATCH 2/2] Remove useless WS_EX_LAYERED from transparent windows on Windows `WS_EX_LAYERED` is not supposed to be used in combination with `CS_OWNDC`. In winit, as it is currently used, `WS_EX_LAYERED` actually has no effect at all. The only relevant call is to `SetLayeredWindowAttributes`, which is required to make the window visible at all with `WS_EX_LAYERED` set, but is called with full opacity, i.e. there's no transparency involved at all. The actual transparency is already achieved by using `DwmEnableBlurBehindWindow`, so `WS_EX_LAYERED` and the call to `SetLayeredWindowAttributes` can both be removed. --- src/platform_impl/windows/window.rs | 6 ------ src/platform_impl/windows/window_state.rs | 3 --- 2 files changed, 9 deletions(-) diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index e65e64c5f3..807a0d61c0 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -756,12 +756,6 @@ unsafe fn init( dwmapi::DwmEnableBlurBehindWindow(real_window.0, &bb); DeleteObject(region as _); - - if attributes.decorations { - let opacity = 255; - - winuser::SetLayeredWindowAttributes(real_window.0, 0, opacity, winuser::LWA_ALPHA); - } } // If the system theme is dark, we need to set the window theme now diff --git a/src/platform_impl/windows/window_state.rs b/src/platform_impl/windows/window_state.rs index 70c5330aed..937a411ecd 100644 --- a/src/platform_impl/windows/window_state.rs +++ b/src/platform_impl/windows/window_state.rs @@ -210,9 +210,6 @@ impl WindowFlags { if self.contains(WindowFlags::NO_BACK_BUFFER) { style_ex |= WS_EX_NOREDIRECTIONBITMAP; } - if self.contains(WindowFlags::TRANSPARENT) && self.contains(WindowFlags::DECORATIONS) { - style_ex |= WS_EX_LAYERED; - } if self.contains(WindowFlags::CHILD) { style |= WS_CHILD; // This is incompatible with WS_POPUP if that gets added eventually. }