简体   繁体   中英

Move a borderless window in wpf

In my C# WinForms app I have a main window that has its default controls hidden.

So to allow me to move it around I added the following to the main 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. I see that the main window is derived from a 'Window' so the above code does not work. How do I do this in 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.

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.

The code is kinda extensive. It deals with cursor, messages to WndProc and all. I'll leave with a link that explains it.

WPF Borderless window resize

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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