XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;

namespace XFEToolBox.WpfCore.Controls;

/// <summary>
/// 与主界面一致的右下角窗口缩放控件。
/// </summary>
public partial class WindowResizeGrip : UserControl
{
    public WindowResizeGrip()
    {
        InitializeComponent();
        var assemblyName = typeof(WindowResizeGrip).Assembly.GetName().Name;
        CornerImage.Source = new BitmapImage(new Uri(
            $"pack://application:,,,/{assemblyName};component/Resources/Image/corner.png",
            UriKind.Absolute));
    }

    public event EventHandler? ResizeCompleted;

    private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
    {
        var window = Window.GetWindow(this);
        if (window is null
            || window.WindowState != WindowState.Normal
            || window.ResizeMode is ResizeMode.NoResize or ResizeMode.CanMinimize)
            return;

        var currentWidth = double.IsFinite(window.Width) ? window.Width : window.ActualWidth;
        var currentHeight = double.IsFinite(window.Height) ? window.Height : window.ActualHeight;
        window.Width = ClampDimension(
            currentWidth + e.HorizontalChange,
            window.MinWidth,
            window.MaxWidth);
        window.Height = ClampDimension(
            currentHeight + e.VerticalChange,
            window.MinHeight,
            window.MaxHeight);
    }

    private void ResizeThumb_DragCompleted(object sender, DragCompletedEventArgs e)
    {
        if (Window.GetWindow(this) is { WindowState: WindowState.Normal })
            ResizeCompleted?.Invoke(this, EventArgs.Empty);
    }

    private static double ClampDimension(double value, double minimum, double maximum)
    {
        var normalizedMaximum = double.IsFinite(maximum) ? maximum : double.MaxValue;
        return Math.Clamp(value, Math.Max(1, minimum), normalizedMaximum);
    }
}