Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more accurate DwmGetWindowAttribute for getting window bounds on Windows #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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