fix: window content protection on older Windows versions (#47856)

This commit is contained in:
Shelley Vohr 2025-07-24 21:05:28 +02:00 committed by GitHub
commit 01faaa30ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 6 deletions

View file

@ -1282,12 +1282,7 @@ void NativeWindowViews::SetOpacity(const double opacity) {
#if BUILDFLAG(IS_WIN) #if BUILDFLAG(IS_WIN)
const double boundedOpacity = std::clamp(opacity, 0.0, 1.0); const double boundedOpacity = std::clamp(opacity, 0.0, 1.0);
HWND hwnd = GetAcceleratedWidget(); HWND hwnd = GetAcceleratedWidget();
if (!layered_) { SetLayered();
LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
ex_style |= WS_EX_LAYERED;
::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
layered_ = true;
}
::SetLayeredWindowAttributes(hwnd, 0, boundedOpacity * 255, LWA_ALPHA); ::SetLayeredWindowAttributes(hwnd, 0, boundedOpacity * 255, LWA_ALPHA);
opacity_ = boundedOpacity; opacity_ = boundedOpacity;
#else #else
@ -1730,6 +1725,16 @@ void NativeWindowViews::UpdateThickFrame() {
FlipWindowStyle(GetAcceleratedWidget(), resizable_, WS_THICKFRAME); FlipWindowStyle(GetAcceleratedWidget(), resizable_, WS_THICKFRAME);
} }
} }
void NativeWindowViews::SetLayered() {
HWND hwnd = GetAcceleratedWidget();
if (!layered_) {
LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
ex_style |= WS_EX_LAYERED;
::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
layered_ = true;
}
}
#endif #endif
void NativeWindowViews::OnWidgetActivationChanged(views::Widget* changed_widget, void NativeWindowViews::OnWidgetActivationChanged(views::Widget* changed_widget,

View file

@ -178,6 +178,7 @@ class NativeWindowViews : public NativeWindow,
#if BUILDFLAG(IS_WIN) #if BUILDFLAG(IS_WIN)
TaskbarHost& taskbar_host() { return taskbar_host_; } TaskbarHost& taskbar_host() { return taskbar_host_; }
void UpdateThickFrame(); void UpdateThickFrame();
void SetLayered();
#endif #endif
SkColor overlay_button_color() const { return overlay_button_color_; } SkColor overlay_button_color() const { return overlay_button_color_; }

View file

@ -176,6 +176,11 @@ void ElectronDesktopWindowTreeHostWin::UpdateAllowScreenshots() {
if (allowed == allow_screenshots_) if (allowed == allow_screenshots_)
return; return;
// On some older Windows versions, setting the display affinity
// to WDA_EXCLUDEFROMCAPTURE won't prevent the window from being
// captured - setting WS_EX_LAYERED mitigates this issue.
if (base::win::GetVersion() < base::win::Version::WIN11_22H2)
native_window_view_->SetLayered();
::SetWindowDisplayAffinity( ::SetWindowDisplayAffinity(
GetAcceleratedWidget(), GetAcceleratedWidget(),
allow_screenshots_ ? WDA_NONE : WDA_EXCLUDEFROMCAPTURE); allow_screenshots_ ? WDA_NONE : WDA_EXCLUDEFROMCAPTURE);