WindowTopArea.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.Input;
  8. namespace CyberX8_Themes.CustomControls
  9. {
  10. public class WindowTopArea : System.Windows.Controls.Border
  11. {
  12. static WindowTopArea()
  13. {
  14. DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowTopArea), new FrameworkPropertyMetadata(typeof(WindowTopArea)));
  15. }
  16. private new bool CaptureMouse = false;
  17. private enum ResizePosition
  18. { None, Left, TopLeft, Top, TopRight, Right, BottomRight, Bottom, BottomLeft }
  19. protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
  20. {
  21. Window win = Window.GetWindow(this);
  22. if (e.ClickCount == 1 && e.LeftButton == MouseButtonState.Pressed)
  23. {
  24. win.DragMove();
  25. }
  26. else if (e.ClickCount == 2)
  27. {
  28. win.SizeToContent = SizeToContent.Manual;
  29. win.WindowState = win.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
  30. }
  31. base.OnMouseLeftButtonDown(e);
  32. e.Handled = true;
  33. }
  34. protected override void OnInitialized(EventArgs e)
  35. {
  36. //SetWindowNoBorder();
  37. //SetWindowResizer();
  38. base.OnInitialized(e);
  39. }
  40. #region 窗体大小变化
  41. public void SetWindowResizer()
  42. {
  43. Window win = Window.GetWindow(this);
  44. ResizePosition ResPosition = ResizePosition.None;
  45. int Resizer = 5;
  46. win.MouseMove += new MouseEventHandler(
  47. delegate (object target, MouseEventArgs args)
  48. {
  49. try
  50. {
  51. //do resize
  52. if (win.WindowState == WindowState.Normal)
  53. {
  54. Point MS = args.GetPosition(win);
  55. if (args.LeftButton == MouseButtonState.Pressed)
  56. {
  57. Win32.POINT pos = new Win32.POINT();
  58. Win32.GetCursorPos(out pos);
  59. #region 改变窗体大小
  60. switch (ResPosition)
  61. {
  62. case ResizePosition.Left:
  63. //左边
  64. Mouse.SetCursor(Cursors.SizeWE);
  65. Point transPointLeft = win.PointToScreen(new Point(0, 0));
  66. win.Left += pos.X - transPointLeft.X;
  67. win.Width += transPointLeft.X - pos.X;
  68. break;
  69. case ResizePosition.Right:
  70. //右边
  71. Mouse.SetCursor(Cursors.SizeWE);
  72. Point transPointRight = win.PointToScreen(new Point(win.Width, 0));
  73. win.Width += pos.X - transPointRight.X;
  74. break;
  75. case ResizePosition.Top:
  76. //顶部
  77. Mouse.SetCursor(Cursors.SizeNS);
  78. Point transPointTop = win.PointToScreen(new Point(0, 0));
  79. win.Top += pos.Y - transPointTop.Y;
  80. win.Height += transPointTop.Y - pos.Y;
  81. break;
  82. case ResizePosition.Bottom:
  83. //底部
  84. Mouse.SetCursor(Cursors.SizeNS);
  85. Point transPointBottom = win.PointToScreen(new Point(0, win.Height));
  86. win.Height += (pos.Y - transPointBottom.Y);
  87. break;
  88. case ResizePosition.TopLeft:
  89. //左上
  90. Mouse.SetCursor(Cursors.SizeNWSE);
  91. Point transPointTopLeft = win.PointToScreen(new Point(0, 0));
  92. win.Left += pos.X - transPointTopLeft.X;
  93. win.Top += pos.Y - transPointTopLeft.Y;
  94. win.Width += transPointTopLeft.X - pos.X;
  95. win.Height += transPointTopLeft.Y - pos.Y;
  96. break;
  97. case ResizePosition.BottomLeft:
  98. //左下
  99. Mouse.SetCursor(Cursors.SizeNESW);
  100. Point transPointBottomLeft = win.PointToScreen(new Point(0, win.Height));
  101. win.Left += pos.X - transPointBottomLeft.X;
  102. win.Width += transPointBottomLeft.X - pos.X;
  103. win.Height += pos.Y - transPointBottomLeft.Y;
  104. break;
  105. case ResizePosition.TopRight:
  106. //右上
  107. Mouse.SetCursor(Cursors.SizeNESW);
  108. Point transPointTopRight = win.PointToScreen(new Point(win.Width, 0));
  109. win.Top += pos.Y - transPointTopRight.Y;
  110. win.Width = transPointTopRight.Y - pos.X;
  111. win.Height = transPointTopRight.Y - pos.Y;
  112. break;
  113. case ResizePosition.BottomRight:
  114. //右下
  115. Mouse.SetCursor(Cursors.SizeNWSE);
  116. Point transPointBottomRight = win.PointToScreen(new Point(win.Width, win.Height));
  117. win.Width += pos.X - transPointBottomRight.X;
  118. win.Height += pos.Y - transPointBottomRight.Y;
  119. break;
  120. case ResizePosition.None:
  121. default:
  122. Mouse.SetCursor(Cursors.Arrow);
  123. break;
  124. }
  125. #endregion
  126. }
  127. else if (MS.X <= Resizer + 5 && MS.Y <= Resizer + 5)
  128. {
  129. //左上 (不执行)
  130. Mouse.SetCursor(Cursors.SizeNWSE);
  131. ResPosition = ResizePosition.TopLeft;
  132. }
  133. else if (MS.X <= Resizer && MS.Y >= win.ActualHeight - Resizer)
  134. {
  135. //左下
  136. Mouse.SetCursor(Cursors.SizeNESW);
  137. ResPosition = ResizePosition.BottomLeft;
  138. }
  139. else if (MS.X >= win.ActualWidth - Resizer - 5 && MS.Y <= Resizer + 5)
  140. {
  141. //右上(不执行)
  142. Mouse.SetCursor(Cursors.SizeNESW);
  143. ResPosition = ResizePosition.TopRight;
  144. }
  145. else if (MS.X >= win.ActualWidth - Resizer && MS.Y >= win.ActualHeight - Resizer)
  146. {
  147. //右下
  148. Mouse.SetCursor(Cursors.SizeNWSE);
  149. ResPosition = ResizePosition.BottomRight;
  150. }
  151. else if (MS.X <= Resizer)
  152. {
  153. //左边
  154. Mouse.SetCursor(Cursors.SizeWE);
  155. ResPosition = ResizePosition.Left;
  156. }
  157. else if (MS.Y <= Resizer + 5)
  158. {
  159. //顶部(不执行)
  160. Mouse.SetCursor(Cursors.SizeNS);
  161. ResPosition = ResizePosition.Top;
  162. }
  163. else if (MS.X >= win.ActualWidth - Resizer)
  164. {
  165. //右边
  166. Mouse.SetCursor(Cursors.SizeWE);
  167. ResPosition = ResizePosition.Right;
  168. }
  169. else if (MS.Y >= win.ActualHeight - Resizer)
  170. {
  171. //底部
  172. Mouse.SetCursor(Cursors.SizeNS);
  173. ResPosition = ResizePosition.Bottom;
  174. }
  175. else
  176. {
  177. //无
  178. Mouse.SetCursor(Cursors.Arrow);
  179. ResPosition = ResizePosition.None;
  180. }
  181. }
  182. }
  183. catch
  184. {
  185. ResPosition = ResizePosition.None;
  186. win.ReleaseMouseCapture();
  187. }
  188. args.Handled = CaptureMouse;
  189. }
  190. );
  191. win.MouseLeftButtonDown += new MouseButtonEventHandler(
  192. delegate (object target, MouseButtonEventArgs args)
  193. {
  194. if (win.WindowState == WindowState.Normal)
  195. {
  196. //获取当前鼠标点击点相对于Dvap.Shell窗体的位置
  197. Point pos = args.GetPosition(win);
  198. if (ResPosition != ResizePosition.None)
  199. {
  200. CaptureMouse = win.CaptureMouse();
  201. }
  202. args.Handled = CaptureMouse;
  203. }
  204. }
  205. );
  206. win.MouseLeftButtonUp += new MouseButtonEventHandler(
  207. delegate (object target, MouseButtonEventArgs args)
  208. {
  209. if (win.WindowState == WindowState.Normal)
  210. {
  211. ResPosition = ResizePosition.None;
  212. if (CaptureMouse)
  213. {
  214. win.ReleaseMouseCapture();
  215. CaptureMouse = false;
  216. }
  217. args.Handled = CaptureMouse;
  218. }
  219. }
  220. );
  221. }
  222. #endregion
  223. private void SetWindowNoBorder()
  224. {
  225. Window win = Window.GetWindow(this);
  226. // 获取窗体句柄
  227. IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(win).Handle;
  228. // 获得窗体的 样式
  229. long oldstyle = Win32.GetWindowLong(hwnd, Win32.GWL_STYLE);
  230. // 更改窗体的样式为无边框窗体
  231. Win32.SetWindowLong(hwnd, Win32.GWL_STYLE, (int)(oldstyle & ~Win32.WS_CAPTION));
  232. }
  233. }
  234. }