Skip to content

win32: fix cursor coordinates when starting window dragging - #4765

Closed
smittie2000 wants to merge 1 commit into
rust-windowing:masterfrom
smittie2000:codex/fix-win32-drag-coordinates
Closed

smittie2000 wants to merge 1 commit into
rust-windowing:masterfrom
smittie2000:codex/fix-win32-drag-coordinates

Conversation

@smittie2000

@smittie2000 smittie2000 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

handle_os_dragging posted WM_NCLBUTTONDOWN with &points as *const _ as LPARAM,
which passes the address of the POINTS struct rather than the coordinates.
Windows reads the coordinates from the bits of lParam itself (low word x, high
word y, each a signed 16-bit value, as decoded by GET_X_LPARAM/GET_Y_LPARAM).

This packs the cursor position into lParam directly. Each coordinate is
truncated to u16 first, so negative positions on multi-monitor setups survive
the round trip. This affects both Window::drag_window and
Window::drag_resize_window.

Tested on Windows 10 x64: inspecting the posted message for a move and a
south-east resize shows the wrong coordinates before this change and the correct
ones after.

See WM_NCLBUTTONDOWN.

v0.30.x still posts the address of POINTS here. I'm happy to open a backport if that's wanted.

@smittie2000 smittie2000 changed the title Fix Win32 cursor coordinates when starting window dragging win32: fix cursor coordinates when starting window dragging Oct 15, 2026
@ogoffart

Copy link
Copy Markdown
Contributor

Looks sensible.
Have you considered using transmute to convert from POINT to LPARAM?

@smittie2000

Copy link
Copy Markdown
Contributor Author

I did not think of that.
I am assuming you meant POINTS as POINT holds two 32bit value so transmute to LPARAM will put y in the wrong place for 64bit systems and not compile on 32bit system

Let me know if i change it. Something in the lines of

let position = unsafe { mem::transmute::<POINTS, u32>(points) } as LPARAM;

@smittie2000
smittie2000 force-pushed the codex/fix-win32-drag-coordinates branch from 3d330f0 to 1449a93 Compare October 22, 2026 10:29
@smittie2000
smittie2000 force-pushed the codex/fix-win32-drag-coordinates branch from 9d43d18 to 3e577b0 Compare November 8, 2026 06:59
@smittie2000

Copy link
Copy Markdown
Contributor Author

@ogoffart I'd prefer to keep the explicit packing since it avoids unsafe and doesn't rely on POINTS's layout, but happy to switch to transmute if you prefer.

@smittie2000
smittie2000 force-pushed the codex/fix-win32-drag-coordinates branch from 6727faa to 82d656a Compare November 8, 2026 08:05
@smittie2000

Copy link
Copy Markdown
Contributor Author

Closing in favour of #4825, which now includes this commit unchanged. Both fixes edit the same PostMessageW call in handle_os_dragging, so a single tested PR avoids the merge conflict between them.

@ogoffart thanks for the review. I kept the explicit packing there rather than transmute.

@smittie2000 smittie2000 closed this Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants