如何将 wpf 窗口移动到负的最高值? [英] How do I move a wpf window into a negative top value?

查看:26
本文介绍了如何将 wpf 窗口移动到负的最高值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

推荐答案

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

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

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

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.

这篇关于如何将 wpf 窗口移动到负的最高值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆