Skip to content

Commit 0d74380

Browse files
author
Zachary Canann
committed
Use more accurate DwmGetWindowAttribute for getting window bounds on Windows
1 parent 92cabc5 commit 0d74380

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ sysinfo = "0.30.0"
2222
windows-capture = "1.3.6"
2323
windows = { version = "0.58", features = [
2424
"Win32_Foundation",
25+
"Win32_Graphics_Dwm",
2526
"Win32_Graphics_Gdi",
2627
"Win32_UI_HiDpi",
2728
"Win32_UI_WindowsAndMessaging",

src/targets/win/mod.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::{Display, Target};
2+
use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_EXTENDED_FRAME_BOUNDS};
23
use windows::Win32::UI::HiDpi::{GetDpiForMonitor, GetDpiForWindow, MDT_EFFECTIVE_DPI};
4+
use windows::Win32::UI::WindowsAndMessaging::GetWindowRect;
35
use windows::Win32::{
46
Foundation::{HWND, RECT},
57
Graphics::Gdi::HMONITOR,
@@ -84,10 +86,21 @@ pub fn get_target_dimensions(target: &Target) -> (u64, u64) {
8486
match target {
8587
Target::Window(window) => unsafe {
8688
let hwnd = window.raw_handle;
87-
88-
// get width and height of the window
8989
let mut rect = RECT::default();
90-
let _ = windows::Win32::UI::WindowsAndMessaging::GetWindowRect(hwnd, &mut rect);
90+
91+
// Try DwmGetWindowAttribute first for accurate bounds without shadows
92+
let result = DwmGetWindowAttribute(
93+
hwnd,
94+
DWMWA_EXTENDED_FRAME_BOUNDS,
95+
&mut rect as *mut RECT as *mut _,
96+
std::mem::size_of::<RECT>() as u32,
97+
);
98+
99+
// Fall back to GetWindowRect if DwmGetWindowAttribute fails
100+
if result.is_err() {
101+
let _ = GetWindowRect(hwnd, &mut rect);
102+
}
103+
91104
let width = rect.right - rect.left;
92105
let height = rect.bottom - rect.top;
93106

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

99112
(
100-
monitor.width().unwrap() as u64,
101-
monitor.height().unwrap() as u64,
113+
monitor.width().unwrap_or_default() as u64,
114+
monitor.height().unwrap_or_default() as u64,
102115
)
103116
}
104117
}

0 commit comments

Comments
 (0)