Home > OS >  Why does my maximize and DragMove not work in my MouseDown Event?
Why does my maximize and DragMove not work in my MouseDown Event?

Time:09-16

So I have an Borderless Window and have in an mousedown event the function, that when double click on the Header, the Window maximize and when i double click on it again it minimize. That is working so far but when I now whant to add the function of DragMove when the Window is in the normal state, but when i finish dragMove it automaticly become maximize. How can I prevent this?

My MouseDown:

 private void TopBorder_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {


        if (e.ButtonState == MouseButtonState.Pressed)
        {
            this.DragMove();
        }


        if (e.ClickCount == 2 && this.WindowState == WindowState.Maximized)
        {
            this.WindowState = WindowState.Normal;

        }
        else
        {
            this.WindowState = WindowState.Maximized;
        }

}

CodePudding user response:

You need something to know if the window drag has happened. There maybe other approaches but I usually use a bool. You can use the MouseMove event instead of LocationChanged

bool windowMoved = false;

Then for code behind,

this.LocationChanged  = (s,e) => { windowMoved = true; };

Or from XAML

WindowStyle="None" LocationChanged="Window_LocationChanged"
    MouseDown="Window_MouseDown"
    Title="MainWindow">

and

private void Window_LocationChanged(object sender, EventArgs e)
    {
         windowMoved = true;
    }

and finally

 private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        windowMoved = false;
        if (e.ButtonState == MouseButtonState.Pressed)
        {
            this.DragMove();
        }

        if (windowMoved)
            return;

        if (e.ClickCount == 2 && this.WindowState == WindowState.Maximized)
        {
            this.WindowState = WindowState.Normal;

        }
        else
        {
            this.WindowState = WindowState.Maximized;
        }
    }
  • Related