简体   繁体   English

在 wpf 中移动无边框 window

[英]Move a borderless window in wpf

In my C# WinForms app I have a main window that has its default controls hidden.在我的 C# WinForms 应用程序中,我有一个主 window 隐藏了默认控件。

So to allow me to move it around I added the following to the main window:所以为了让我可以移动它,我在主 window 中添加了以下内容:

private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
private const int WM_NCLBUTTONDBLCLK = 0x00A3;

protected override void WndProc(ref Message message)
{
    if (message.Msg == WM_NCLBUTTONDBLCLK)
    {
        message.Result = IntPtr.Zero;
        return;
    }

    base.WndProc(ref message);

    //Allow window to move
    if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
        message.Result = (IntPtr)HTCAPTION;
}

I have a WPF App where I have also hidden the default controls and I want to do the same.我有一个 WPF 应用程序,我还隐藏了默认控件,我也想做同样的事情。 I see that the main window is derived from a 'Window' so the above code does not work.我看到主要的 window 是从“窗口”派生的,所以上面的代码不起作用。 How do I do this in WPF?如何在 WPF 中执行此操作?

To do this you will want to attach an event handler to the MouseDown event of the window, check that the left mouse button was pressed and call the DragMove method on the window.为此,您需要将事件处理程序附加到窗口的MouseDown事件,检查是否按下了鼠标左键并在窗口上调用DragMove方法。

Here is a sample of a window with this functionality:以下是具有此功能的窗口示例:

public partial class MyWindow : Window
{
    public MyWindow()
    {
        InitializeComponent();
        MouseDown += Window_MouseDown;
    }

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
            DragMove();
    }
}

As I said in your other topic, in my struggle creating a custom window in WPF I found some method online that deals with the Win32 API to Resize the window, JustinM code is right if you want to Drag the Window. 正如我在你的另一个主题中所说,在我在WPF中创建自定义窗口的过程中,我发现了一些在线处理Win32 API以Resize窗口Resize的方法,如果你想拖动窗口,JustinM代码是正确的。

The code is kinda extensive. 代码有点广泛。 It deals with cursor, messages to WndProc and all. 它处理游标,消息到WndProc和所有。 I'll leave with a link that explains it. 我将留下解释它的链接。

WPF Borderless window resize WPF无边框窗口调整大小

It's super simple, here:超级简单,在这里:

private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
    DragMove();
}

First of all, you need to make sure that user wants to drag the window.首先,您需要确保用户想要拖动 window。 So left button of mouse should be down and it should be moving a least some pixels.所以鼠标的左键应该向下并且它应该移动至少一些像素。 Create a point property to store cursor's exact point and add two events to window itself: PreviewMouseLeftButtonDown and MouseMove.创建一个点属性来存储光标的精确点,并向 window 本身添加两个事件:PreviewMouseLeftButtonDown 和 MouseMove。 Check the code below.检查下面的代码。

private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  Position = e.GetPosition(null);
}



private void Window_MouseMove(object sender, MouseEventArgs e)
    {

        Point mousePos = e.GetPosition(null);
        Vector diff = Position - mousePos;

        if (e.LeftButton == MouseButtonState.Pressed &&
            (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
            Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
        {
            DragMove();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM