在WPF中使用可预览大小更改的边框调整自定义窗口大小

mcvgt66p  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(99)

我有一些麻烦doind自定义窗口大小调整行为。我需要的是,当你调整窗口大小(从边框拖动),它必须显示一个调整大小的边框,根据鼠标位置改变其大小,当鼠标左键单击被释放,它应用到窗口的新大小,像Skype一样。
我有这样的东西:
Windows 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;
        }
    }
}

有了这个,当鼠标被释放的时候,调整大小就完成了,但是现在我不知道如何添加可调整大小的边框。顺便说一下,我正在使用.NET 4.5与MVVM。非常感谢

nmpmafwu

nmpmafwu1#

<Window WindowStyle="None"
        AllowsTransparency="True"
        Background="Transparent" 
        ResizeMode="CanResize"
        Title="WareHouse" 
        Height="600" 
        Width="1000"
        >
    
    <WindowChrome.WindowChrome>
        <WindowChrome/>   
    </WindowChrome.WindowChrome>
</Window>

WindowsChrome将允许您调整自定义窗口的大小。

相关问题