C# WPF OnDragMoveWindow preforms on just click?

148 views Asked by At

I created a borderless window in WPF. My goal is to have a window with full functionality.
Currently I have one function remaining.

When the windowState is Maximized and I click on the titlebar, the window is set to minimized. This behavior is wrong because the windowState should not minimized unless I double click or if I click or move the cursor.

This is the code I want to only execute if the cursor is moving: (Around 10px from the start position)

private void OnDragMoveWindow(Object sender, MouseButtonEventArgs e)
{
    if (this.InternalWindowState == WindowState.Maximized)
    {
        if (_enableDrag)
        {
            var c = System.Windows.Forms.Cursor.Position;
            this.InternalWindowState = WindowState.Normal;
            this.Height = _location.Height;
            this.Width = _location.Width;
            this.Top = c.Y - (titleBar.ActualHeight / 2);
            this.Left = c.X - (_location.Width / 2);
            this.DragMove();
        }
     }
     else
     {
        this.DragMove();
     }
}

When I double click this code is executed:

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void OnMaximizeWindow(Object sender, MouseButtonEventArgs e)
{
    if (this.InternalWindowState == WindowState.Maximized)
        this.InternalWindowState = WindowState.Normal;
    else
    {
        timer.Interval = 100;
        timer.Tick += timer_Tick;
        timer.Start();
        _enableDrag = false;
        this.InternalWindowState = WindowState.Maximized;
    }
}

My window

2

There are 2 answers

0
GazTheDestroyer On

This question is very confusing, but if you want detect drag actions then you need to compare mouse position between mouse down, and mouse up.

  • If mouse movement < 10 px then you do your click behaviour.
  • If mouse movement > 10 px then you do your drag behaviour.

You can override UIElement.OnPreviewMouseLeftButtonDown and UIElement.OnPreviewMouseLeftButtonUp to capture the mouse movement during the click.

3
sven On

My Solution is :

private void OnDragMoveWindow(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount >= 2)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                var c = e.GetPosition(this);
                this.WindowState = WindowState.Normal;
            }
            else
            {
                this.WindowState = WindowState.Maximized;
            }
        }
        else
        {
            this.DragMove();
        }
       
    }

The Event Source is MouseDown

MouseDown="OnDragMoveWindow"

Yours is not Working because the mousedoubleclick event is after the mouse click event. So your Window state is switching betwenn the states with one "MouseAction".

Replace my -10 constants with your prefered values.