WindowResizeBehavior.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Controls.Primitives;
  9. namespace WpfStyleableWindow.StyleableWindow
  10. {
  11. public static class WindowResizeBehavior
  12. {
  13. public static Window GetTopLeftResize(DependencyObject obj)
  14. {
  15. return (Window)obj.GetValue(TopLeftResize);
  16. }
  17. public static void SetTopLeftResize(DependencyObject obj, Window window)
  18. {
  19. obj.SetValue(TopLeftResize, window);
  20. }
  21. public static readonly DependencyProperty TopLeftResize = DependencyProperty.RegisterAttached("TopLeftResize",
  22. typeof(Window), typeof(WindowResizeBehavior),
  23. new UIPropertyMetadata(null, OnTopLeftResizeChanged));
  24. private static void OnTopLeftResizeChanged(object sender, DependencyPropertyChangedEventArgs e)
  25. {
  26. var thumb = sender as Thumb;
  27. if (thumb != null)
  28. {
  29. thumb.DragDelta += DragTopLeft;
  30. }
  31. }
  32. public static Window GetTopRightResize(DependencyObject obj)
  33. {
  34. return (Window)obj.GetValue(TopRightResize);
  35. }
  36. public static void SetTopRightResize(DependencyObject obj, Window window)
  37. {
  38. obj.SetValue(TopRightResize, window);
  39. }
  40. public static readonly DependencyProperty TopRightResize = DependencyProperty.RegisterAttached("TopRightResize",
  41. typeof(Window), typeof(WindowResizeBehavior),
  42. new UIPropertyMetadata(null, OnTopRightResizeChanged));
  43. private static void OnTopRightResizeChanged(object sender, DependencyPropertyChangedEventArgs e)
  44. {
  45. var thumb = sender as Thumb;
  46. if (thumb != null)
  47. {
  48. thumb.DragDelta += DragTopRight;
  49. }
  50. }
  51. public static Window GetBottomRightResize(DependencyObject obj)
  52. {
  53. return (Window)obj.GetValue(BottomRightResize);
  54. }
  55. public static void SetBottomRightResize(DependencyObject obj, Window window)
  56. {
  57. obj.SetValue(BottomRightResize, window);
  58. }
  59. public static readonly DependencyProperty BottomRightResize = DependencyProperty.RegisterAttached("BottomRightResize",
  60. typeof(Window), typeof(WindowResizeBehavior),
  61. new UIPropertyMetadata(null, OnBottomRightResizeChanged));
  62. private static void OnBottomRightResizeChanged(object sender, DependencyPropertyChangedEventArgs e)
  63. {
  64. var thumb = sender as Thumb;
  65. if (thumb != null)
  66. {
  67. thumb.DragDelta += DragBottomRight;
  68. }
  69. }
  70. public static Window GetBottomLeftResize(DependencyObject obj)
  71. {
  72. return (Window)obj.GetValue(BottomLeftResize);
  73. }
  74. public static void SetBottomLeftResize(DependencyObject obj, Window window)
  75. {
  76. obj.SetValue(BottomLeftResize, window);
  77. }
  78. public static readonly DependencyProperty BottomLeftResize = DependencyProperty.RegisterAttached("BottomLeftResize",
  79. typeof(Window), typeof(WindowResizeBehavior),
  80. new UIPropertyMetadata(null, OnBottomLeftResizeChanged));
  81. private static void OnBottomLeftResizeChanged(object sender, DependencyPropertyChangedEventArgs e)
  82. {
  83. var thumb = sender as Thumb;
  84. if (thumb != null)
  85. {
  86. thumb.DragDelta += DragBottomLeft;
  87. }
  88. }
  89. public static Window GetLeftResize(DependencyObject obj)
  90. {
  91. return (Window)obj.GetValue(LeftResize);
  92. }
  93. public static void SetLeftResize(DependencyObject obj, Window window)
  94. {
  95. obj.SetValue(LeftResize, window);
  96. }
  97. public static readonly DependencyProperty LeftResize = DependencyProperty.RegisterAttached("LeftResize",
  98. typeof(Window), typeof(WindowResizeBehavior),
  99. new UIPropertyMetadata(null, OnLeftResizeChanged));
  100. private static void OnLeftResizeChanged(object sender, DependencyPropertyChangedEventArgs e)
  101. {
  102. var thumb = sender as Thumb;
  103. if (thumb != null)
  104. {
  105. thumb.DragDelta += DragLeft;
  106. }
  107. }
  108. public static Window GetRightResize(DependencyObject obj)
  109. {
  110. return (Window)obj.GetValue(RightResize);
  111. }
  112. public static void SetRightResize(DependencyObject obj, Window window)
  113. {
  114. obj.SetValue(RightResize, window);
  115. }
  116. public static readonly DependencyProperty RightResize = DependencyProperty.RegisterAttached("RightResize",
  117. typeof(Window), typeof(WindowResizeBehavior),
  118. new UIPropertyMetadata(null, OnRightResizeChanged));
  119. private static void OnRightResizeChanged(object sender, DependencyPropertyChangedEventArgs e)
  120. {
  121. var thumb = sender as Thumb;
  122. if (thumb != null)
  123. {
  124. thumb.DragDelta += DragRight;
  125. }
  126. }
  127. public static Window GetTopResize(DependencyObject obj)
  128. {
  129. return (Window)obj.GetValue(TopResize);
  130. }
  131. public static void SetTopResize(DependencyObject obj, Window window)
  132. {
  133. obj.SetValue(TopResize, window);
  134. }
  135. public static readonly DependencyProperty TopResize = DependencyProperty.RegisterAttached("TopResize",
  136. typeof(Window), typeof(WindowResizeBehavior),
  137. new UIPropertyMetadata(null, OnTopResizeChanged));
  138. private static void OnTopResizeChanged(object sender, DependencyPropertyChangedEventArgs e)
  139. {
  140. var thumb = sender as Thumb;
  141. if (thumb != null)
  142. {
  143. thumb.DragDelta += DragTop;
  144. }
  145. }
  146. public static Window GetBottomResize(DependencyObject obj)
  147. {
  148. return (Window)obj.GetValue(BottomResize);
  149. }
  150. public static void SetBottomResize(DependencyObject obj, Window window)
  151. {
  152. obj.SetValue(BottomResize, window);
  153. }
  154. public static readonly DependencyProperty BottomResize = DependencyProperty.RegisterAttached("BottomResize",
  155. typeof(Window), typeof(WindowResizeBehavior),
  156. new UIPropertyMetadata(null, OnBottomResizeChanged));
  157. private static void OnBottomResizeChanged(object sender, DependencyPropertyChangedEventArgs e)
  158. {
  159. var thumb = sender as Thumb;
  160. if (thumb != null)
  161. {
  162. thumb.DragDelta += DragBottom;
  163. }
  164. }
  165. private static void DragLeft(object sender, DragDeltaEventArgs e)
  166. {
  167. var thumb = sender as Thumb;
  168. var window = thumb.GetValue(LeftResize) as Window;
  169. if (window != null)
  170. {
  171. var horizontalChange = window.SafeWidthChange(e.HorizontalChange, false);
  172. window.Width -= horizontalChange;
  173. window.Left += horizontalChange;
  174. }
  175. }
  176. private static void DragRight(object sender, DragDeltaEventArgs e)
  177. {
  178. var thumb = sender as Thumb;
  179. var window = thumb.GetValue(RightResize) as Window;
  180. if (window != null)
  181. {
  182. var horizontalChange = window.SafeWidthChange(e.HorizontalChange);
  183. window.Width += horizontalChange;
  184. }
  185. }
  186. private static void DragTop(object sender, DragDeltaEventArgs e)
  187. {
  188. var thumb = sender as Thumb;
  189. var window = thumb.GetValue(TopResize) as Window;
  190. if (window != null)
  191. {
  192. var verticalChange = window.SafeHeightChange(e.VerticalChange,false);
  193. window.Height -= verticalChange;
  194. window.Top += verticalChange;
  195. }
  196. }
  197. private static void DragBottom(object sender, DragDeltaEventArgs e)
  198. {
  199. var thumb = sender as Thumb;
  200. var window = thumb.GetValue(BottomResize) as Window;
  201. if (window != null)
  202. {
  203. var verticalChange = window.SafeHeightChange(e.VerticalChange);
  204. window.Height += verticalChange;
  205. }
  206. }
  207. private static void DragTopLeft(object sender, DragDeltaEventArgs e)
  208. {
  209. var thumb = sender as Thumb;
  210. var window = thumb.GetValue(TopLeftResize) as Window;
  211. if (window != null)
  212. {
  213. var verticalChange = window.SafeHeightChange(e.VerticalChange,false);
  214. var horizontalChange = window.SafeWidthChange(e.HorizontalChange,false);
  215. window.Width -= horizontalChange;
  216. window.Left += horizontalChange;
  217. window.Height -= verticalChange;
  218. window.Top += verticalChange;
  219. }
  220. }
  221. private static void DragTopRight(object sender, DragDeltaEventArgs e)
  222. {
  223. var thumb = sender as Thumb;
  224. var window = thumb.GetValue(TopRightResize) as Window;
  225. if (window != null)
  226. {
  227. var verticalChange = window.SafeHeightChange(e.VerticalChange, false);
  228. var horizontalChange = window.SafeWidthChange(e.HorizontalChange);
  229. window.Width += horizontalChange;
  230. window.Height -= verticalChange;
  231. window.Top += verticalChange;
  232. }
  233. }
  234. private static void DragBottomRight(object sender, DragDeltaEventArgs e)
  235. {
  236. var thumb = sender as Thumb;
  237. var window = thumb.GetValue(BottomRightResize) as Window;
  238. if (window != null)
  239. {
  240. var verticalChange = window.SafeHeightChange(e.VerticalChange);
  241. var horizontalChange = window.SafeWidthChange(e.HorizontalChange);
  242. window.Width += horizontalChange;
  243. window.Height += verticalChange;
  244. }
  245. }
  246. private static void DragBottomLeft(object sender, DragDeltaEventArgs e)
  247. {
  248. var thumb = sender as Thumb;
  249. var window = thumb.GetValue(BottomLeftResize) as Window;
  250. if (window != null)
  251. {
  252. var verticalChange = window.SafeHeightChange(e.VerticalChange);
  253. var horizontalChange = window.SafeWidthChange(e.HorizontalChange, false);
  254. window.Width -= horizontalChange;
  255. window.Left += horizontalChange;
  256. window.Height += verticalChange;
  257. }
  258. }
  259. private static double SafeWidthChange(this Window window, double change, bool positive = true)
  260. {
  261. var result = positive ? window.Width + change : window.Width - change;
  262. if (result <= window.MinWidth)
  263. {
  264. return 0;
  265. } else if(result >= window.MaxWidth)
  266. {
  267. return 0;
  268. } else if(result < 0)
  269. {
  270. return 0;
  271. }
  272. else
  273. {
  274. return change;
  275. }
  276. }
  277. private static double SafeHeightChange(this Window window, double change, bool positive = true)
  278. {
  279. var result = positive ? window.Height + change : window.Height - change;
  280. if (result <= window.MinHeight)
  281. {
  282. return 0;
  283. }
  284. else if (result >= window.MaxHeight)
  285. {
  286. return 0;
  287. }
  288. else if (result < 0)
  289. {
  290. return 0;
  291. }
  292. else
  293. {
  294. return change;
  295. }
  296. }
  297. }
  298. }