use windows_sys::Win32::{
Foundation::{HWND, LPARAM},
Graphics::Dwm::{DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_ROUND, DwmSetWindowAttribute},
System::Threading::GetCurrentProcessId,
UI::WindowsAndMessaging::{
EnumWindows, GetWindowLongW, GetWindowThreadProcessId, IsWindowVisible, WS_CHILD,
},
};
use crate::error::{Error, Result};
pub fn round_corners() -> Result<()> {
let window = main_window().ok_or_else(|| Error::saying("EnumWindows", "no window of ours"))?;
let preference = DWMWCP_ROUND;
let status = unsafe {
DwmSetWindowAttribute(
window,
DWMWA_WINDOW_CORNER_PREFERENCE as u32,
std::ptr::from_ref(&preference).cast(),
size_of_val(&preference) as u32,
)
};
if status < 0 {
return Err(Error::hresult("DwmSetWindowAttribute", status));
}
Ok(())
}
fn main_window() -> Option<HWND> {
let mut found: HWND = std::ptr::null_mut();
unsafe {
EnumWindows(Some(consider), std::ptr::from_mut(&mut found) as LPARAM);
}
(!found.is_null()).then_some(found)
}
unsafe extern "system" fn consider(window: HWND, out: LPARAM) -> i32 {
const KEEP_LOOKING: i32 = 1;
const STOP: i32 = 0;
unsafe {
let mut owner = 0u32;
GetWindowThreadProcessId(window, &mut owner);
if owner != GetCurrentProcessId() || IsWindowVisible(window) == 0 {
return KEEP_LOOKING;
}
if GetWindowLongW(window, GWL_STYLE) & WS_CHILD as i32 != 0 {
return KEEP_LOOKING;
}
*(out as *mut HWND) = window;
}
STOP
}
const GWL_STYLE: i32 = -16;