|
| 1 | +//! DWM Window Cloaking — hide windows from screen capture while remaining visible. |
| 2 | +//! |
| 3 | +//! Uses `DwmSetWindowAttribute(DWMWA_CLOAK)` to make a window invisible to |
| 4 | +//! PrintScreen, OBS, and other capture methods while still rendering on desktop. |
| 5 | +//! PowerToys and Flow Launcher use this technique for instant show/hide. |
| 6 | +//! |
| 7 | +//! # Requirements |
| 8 | +//! - Windows 8+ (DWM is always-on since Windows 8) |
| 9 | +//! - No elevated privileges required |
| 10 | +//! - Window must be a top-level window (not a child window) |
| 11 | +
|
| 12 | +#[cfg(windows)] |
| 13 | +use std::ffi::c_void; |
| 14 | +#[cfg(windows)] |
| 15 | +use std::mem; |
| 16 | + |
| 17 | +#[cfg(windows)] |
| 18 | +use windows::Win32::Foundation::{BOOL, HWND}; |
| 19 | +#[cfg(windows)] |
| 20 | +use windows::Win32::Graphics::Dwm::{ |
| 21 | + DwmGetWindowAttribute, DwmSetWindowAttribute, DWMWA_CLOAK, DWMWA_CLOAKED, |
| 22 | +}; |
| 23 | + |
| 24 | +/// Cloak a window — invisible to screen capture but still visible on desktop. |
| 25 | +#[cfg(windows)] |
| 26 | +pub fn cloak_window(hwnd: HWND) -> windows::core::Result<()> { |
| 27 | + set_cloak(hwnd, true) |
| 28 | +} |
| 29 | + |
| 30 | +/// Uncloak a window — visible to screen capture again. |
| 31 | +#[cfg(windows)] |
| 32 | +pub fn uncloak_window(hwnd: HWND) -> windows::core::Result<()> { |
| 33 | + set_cloak(hwnd, false) |
| 34 | +} |
| 35 | + |
| 36 | +/// Check whether a window is currently cloaked (by app, shell, or inheritance). |
| 37 | +#[cfg(windows)] |
| 38 | +pub fn is_cloaked(hwnd: HWND) -> windows::core::Result<bool> { |
| 39 | + let mut cloaked: u32 = 0; |
| 40 | + unsafe { |
| 41 | + DwmGetWindowAttribute( |
| 42 | + hwnd, |
| 43 | + DWMWA_CLOAKED, |
| 44 | + &mut cloaked as *mut u32 as *mut c_void, |
| 45 | + mem::size_of::<u32>() as u32, |
| 46 | + )?; |
| 47 | + } |
| 48 | + Ok(cloaked != 0) |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(windows)] |
| 52 | +fn set_cloak(hwnd: HWND, cloak: bool) -> windows::core::Result<()> { |
| 53 | + let value = if cloak { BOOL(1) } else { BOOL(0) }; |
| 54 | + unsafe { |
| 55 | + DwmSetWindowAttribute( |
| 56 | + hwnd, |
| 57 | + DWMWA_CLOAK, |
| 58 | + &value as *const BOOL as *const c_void, |
| 59 | + mem::size_of::<BOOL>() as u32, |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// --- Non-Windows stubs (compile on Linux, always return error) --- |
| 65 | + |
| 66 | +/// Cloak a window (non-Windows stub — always errors). |
| 67 | +#[cfg(not(windows))] |
| 68 | +pub fn cloak_window(_hwnd_raw: isize) -> Result<(), String> { |
| 69 | + Err("DWM cloaking requires Windows".to_string()) |
| 70 | +} |
| 71 | + |
| 72 | +/// Uncloak a window (non-Windows stub — always errors). |
| 73 | +#[cfg(not(windows))] |
| 74 | +pub fn uncloak_window(_hwnd_raw: isize) -> Result<(), String> { |
| 75 | + Err("DWM cloaking requires Windows".to_string()) |
| 76 | +} |
| 77 | + |
| 78 | +/// Check cloaked state (non-Windows stub — always errors). |
| 79 | +#[cfg(not(windows))] |
| 80 | +pub fn is_cloaked(_hwnd_raw: isize) -> Result<bool, String> { |
| 81 | + Err("DWM cloaking requires Windows".to_string()) |
| 82 | +} |
| 83 | + |
| 84 | +// ============================================================================= |
| 85 | +// Tests |
| 86 | +// ============================================================================= |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + use super::*; |
| 91 | + |
| 92 | + // ---- Windows tests (real DWM API calls) ---- |
| 93 | + |
| 94 | + #[cfg(windows)] |
| 95 | + mod windows_tests { |
| 96 | + use super::*; |
| 97 | + use windows::Win32::Foundation::HWND; |
| 98 | + use windows::Win32::UI::WindowsAndMessaging::{ |
| 99 | + CreateWindowExW, DestroyWindow, RegisterClassW, CS_HREDRAW, CS_VREDRAW, |
| 100 | + WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_OVERLAPPEDWINDOW, WNDCLASSW, |
| 101 | + }; |
| 102 | + use windows::core::w; |
| 103 | + use std::sync::Once; |
| 104 | + |
| 105 | + static REGISTER_CLASS: Once = Once::new(); |
| 106 | + const CLASS_NAME: &str = "DesktopAICloakTest\0"; |
| 107 | + |
| 108 | + fn register_test_class() { |
| 109 | + REGISTER_CLASS.call_once(|| unsafe { |
| 110 | + let class_name = w!("DesktopAICloakTest"); |
| 111 | + let wc = WNDCLASSW { |
| 112 | + style: CS_HREDRAW | CS_VREDRAW, |
| 113 | + lpszClassName: class_name.into(), |
| 114 | + ..Default::default() |
| 115 | + }; |
| 116 | + RegisterClassW(&wc); |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + fn create_test_window() -> HWND { |
| 121 | + register_test_class(); |
| 122 | + unsafe { |
| 123 | + CreateWindowExW( |
| 124 | + WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE, |
| 125 | + w!("DesktopAICloakTest"), |
| 126 | + w!("CloakTestWindow"), |
| 127 | + WS_OVERLAPPEDWINDOW, |
| 128 | + 0, 0, 100, 100, |
| 129 | + None, |
| 130 | + None, |
| 131 | + None, |
| 132 | + None, |
| 133 | + ) |
| 134 | + } |
| 135 | + .expect("Failed to create test window") |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_cloak_and_uncloak_round_trip() { |
| 140 | + let hwnd = create_test_window(); |
| 141 | + |
| 142 | + // Cloak the window |
| 143 | + cloak_window(hwnd).expect("cloak_window failed"); |
| 144 | + assert!(is_cloaked(hwnd).expect("is_cloaked failed"), "window should be cloaked"); |
| 145 | + |
| 146 | + // Uncloak the window |
| 147 | + uncloak_window(hwnd).expect("uncloak_window failed"); |
| 148 | + assert!(!is_cloaked(hwnd).expect("is_cloaked failed"), "window should not be cloaked"); |
| 149 | + |
| 150 | + unsafe { let _ = DestroyWindow(hwnd); } |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn test_uncloak_uncloaked_window_is_noop() { |
| 155 | + let hwnd = create_test_window(); |
| 156 | + // Uncloaking an already-visible window should succeed silently |
| 157 | + uncloak_window(hwnd).expect("uncloak on visible window should be Ok"); |
| 158 | + assert!(!is_cloaked(hwnd).expect("is_cloaked failed")); |
| 159 | + unsafe { let _ = DestroyWindow(hwnd); } |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_cloak_twice_is_idempotent() { |
| 164 | + let hwnd = create_test_window(); |
| 165 | + cloak_window(hwnd).expect("first cloak failed"); |
| 166 | + cloak_window(hwnd).expect("second cloak should also succeed"); |
| 167 | + assert!(is_cloaked(hwnd).expect("is_cloaked failed")); |
| 168 | + uncloak_window(hwnd).expect("cleanup uncloak failed"); |
| 169 | + unsafe { let _ = DestroyWindow(hwnd); } |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn test_is_cloaked_on_new_window() { |
| 174 | + let hwnd = create_test_window(); |
| 175 | + assert!(!is_cloaked(hwnd).expect("is_cloaked failed"), "fresh window should not be cloaked"); |
| 176 | + unsafe { let _ = DestroyWindow(hwnd); } |
| 177 | + } |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn test_invalid_hwnd_returns_error() { |
| 181 | + let bad_hwnd = HWND(0xDEAD as *mut _); |
| 182 | + assert!(cloak_window(bad_hwnd).is_err(), "cloaking invalid HWND should fail"); |
| 183 | + assert!(is_cloaked(bad_hwnd).is_err(), "querying invalid HWND should fail"); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // ---- Non-Windows tests (stub error paths) ---- |
| 188 | + |
| 189 | + #[cfg(not(windows))] |
| 190 | + mod non_windows_tests { |
| 191 | + use super::*; |
| 192 | + |
| 193 | + #[test] |
| 194 | + fn test_cloak_fails_on_non_windows() { |
| 195 | + let result = cloak_window(0); |
| 196 | + assert!(result.is_err()); |
| 197 | + assert_eq!(result.unwrap_err(), "DWM cloaking requires Windows"); |
| 198 | + } |
| 199 | + |
| 200 | + #[test] |
| 201 | + fn test_uncloak_fails_on_non_windows() { |
| 202 | + let result = uncloak_window(0); |
| 203 | + assert!(result.is_err()); |
| 204 | + assert_eq!(result.unwrap_err(), "DWM cloaking requires Windows"); |
| 205 | + } |
| 206 | + |
| 207 | + #[test] |
| 208 | + fn test_is_cloaked_fails_on_non_windows() { |
| 209 | + let result = is_cloaked(0); |
| 210 | + assert!(result.is_err()); |
| 211 | + assert_eq!(result.unwrap_err(), "DWM cloaking requires Windows"); |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments