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.
This commit is contained in:
Shuoliu Yang 2022-01-03 20:54:31 +08:00 committed by GitHub
parent 6b250a74f8
commit 25ff30ee8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 16 deletions

View file

@ -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)

View file

@ -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