DragMove() and Maximize

wpf windowstyle=none drag
how to move window in wpf

I have a problem with my custom window (AllowTransparency, WindowStyle=None) in WPF. DragMove() method works good, but when I maximize window, or it maximizing automatically by Windows 7 Aero Snap, this method does not work at all. So I can't unsnap window with mouse drag and return it state to WindowState.Normal. Left and Right Aero Snap works good, I can snap and unsnap window without a problem. But when it maximized, nothing works except Win+Down combination. Maybe somebody knows how to solve this problem or where can I find other ways to do proper DragMove of custom window with working Aero Snap features?

Here is my method. Try make it shorter )))

private void InitHeader()
{
    var border = Find<Border>("borderHeader");
    var restoreIfMove = false;

    border.MouseLeftButtonDown += (s, e) =>
    {
        if (e.ClickCount == 2)
        {
            if ((ResizeMode == ResizeMode.CanResize) ||
                (ResizeMode == ResizeMode.CanResizeWithGrip))
            {
                SwitchState();
            }
        }
        else
        {
            if (WindowState == WindowState.Maximized)
            {
                restoreIfMove = true;
            }

            DragMove();
        }
    };
    border.MouseLeftButtonUp += (s, e) =>
    {
        restoreIfMove = false;
    };
    border.MouseMove += (s, e) =>
    {
        if (restoreIfMove)
        {
            restoreIfMove = false;
            var mouseX = e.GetPosition(this).X;
            var width = RestoreBounds.Width;
            var x = mouseX - width / 2;

            if (x < 0)
            {
                x = 0;
            }
            else
            if (x + width > screenSize.X)
            {
                x = screenSize.X - width;
            }

            WindowState = WindowState.Normal;
            Left = x;
            Top = 0;
            DragMove();
        }
    };
}

private void SwitchState()
{
    switch (WindowState)
    {
        case WindowState.Normal:
        {
            WindowState = WindowState.Maximized;
            break;
        }
        case WindowState.Maximized:
        {
            WindowState = WindowState.Normal;
            break;
        }
    }
}

(To get screenSize I use native methods)

How to move(Drag and Drop) wpf window when windows is , What you could do is not Maximize the window but make the window size to I am implementing DragMove() method but its not working when� WM_SYSCOMMAND message. 07/27/2020; 3 minutes to read; In this article. A window receives this message when the user chooses a command from the Window menu (formerly known as the system or control menu) or when the user chooses the maximize button, minimize button, restore button, or close button.

Groaner's solution does not function properly with multiple monitor setups, especially where the primary monitor is not the left-most.

Here is my solution based on his which handles single or multiple monitor setups properly. In this code 'rctHeader' is a Rectangle defined in the XAML.

    private bool mRestoreIfMove = false;


    public MainWindow()
    {
        InitializeComponent();
    }


    private void SwitchWindowState()
    {
        switch (WindowState)
        {
            case WindowState.Normal:
                {
                    WindowState = WindowState.Maximized;
                    break;
                }
            case WindowState.Maximized:
                {
                    WindowState = WindowState.Normal;
                    break;
                }
        }
    }


    private void rctHeader_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount == 2)
        {
            if ((ResizeMode == ResizeMode.CanResize) || (ResizeMode == ResizeMode.CanResizeWithGrip))
            {
                SwitchWindowState();
            }

            return;
        }

        else if (WindowState == WindowState.Maximized)
        {
            mRestoreIfMove = true;
            return;
        }

        DragMove();
    }


    private void rctHeader_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        mRestoreIfMove = false;
    }


    private void rctHeader_MouseMove(object sender, MouseEventArgs e)
    {
        if (mRestoreIfMove)
        {
            mRestoreIfMove = false;

            double percentHorizontal = e.GetPosition(this).X / ActualWidth;
            double targetHorizontal = RestoreBounds.Width * percentHorizontal;

            double percentVertical = e.GetPosition(this).Y / ActualHeight;
            double targetVertical = RestoreBounds.Height * percentVertical;

            WindowState = WindowState.Normal;

            POINT lMousePosition;
            GetCursorPos(out lMousePosition);

            Left = lMousePosition.X - targetHorizontal;
            Top = lMousePosition.Y - targetVertical;

            DragMove();
        }
    }



    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetCursorPos(out POINT lpPoint);


    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int X;
        public int Y;

        public POINT(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
    }


}

How to do Minimize, Maximize and DragMove � Issue #75 � canton7 , Is this correct way of doing? public void DragMove() { ((Window)this.View). DragMove(); }. @canton7 Just one request, it would be helpful� Also, I want to call the DragMove function in ViewModel. You'll either have to access .View directly in the ViewModel, or bind something through to either a DP on the ViewModel or on an attached property / behaviour which signals when you want DragMove to be called. (By the way, these are both general WPF questions which aren't specific to Stylet.

In WPF I would highly recommend using Control.PointToScreen when restoring the window before your Window.DragMove. PointToScreen will also handle multiple monitor setups. This would simplify the restore to the following:

    private void OnMouseLeftButtonDown( object sender, MouseButtonEventArgs e )
    {
        if( e.ClickCount == 2 )
        {
            if( ResizeMode != ResizeMode.CanResize && 
                ResizeMode != ResizeMode.CanResizeWithGrip )
            {
                return;
            }

            WindowState = WindowState == WindowState.Maximized
                ? WindowState.Normal
                : WindowState.Maximized;
        }
        else
        {
            mRestoreForDragMove = WindowState == WindowState.Maximized;
            DragMove();
        }
    }

    private void OnMouseMove( object sender, MouseEventArgs e )
    {
        if( mRestoreForDragMove )
        {
            mRestoreForDragMove = false;

            var point = PointToScreen( e.MouseDevice.GetPosition( this ) );

            Left = point.X - ( RestoreBounds.Width * 0.5 );
            Top = point.Y;

            WindowState = WindowState.Normal;

            DragMove();
        }
    }

    private void OnMouseLeftButtonUp( object sender, MouseButtonEventArgs e )
    {
        mRestoreForDragMove = false;
    }

    private bool mRestoreForDragMove;

wpf - DragMove() и Maximize, Вот мой метод. Постарайтесь сделать его короче))) private void InitHeader() { var border = Find<Border>("borderHeader"); var restoreIfMove = false; border. Hi Angelica, For Minimize/Maximize, use: this.WindowState = WindowState.Minimized; this.WindowState = WindowState.Maximized; For moving window: I guess you have some kind of a title for your window, let's say you use a border as a title So, capture the MouseMove="Border_MouseMove" event and write this in the handler:

A little late for another answer, but my code was simpler so I'll put it here. As you men the left and right snapping works just fine but when the window is Maximized or snaps to upper screen bounds and maximizes, the DragMove method will not work!


Just handle the Mouse_Down Event on the element you want to drag with like this:

private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (WindowState == WindowState.Maximized)
    {
        var point = PointToScreen(e.MouseDevice.GetPosition(this));

        if (point.X <= RestoreBounds.Width / 2)
            Left = 0;

        else if (point.X >= RestoreBounds.Width)
            Left = point.X - (RestoreBounds.Width - (this.ActualWidth - point.X));

        else
            Left = point.X - (RestoreBounds.Width / 2);

        Top = point.Y - (((FrameworkElement)sender).ActualHeight / 2);
        WindowState = WindowState.Normal;
    }
    DragMove(); 
}

I hope it helps someone!

Custom WPF Window with Windows System Functionality Restored , private void ShowRestoreDownButton() { ButtonMaximize.Visibility true); ShowMaximumWindowButton(); DragMove(); } else { DragMove(); }� Here I use a handler called a MouseDown event for calling a DragMove method to make a window drag-gable. private void rectangle2_MouseDown(object sender, MouseButtonEventArgs e) { this.DragMove(); } Step 1 To create a WPF application, use the following procedure: Open the Visual Studio. Select the C# language and "WPF" Application.

The DragMove() method only works in the titlebar of the form so use:

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

public static void StartDrag(Window window)
{
    WindowInteropHelper helper = new WindowInteropHelper(window);
    SendMessage(helper.Handle, 161, 2, 0);
}

Dont forget to add System.Windows.Interop

C#, DragMove();//Here is where I do the drag move } } } /// <summary> /// Adjusts the static void Maximize() { AdjustWindowSize(); } /// <summary> /// Minimized� Recommend:c# - Set wpf application screen height on secondary monitor on maximize. 00, secondary - 1920*1080 My application is working fine on primary screen, but when i drag the application on secondary screen and maximize ,it maximize only as per primary screen height. I want the application screen size as per current s

overwolf.windows API � Overwolf, When you dragMove a native window between monitors with different DPIs, the If you would like to "unmaximize" the window after calling "maximize()", you� Disable window maximize; The DragMove() method of the Window class is used to move the mouse to drag the window after the left mouse button is pressed.

Borderless and Draggable Window in WPF, Here I use a handler called a MouseDown event for calling a DragMove method to make a window drag-gable. public MainWindow() and if we click on the " maximize [[]] icon it maximizes the window so that it looks as: 1. Why Customize Window. On a little scale desktop software, custom Window is almost a standard, a designer is always to restrain myself think software is more personalized, in order to modify the Window UI harmony is also necessary; secondly multi-line the space can add a lot of functionality, especially on the edges, because the screen is locked move the mouse on the edge of the button so

DragMove()和Maximize, DragMove()方法效果很好,但是当我最大化窗口,或者它通过Windows 7 Aero Normal; Left = x; Top = 0; DragMove(); } }; } private void SwitchState() { switch� if the wpf form needs to be draggable no matter where it was clicked the easy work around is using a delegate to trigger the DragMove() method on either the windows onload event or the grid load event. private void Grid_Loaded(object sender, RoutedEventArgs { this.MouseDown += delegate{DragMove();}; }

Comments
  • @groaner can you point to the screenSize native method you used?
  • I need an evaluation of if Mouse.LeftButton == MouseButtonState.Pressed every time I do DragMove(); to avoid "Can only call DragMove when primary mouse button is down". Also, WindowState should be set to WindowState.Normal before evaluating the new x position (var x = mouseX - width / 2; line). With these modifications works perfect, thanks!
  • Works best in combination with Anthony M's solution. Thanks!
  • This was really helpful! Thanks.
  • This solution works perfectly, exactly what I was looking for. Thank you for sharing.
  • Compared to the other solutions this is working the best - it has its minor flaws but it works really good nonetheless.
  • Ocasionally it happens that the window gets back to a normal state but does not anchor to the mouse which is really irritating.
  • I'm sorry but the blog post is almost useless, it actually requires to rip apart the entire project you mention in the blog post and get out what is needed.