I have a borderless WPF window (WindowStyle=”None”) that can be moved by catching LeftMouseDown and then call DragMove. That works fine.
However, I want to prevent any part of the window from leaving the screen area.
I catch the LocationChanged but that is called AFTER the window has already moved and thus any changes to the windows position I make in that event cause the window to jump back, which looks very unprofessional.
Is there an event I can catch that is fired BEFORE the window is actually moved.
I tried MouseMove and PreviewMouseMove but they don’t get fired during DragMove.
This stuff works fine with WinForms.
Does anybody have a solution for this or is this just due to the half-baked nature and state of WPF?
I didn’t get the CoerceValueCallback approach to work (if anybody has a solution for this approach please share it).
My solution:
Intercept WM_WINDOWPOSCHANGING message in a WPF WndProc. The solution is a bit long to post here, but here are 2 links that put me on the right track:
How to handle WndProc messages in WPF?
(There were a few nay-sayers in this link that stated that WndProc would not work with WPF.
I can’t comment on the statement in that general fashion but in this particular case it works like a charm.)
http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/
Put body of OnPreviewPositionChange in WndProc.