Skip to content

Commit

Permalink
Use more accurate DwmGetWindowAttribute for getting window bounds on …
Browse files Browse the repository at this point in the history
…Windows
  • Loading branch information
Zachary Canann committed Dec 31, 2024
1 parent 92cabc5 commit 0be299e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sysinfo = "0.30.0"
windows-capture = "1.3.6"
windows = { version = "0.58", features = [
"Win32_Foundation",
"Win32_Graphics_Dwm",
"Win32_Graphics_Gdi",
"Win32_UI_HiDpi",
"Win32_UI_WindowsAndMessaging",
Expand Down
21 changes: 18 additions & 3 deletions src/targets/win/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::{Display, Target};
use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_EXTENDED_FRAME_BOUNDS};
use windows::Win32::UI::HiDpi::{GetDpiForMonitor, GetDpiForWindow, MDT_EFFECTIVE_DPI};
use windows::Win32::UI::WindowsAndMessaging::GetWindowRect;
use windows::Win32::{
Foundation::{HWND, RECT},
Graphics::Gdi::HMONITOR,
Expand Down Expand Up @@ -87,7 +89,20 @@ pub fn get_target_dimensions(target: &Target) -> (u64, u64) {

// get width and height of the window
let mut rect = RECT::default();
let _ = windows::Win32::UI::WindowsAndMessaging::GetWindowRect(hwnd, &mut rect);

// Try DwmGetWindowAttribute first for accurate bounds without shadows
let result = DwmGetWindowAttribute(
hwnd,
DWMWA_EXTENDED_FRAME_BOUNDS,
&mut rect as *mut RECT as *mut _,
std::mem::size_of::<RECT>() as u32,
);

// Fall back to GetWindowRect if DwmGetWindowAttribute fails
if result.is_err() {
let _ = GetWindowRect(hwnd, &mut rect);
}

let width = rect.right - rect.left;
let height = rect.bottom - rect.top;

Expand All @@ -97,8 +112,8 @@ pub fn get_target_dimensions(target: &Target) -> (u64, u64) {
let monitor = Monitor::from_raw_hmonitor(display.raw_handle.0);

(
monitor.width().unwrap() as u64,
monitor.height().unwrap() as u64,
monitor.width().unwrap_or_default() as u64,
monitor.height().unwrap_or_default() as u64,
)
}
}
Expand Down

0 comments on commit 0be299e

Please sign in to comment.