Windows: Fix transparency (#675)

* Windows: Fix transparency (#260)

* Windows: Only enable WS_EX_LAYERED for transparent windows

* Update winapi to 0.3.6

* Windows: Amend transparency code

* Add transparency fix to CHANGELOG.md
This commit is contained in:
Alex Taylor 2018-10-17 17:23:59 -07:00 committed by Francesca Plebani
parent 5d5fcb3911
commit 26b70e457b
3 changed files with 23 additions and 3 deletions

View file

@ -12,6 +12,7 @@
- Fixed UTF8 handling bug in X11 `set_title` function. - Fixed UTF8 handling bug in X11 `set_title` function.
- On Windows, `Window::set_cursor` now applies immediately instead of requiring specific events to occur first. - On Windows, `Window::set_cursor` now applies immediately instead of requiring specific events to occur first.
- On Windows, fix window.set_maximized(). - On Windows, fix window.set_maximized().
- On Windows, fix transparency (#260).
# Version 0.17.2 (2018-08-19) # Version 0.17.2 (2018-08-19)

View file

@ -35,7 +35,7 @@ core-foundation = "0.6"
core-graphics = "0.16" core-graphics = "0.16"
[target.'cfg(target_os = "windows")'.dependencies.winapi] [target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3.5" version = "0.3.6"
features = [ features = [
"combaseapi", "combaseapi",
"dwmapi", "dwmapi",

View file

@ -13,6 +13,7 @@ use winapi::shared::windef::{HWND, LPPOINT, POINT, RECT};
use winapi::um::{combaseapi, dwmapi, libloaderapi, winuser}; use winapi::um::{combaseapi, dwmapi, libloaderapi, winuser};
use winapi::um::objbase::COINIT_MULTITHREADED; use winapi::um::objbase::COINIT_MULTITHREADED;
use winapi::um::shobjidl_core::{CLSID_TaskbarList, ITaskbarList2}; use winapi::um::shobjidl_core::{CLSID_TaskbarList, ITaskbarList2};
use winapi::um::wingdi::{CreateRectRgn, DeleteObject};
use winapi::um::winnt::{LONG, LPCWSTR}; use winapi::um::winnt::{LONG, LPCWSTR};
use { use {
@ -903,6 +904,9 @@ unsafe fn init(
if pl_attribs.no_redirection_bitmap { if pl_attribs.no_redirection_bitmap {
ex_style |= winuser::WS_EX_NOREDIRECTIONBITMAP; ex_style |= winuser::WS_EX_NOREDIRECTIONBITMAP;
} }
if attributes.transparent && attributes.decorations {
ex_style |= winuser::WS_EX_LAYERED;
}
// adjusting the window coordinates using the style // adjusting the window coordinates using the style
winuser::AdjustWindowRectEx(&mut rect, style, 0, ex_style); winuser::AdjustWindowRectEx(&mut rect, style, 0, ex_style);
@ -1017,14 +1021,29 @@ unsafe fn init(
// making the window transparent // making the window transparent
if attributes.transparent && !pl_attribs.no_redirection_bitmap { if attributes.transparent && !pl_attribs.no_redirection_bitmap {
let region = CreateRectRgn(0, 0, -1, -1); // makes the window transparent
let bb = dwmapi::DWM_BLURBEHIND { let bb = dwmapi::DWM_BLURBEHIND {
dwFlags: 0x1, // FIXME: DWM_BB_ENABLE; dwFlags: dwmapi::DWM_BB_ENABLE | dwmapi::DWM_BB_BLURREGION,
fEnable: 1, fEnable: 1,
hRgnBlur: ptr::null_mut(), hRgnBlur: region,
fTransitionOnMaximized: 0, fTransitionOnMaximized: 0,
}; };
dwmapi::DwmEnableBlurBehindWindow(real_window.0, &bb); dwmapi::DwmEnableBlurBehindWindow(real_window.0, &bb);
DeleteObject(region as _);
if attributes.decorations {
// HACK: When opaque (opacity 255), there is a trail whenever
// the transparent window is moved. By reducing it to 254,
// the window is rendered properly.
let opacity = 254;
// The color key can be any value except for black (0x0).
let color_key = 0x0030c100;
winuser::SetLayeredWindowAttributes(real_window.0, color_key, opacity, winuser::LWA_ALPHA);
}
} }
let win = Window { let win = Window {