Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I move a wpf window into a negative top value?

Tags:

wpf

If I use dragMove the wpf window will not move to a location where the y value is negative. I can however set the windows top value to a negative value. Is there a simple way to enable dragMove to allow the top of the window to be moved above the displays 0 position?

Edit:

It seems that this is the default window's handling of moveWindow. Verified with a call to SendMessage( hwnd, WM_SYSCOMMAND, SC_MOVE, null);

like image 684
Aaron Fischer Avatar asked Nov 29 '08 22:11

Aaron Fischer


1 Answers

As I found, the window will move to "negative" location, but will then jump back. To prevent this you could 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));
    }        
}

Handling WM_WINDOWPOSCHANGING this way will prevent any movement of the window unless left mouse button is pressed. This includes maximizing the window and programmatic change of window position as well, so you'll have to adapt the code if you need another behavior.

like image 126
Stanislav Kniazev Avatar answered Sep 24 '22 02:09

Stanislav Kniazev