DragMove() and Maximize

前端 未结 6 1203
误落风尘
误落风尘 2020-12-16 01:32

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

6条回答
  •  再見小時候
    2020-12-16 01:57

    A little late for another answer, but my code was simpler so I'll put it here. 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! So here is what I did:


    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!

提交回复
热议问题