Skip to content

Commit 0be299e

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

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
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 & 3 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,
@@ -87,7 +89,20 @@ pub fn get_target_dimensions(target: &Target) -> (u64, u64) {
8789

8890
// get width and height of the window
8991
let mut rect = RECT::default();
90-
let _ = windows::Win32::UI::WindowsAndMessaging::GetWindowRect(hwnd, &mut rect);
92+
93+
// Try DwmGetWindowAttribute first for accurate bounds without shadows
94+
let result = DwmGetWindowAttribute(
95+
hwnd,
96+
DWMWA_EXTENDED_FRAME_BOUNDS,
97+
&mut rect as *mut RECT as *mut _,
98+
std::mem::size_of::<RECT>() as u32,
99+
);
100+
101+
// Fall back to GetWindowRect if DwmGetWindowAttribute fails
102+
if result.is_err() {
103+
let _ = GetWindowRect(hwnd, &mut rect);
104+
}
105+
91106
let width = rect.right - rect.left;
92107
let height = rect.bottom - rect.top;
93108

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

99114
(
100-
monitor.width().unwrap() as u64,
101-
monitor.height().unwrap() as u64,
115+
monitor.width().unwrap_or_default() as u64,
116+
monitor.height().unwrap_or_default() as u64,
102117
)
103118
}
104119
}

0 commit comments

Comments
 (0)