如何将wpf窗口移动到负顶值?

时间:2008-11-29 22:53:20

标签: wpf

如果我使用dragMove,wpf窗口将不会移动到y值为负的位置。但是,我可以将窗口顶部值设置为负值。有没有一种简单的方法可以让dragMove允许窗口顶部移动到显示0位置上方?

编辑:

这似乎是默认窗口对moveWindow的处理。通过调用SendMessage验证(hwnd,WM_SYSCOMMAND,SC_MOVE,null);

1 个答案:

答案 0 :(得分:6)

正如我发现的那样,窗口将移动到“负”位置,但随后将跳回。为了防止这种情况,您可以执行以下操作:

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));
    }        
}

以这种方式处理WM_WINDOWPOSCHANGING将阻止窗口的任何移动,除非按下鼠标左键。这包括最大化窗口和窗口位置的程序化更改,因此如果您需要其他行为,则必须调整代码。