From dd32ace9ab4179cdbbccdc191d2bc7a513569c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Wed, 17 Feb 2021 13:50:24 +0100 Subject: [PATCH] Restore the ability to have fully transparent windows on Windows (#1815) * 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. * 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 | 15 +++++++-------- src/platform_impl/windows/window_state.rs | 3 --- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 54389b39..cc04b8f7 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, }, @@ -753,20 +754,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); - - if attributes.decorations { - let opacity = 255; - - winuser::SetLayeredWindowAttributes(real_window.0, 0, opacity, winuser::LWA_ALPHA); - } + DeleteObject(region as _); } // 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 6a6f0255..9ef55232 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. }