Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transparent window crash on Windows 11 #2121

Merged
merged 3 commits into from Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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