From 25ff30ee8c3b07f9bca43ecc60352e3baee8b5d0 Mon Sep 17 00:00:00 2001 From: Shuoliu Yang Date: Mon, 3 Jan 2022 20:54:31 +0800 Subject: [PATCH] Fix transparent window crash on Windows 11 (#2121) Maybe the transparent setting in WM_NCCREATE on Windows 11 will cause a block when calling DwmEnableBlureBehindWindow and will crash. Puts them into WM_CREATE and it works. --- CHANGELOG.md | 1 + src/platform_impl/windows/window.rs | 39 +++++++++++++++++------------ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c47658b3..fb097296 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - On X11, add mappings for numpad comma, numpad enter, numlock and pause. - On macOS, fix Pinyin IME input by reverting a change that intended to improve IME. +- On Windows, fix a crash with transparent windows on Windows 11. # 0.26.0 (2021-12-01) diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 7809cf97..f0ea7fbd 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -16,6 +16,7 @@ use winapi::{ shared::{ minwindef::{HINSTANCE, LPARAM, UINT, WPARAM}, windef::{HWND, POINT, POINTS, RECT}, + winerror::SUCCEEDED, }, um::{ combaseapi, dwmapi, @@ -699,22 +700,6 @@ impl<'a, T: 'static> InitData<'a, T> { let dpi = hwnd_dpi(window); let scale_factor = dpi_to_scale_factor(dpi); - // making the window transparent - if self.attributes.transparent && !self.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 | dwmapi::DWM_BB_BLURREGION, - fEnable: 1, - hRgnBlur: region, - fTransitionOnMaximized: 0, - }; - - dwmapi::DwmEnableBlurBehindWindow(window, &bb); - DeleteObject(region as _); - } - // If the system theme is dark, we need to set the window theme now // before we update the window flags (and possibly show the // window for the first time). @@ -809,6 +794,28 @@ impl<'a, T: 'static> InitData<'a, T> { pub unsafe fn on_create(&mut self) { let win = self.window.as_mut().expect("failed window creation"); + + // making the window transparent + if self.attributes.transparent && !self.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 | dwmapi::DWM_BB_BLURREGION, + fEnable: 1, + hRgnBlur: region, + fTransitionOnMaximized: 0, + }; + let hr = dwmapi::DwmEnableBlurBehindWindow(win.hwnd(), &bb); + if !SUCCEEDED(hr) { + warn!( + "Setting transparent window is failed. HRESULT Code: 0x{:X}", + hr + ); + } + DeleteObject(region as _); + } + let attributes = self.attributes.clone(); // Set visible before setting the size to ensure the