Skip to content

Commit b2742ce

Browse files
committed
Win: Fix drag mouse position for parented windows
1 parent 3dcd681 commit b2742ce

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/win/drop_target.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use windows::Win32::System::SystemServices::MODIFIERKEYS_FLAGS;
1111
use windows_core::Ref;
1212
use windows_sys::Win32::{
1313
Foundation::POINT, Graphics::Gdi::ScreenToClient, UI::Shell::DragQueryFileW,
14+
UI::WindowsAndMessaging::GetCursorPos,
1415
};
1516

1617
use crate::{DropData, DropEffect, Event, EventStatus, MouseEvent, PhyPoint, Point};
@@ -61,14 +62,25 @@ impl DropTarget {
6162
}
6263
}
6364

64-
fn parse_coordinates(&self, pt: POINTL) {
65+
fn parse_coordinates(&self, _pt: POINTL) {
6566
let Some(window_state) = self.window_state.upgrade() else {
6667
return;
6768
};
68-
let mut pt = POINT { x: pt.x, y: pt.y };
69-
unsafe { ScreenToClient(window_state.hwnd, &mut pt as *mut POINT) };
70-
let phy_point = PhyPoint::new(pt.x, pt.y);
71-
self.drag_position.set(phy_point.to_logical(&window_state.window_info()));
69+
// OLE-supplied points can disagree with the actual cursor position for embedded
70+
// child windows (DPI virtualization / DragEnter quirks). Query the cursor directly
71+
// so drag coordinates match WM_MOUSEMOVE.
72+
let mut pt = POINT { x: 0, y: 0 };
73+
unsafe {
74+
GetCursorPos(&mut pt as *mut POINT);
75+
ScreenToClient(window_state.hwnd, &mut pt as *mut POINT);
76+
}
77+
let logical_point = if window_state.has_parent() {
78+
// If the window has a parent, the coordinates are already in logical coordinates
79+
Point::new(pt.x as f64, pt.y as f64)
80+
} else {
81+
PhyPoint::new(pt.x, pt.y).to_logical(&window_state.window_info())
82+
};
83+
self.drag_position.set(logical_point);
7284
}
7385

7486
fn parse_drop_data(&self, data_object: &IDataObject) {

src/win/window.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@ impl WindowState {
532532
self.handler.borrow_mut()
533533
}
534534

535+
pub(super) fn has_parent(&self) -> bool {
536+
self._parent_handle.is_some()
537+
}
538+
535539
/// Handle a deferred task as described in [`Self::deferred_tasks`].
536540
pub(self) fn handle_deferred_task(&self, task: WindowTask) {
537541
match task {

0 commit comments

Comments
 (0)