How to move wpf window to negative top value?

If I use dragMove, the wpf window will not move to the place where the y value is negative. However, I can set the top value of the window to a negative value. Is there an easy way to enable dragMove so that the top of the window is moved above display position 0?

Edit:

This seems to be the default window processing for moveWindow. Confirmed by a call to SendMessage (hwnd, WM_SYSCOMMAND, SC_MOVE, null);

+4
source share
1 answer

As I found, the window will move to the "negative" place, but then bounce back. To prevent this, you can do something like:

public partial class Window1: Window { public Window1() { InitializeComponent(); } private void Window_MouseDown(object sender, MouseButtonEventArgs e) { DragMove(); } public struct WINDOWPOS { public IntPtr hwnd; public IntPtr hwndInsertAfter; public int x; public int y; public int cx; public int cy; public UInt32 flags; }; private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch(msg) { case 0x46://WM_WINDOWPOSCHANGING if(Mouse.LeftButton != MouseButtonState.Pressed) { WINDOWPOS wp = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS)); wp.flags = wp.flags | 2; //SWP_NOMOVE Marshal.StructureToPtr(wp, lParam, false); } break; } return IntPtr.Zero; } private void Window_Loaded(object sender, RoutedEventArgs e) { HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); source.AddHook(new HwndSourceHook(WndProc)); } } 

Processing WM_WINDOWPOSCHANGING this way will prevent any window movement if the left mouse button is not pressed. This includes maximizing the window and programmatically changing the position of the window, so you have to adapt the code if you need other behavior.

+6
source

All Articles