我在修改自定义窗口大小调整行为方面遇到了一些麻烦。我需要的是当你调整窗口大小(从边框拖动)时,它必须显示一个调整大小的边框,根据鼠标位置改变其大小,当鼠标左键单击被释放时,它会将新大小应用于窗口,如Skype确实
我拥有的是:
Window XAML:
<Window x:Class="View.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:sw="clr-namespace:View.WindowStyle"
Title="CustomWindow" Height="600" Width="800"
MinWidth="800"
MinHeight="600"
WindowStartupLocation="CenterScreen">
<Grid>
<!-- windows items -->
<Thumb x:Name="ThumbTop" Height="6" Margin="8,0" VerticalAlignment="Top" Cursor="SizeNS" Opacity="0" sw:WindowResizeBehavior.TopResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
<Thumb x:Name="ThumbTopLeft" Height="8" Width="8" VerticalAlignment="Top" HorizontalAlignment="Left" Cursor="SizeNWSE" Opacity="0" sw:WindowResizeBehavior.TopLeftResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
<Thumb x:Name="ThumbBottom" Height="6" Margin="8,0" VerticalAlignment="Bottom" Cursor="SizeNS" Opacity="0" sw:WindowResizeBehavior.BottomResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
<Thumb x:Name="ThumbTopRight" Height="8" Width="8" VerticalAlignment="Top" HorizontalAlignment="Right" Cursor="SizeNESW" Opacity="0" sw:WindowResizeBehavior.TopRightResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
<Thumb x:Name="ThumbRight" HorizontalAlignment="Right" Margin="0,6" Width="6" Cursor="SizeWE" Opacity="0" sw:WindowResizeBehavior.RightResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
<Thumb x:Name="ThumbBottomRight" Height="8" Width="8" VerticalAlignment="Bottom" HorizontalAlignment="Right" Cursor="SizeNWSE" Opacity="0" sw:WindowResizeBehavior.BottomRightResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
<Thumb x:Name="ThumbLeft" HorizontalAlignment="Left" Margin="0,8" Width="6" Cursor="SizeWE" Opacity="0" sw:WindowResizeBehavior.LeftResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
<Thumb x:Name="ThumbBottomLeft" Height="8" Width="8" VerticalAlignment="Bottom" HorizontalAlignment="Left" Cursor="SizeNESW" Opacity="0" sw:WindowResizeBehavior.BottomLeftResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
</Grid>
</Window>
我使用8个大拇指来处理调整大小的行为,这是在一个类中处理的,这是调整大小的方式(仅适用于一个拇指,但其他7个逻辑相同):
public static class WindowResizeBehavior
{
public static Window GetBottomRightResize(DependencyObject obj)
{
return (Window)obj.GetValue(BottomRightResize);
}
public static void SetBottomRightResize(DependencyObject obj, Window window)
{
obj.SetValue(BottomRightResize, window);
}
public static readonly DependencyProperty BottomRightResize = DependencyProperty.RegisterAttached("BottomRightResize",
typeof(Window), typeof(WindowResizeBehavior),
new UIPropertyMetadata(null, OnBottomRightResizeChanged));
private static void OnBottomRightResizeChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var thumb = sender as Thumb;
if (thumb != null)
{
thumb.DragCompleted += DragBottomRight;
}
}
private static void DragBottomRight(object sender, DragCompletedEventArgs e)
{
var thumb = sender as Thumb;
var window = thumb.GetValue(BottomRightResize) as Window;
if (window != null)
{
var verticalChange = window.SafeHeightChange(e.VerticalChange);
var horizontalChange = window.SafeWidthChange(e.HorizontalChange);
window.Width += horizontalChange;
window.Height += verticalChange;
}
}
private static double SafeWidthChange(this Window window, double change, bool positive = true)
{
var result = positive ? window.Width + change : window.Width - change;
if (result <= window.MinWidth)
{
if (positive)
return window.MinWidth - window.Width;
else
return window.Width - window.MinWidth;
} else if(result >= window.MaxWidth)
{
return 0;
} else if(result < 0)
{
return 0;
}
else
{
return change;
}
}
private static double SafeHeightChange(this Window window, double change, bool positive = true)
{
var result = positive ? window.Height + change : window.Height - change;
if (result <= window.MinHeight)
{
if (positive)
return window.MinHeight - window.Height;
else
return window.Height - window.MinHeight;
}
else if (result >= window.MaxHeight)
{
return 0;
}
else if (result < 0)
{
return 0;
}
else
{
return change;
}
}
}
这样就可以在鼠标释放时完成调整,但现在我不知道如何添加可调整大小的边框。顺便说一句,我正在使用带有MVVM的.NET 4.5。 非常感谢