如何将WPF窗口移动到负的顶部值?

5
如果我使用dragMove,WPF窗口将无法移动到y值为负的位置。但是,我可以将窗口的顶部值设置为负值。有没有简单的方法启用dragMove,允许窗口的顶部移动到显示器0位置以上?
编辑:
看起来这是moveWindow的默认窗口处理方式。通过调用SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE, null)进行验证。
1个回答

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消息,那么除非按下鼠标左键,否则无法移动窗口。这包括将窗口最大化以及程序改变窗口位置等情况,所以如果需要其他行为,则需要调整代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接