| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Controls.Primitives;namespace WpfStyleableWindow.StyleableWindow{    public static class WindowResizeBehavior    {        public static Window GetTopLeftResize(DependencyObject obj)        {            return (Window)obj.GetValue(TopLeftResize);        }        public static void SetTopLeftResize(DependencyObject obj, Window window)        {            obj.SetValue(TopLeftResize, window);        }        public static readonly DependencyProperty TopLeftResize = DependencyProperty.RegisterAttached("TopLeftResize",            typeof(Window), typeof(WindowResizeBehavior),            new UIPropertyMetadata(null, OnTopLeftResizeChanged));        private static void OnTopLeftResizeChanged(object sender, DependencyPropertyChangedEventArgs e)        {            var thumb = sender as Thumb;            if (thumb != null)            {                thumb.DragDelta += DragTopLeft;            }        }        public static Window GetTopRightResize(DependencyObject obj)        {            return (Window)obj.GetValue(TopRightResize);        }        public static void SetTopRightResize(DependencyObject obj, Window window)        {            obj.SetValue(TopRightResize, window);        }        public static readonly DependencyProperty TopRightResize = DependencyProperty.RegisterAttached("TopRightResize",            typeof(Window), typeof(WindowResizeBehavior),            new UIPropertyMetadata(null, OnTopRightResizeChanged));        private static void OnTopRightResizeChanged(object sender, DependencyPropertyChangedEventArgs e)        {            var thumb = sender as Thumb;            if (thumb != null)            {                thumb.DragDelta += DragTopRight;            }        }        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.DragDelta += DragBottomRight;            }        }        public static Window GetBottomLeftResize(DependencyObject obj)        {            return (Window)obj.GetValue(BottomLeftResize);        }        public static void SetBottomLeftResize(DependencyObject obj, Window window)        {            obj.SetValue(BottomLeftResize, window);        }        public static readonly DependencyProperty BottomLeftResize = DependencyProperty.RegisterAttached("BottomLeftResize",            typeof(Window), typeof(WindowResizeBehavior),            new UIPropertyMetadata(null, OnBottomLeftResizeChanged));        private static void OnBottomLeftResizeChanged(object sender, DependencyPropertyChangedEventArgs e)        {            var thumb = sender as Thumb;            if (thumb != null)            {                thumb.DragDelta += DragBottomLeft;            }        }        public static Window GetLeftResize(DependencyObject obj)        {            return (Window)obj.GetValue(LeftResize);        }        public static void SetLeftResize(DependencyObject obj, Window window)        {            obj.SetValue(LeftResize, window);        }        public static readonly DependencyProperty LeftResize = DependencyProperty.RegisterAttached("LeftResize",            typeof(Window), typeof(WindowResizeBehavior),            new UIPropertyMetadata(null, OnLeftResizeChanged));        private static void OnLeftResizeChanged(object sender, DependencyPropertyChangedEventArgs e)        {            var thumb = sender as Thumb;            if (thumb != null)            {                thumb.DragDelta += DragLeft;            }        }        public static Window GetRightResize(DependencyObject obj)        {            return (Window)obj.GetValue(RightResize);        }        public static void SetRightResize(DependencyObject obj, Window window)        {            obj.SetValue(RightResize, window);        }        public static readonly DependencyProperty RightResize = DependencyProperty.RegisterAttached("RightResize",            typeof(Window), typeof(WindowResizeBehavior),            new UIPropertyMetadata(null, OnRightResizeChanged));        private static void OnRightResizeChanged(object sender, DependencyPropertyChangedEventArgs e)        {            var thumb = sender as Thumb;            if (thumb != null)            {                thumb.DragDelta += DragRight;            }        }        public static Window GetTopResize(DependencyObject obj)        {            return (Window)obj.GetValue(TopResize);        }        public static void SetTopResize(DependencyObject obj, Window window)        {            obj.SetValue(TopResize, window);        }        public static readonly DependencyProperty TopResize = DependencyProperty.RegisterAttached("TopResize",            typeof(Window), typeof(WindowResizeBehavior),            new UIPropertyMetadata(null, OnTopResizeChanged));        private static void OnTopResizeChanged(object sender, DependencyPropertyChangedEventArgs e)        {            var thumb = sender as Thumb;            if (thumb != null)            {                thumb.DragDelta += DragTop;            }        }        public static Window GetBottomResize(DependencyObject obj)        {            return (Window)obj.GetValue(BottomResize);        }        public static void SetBottomResize(DependencyObject obj, Window window)        {            obj.SetValue(BottomResize, window);        }        public static readonly DependencyProperty BottomResize = DependencyProperty.RegisterAttached("BottomResize",            typeof(Window), typeof(WindowResizeBehavior),            new UIPropertyMetadata(null, OnBottomResizeChanged));        private static void OnBottomResizeChanged(object sender, DependencyPropertyChangedEventArgs e)        {            var thumb = sender as Thumb;            if (thumb != null)            {                thumb.DragDelta += DragBottom;            }        }        private static void DragLeft(object sender, DragDeltaEventArgs e)        {            var thumb = sender as Thumb;            var window = thumb.GetValue(LeftResize) as Window;            if (window != null)            {                var horizontalChange = window.SafeWidthChange(e.HorizontalChange, false);                window.Width -= horizontalChange;                window.Left += horizontalChange;            }        }        private static void DragRight(object sender, DragDeltaEventArgs e)        {            var thumb = sender as Thumb;            var window = thumb.GetValue(RightResize) as Window;            if (window != null)            {                var horizontalChange = window.SafeWidthChange(e.HorizontalChange);                window.Width += horizontalChange;            }        }        private static void DragTop(object sender, DragDeltaEventArgs e)        {            var thumb = sender as Thumb;            var window = thumb.GetValue(TopResize) as Window;            if (window != null)            {                var verticalChange = window.SafeHeightChange(e.VerticalChange,false);                window.Height -= verticalChange;                window.Top += verticalChange;            }        }        private static void DragBottom(object sender, DragDeltaEventArgs e)        {            var thumb = sender as Thumb;            var window = thumb.GetValue(BottomResize) as Window;            if (window != null)            {                var verticalChange = window.SafeHeightChange(e.VerticalChange);                window.Height += verticalChange;            }        }        private static void DragTopLeft(object sender, DragDeltaEventArgs e)        {            var thumb = sender as Thumb;            var window = thumb.GetValue(TopLeftResize) as Window;            if (window != null)            {                var verticalChange = window.SafeHeightChange(e.VerticalChange,false);                var horizontalChange = window.SafeWidthChange(e.HorizontalChange,false);                window.Width -= horizontalChange;                window.Left += horizontalChange;                window.Height -= verticalChange;                window.Top += verticalChange;            }        }        private static void DragTopRight(object sender, DragDeltaEventArgs e)        {            var thumb = sender as Thumb;            var window = thumb.GetValue(TopRightResize) as Window;            if (window != null)            {                var verticalChange = window.SafeHeightChange(e.VerticalChange, false);                var horizontalChange = window.SafeWidthChange(e.HorizontalChange);                window.Width += horizontalChange;                window.Height -= verticalChange;                window.Top += verticalChange;            }        }        private static void DragBottomRight(object sender, DragDeltaEventArgs 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 void DragBottomLeft(object sender, DragDeltaEventArgs e)        {            var thumb = sender as Thumb;            var window = thumb.GetValue(BottomLeftResize) as Window;            if (window != null)            {                var verticalChange = window.SafeHeightChange(e.VerticalChange);                var horizontalChange = window.SafeWidthChange(e.HorizontalChange, false);                window.Width -= horizontalChange;                window.Left += 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)            {                return 0;            } 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)            {                return 0;            }            else if (result >= window.MaxHeight)            {                return 0;            }            else if (result < 0)            {                return 0;            }            else            {                return change;            }        }    }}
 |