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?
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!
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With