Skip to content

Commit

Permalink
Fix transparent window crash on Windows 11 (#2121)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yslib committed Jan 3, 2022
1 parent 6b250a7 commit 25ff30e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
39 changes: 23 additions & 16 deletions src/platform_impl/windows/window.rs
Expand Up @@ -16,6 +16,7 @@ use winapi::{
shared::{
minwindef::{HINSTANCE, LPARAM, UINT, WPARAM},
windef::{HWND, POINT, POINTS, RECT},
winerror::SUCCEEDED,
},
um::{
combaseapi, dwmapi,
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 25ff30e

Please sign in to comment.