GlowWindow.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Interop;
  8. using System.Windows.Media;
  9. using System.Windows.Threading;
  10. using HandyControl.Data;
  11. using HandyControl.Expression.Drawing;
  12. using HandyControl.Tools;
  13. using HandyControl.Tools.Interop;
  14. namespace HandyControl.Controls;
  15. public class GlowWindow : Window
  16. {
  17. internal int DeferGlowChangesCount;
  18. private readonly GlowEdge[] _glowEdges = new GlowEdge[4];
  19. private DispatcherTimer _makeGlowVisibleTimer;
  20. private bool _isGlowVisible;
  21. private bool _useLogicalSizeForRestore;
  22. private bool _updatingZOrder;
  23. private Rect _logicalSizeForRestore = Rect.Empty;
  24. public static readonly DependencyProperty ActiveGlowColorProperty = DependencyProperty.Register(
  25. nameof(ActiveGlowColor), typeof(Color), typeof(GlowWindow), new PropertyMetadata(default(Color), OnGlowColorChanged));
  26. public Color ActiveGlowColor
  27. {
  28. get => (Color) GetValue(ActiveGlowColorProperty);
  29. set => SetValue(ActiveGlowColorProperty, value);
  30. }
  31. public static readonly DependencyProperty InactiveGlowColorProperty = DependencyProperty.Register(
  32. nameof(InactiveGlowColor), typeof(Color), typeof(GlowWindow), new PropertyMetadata(default(Color), OnGlowColorChanged));
  33. public Color InactiveGlowColor
  34. {
  35. get => (Color) GetValue(InactiveGlowColorProperty);
  36. set => SetValue(InactiveGlowColorProperty, value);
  37. }
  38. #region internal
  39. internal void EndDeferGlowChanges()
  40. {
  41. foreach (var current in LoadedGlowWindows) current.CommitChanges();
  42. }
  43. #endregion
  44. #region protected
  45. protected virtual bool ShouldShowGlow
  46. {
  47. get
  48. {
  49. var handle = this.GetHandle();
  50. return InteropMethods.IsWindowVisible(handle) && !InteropMethods.IsIconic(handle) &&
  51. !InteropMethods.IsZoomed(handle) && ResizeMode != ResizeMode.NoResize;
  52. }
  53. }
  54. private IntPtr HwndSourceHook(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  55. {
  56. if (msg <= InteropValues.WM_WINDOWPOSCHANGED)
  57. {
  58. if (msg == InteropValues.WM_ACTIVATE)
  59. {
  60. return IntPtr.Zero;
  61. }
  62. if (msg != InteropValues.WM_QUIT)
  63. {
  64. switch (msg)
  65. {
  66. case InteropValues.WM_WINDOWPOSCHANGING:
  67. WmWindowPosChanging(lParam);
  68. return IntPtr.Zero;
  69. case InteropValues.WM_WINDOWPOSCHANGED:
  70. WmWindowPosChanged(lParam);
  71. return IntPtr.Zero;
  72. default:
  73. return IntPtr.Zero;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. if (msg <= InteropValues.WM_NCRBUTTONDBLCLK)
  80. {
  81. switch (msg)
  82. {
  83. case InteropValues.WM_SETICON:
  84. break;
  85. case InteropValues.WM_NCCREATE:
  86. case InteropValues.WM_NCDESTROY:
  87. return IntPtr.Zero;
  88. case InteropValues.WM_NCACTIVATE:
  89. handled = true;
  90. return WmNcActivate(hWnd, wParam);
  91. default:
  92. switch (msg)
  93. {
  94. case InteropValues.WM_NCRBUTTONDOWN:
  95. case InteropValues.WM_NCRBUTTONUP:
  96. case InteropValues.WM_NCRBUTTONDBLCLK:
  97. handled = true;
  98. return IntPtr.Zero;
  99. default:
  100. return IntPtr.Zero;
  101. }
  102. }
  103. }
  104. else
  105. {
  106. switch (msg)
  107. {
  108. case InteropValues.WM_NCUAHDRAWCAPTION:
  109. case InteropValues.WM_NCUAHDRAWFRAME:
  110. handled = true;
  111. return IntPtr.Zero;
  112. default:
  113. if (msg != InteropValues.WM_SYSCOMMAND) return IntPtr.Zero;
  114. WmSysCommand(hWnd, wParam);
  115. return IntPtr.Zero;
  116. }
  117. }
  118. }
  119. handled = true;
  120. return CallDefWindowProcWithoutVisibleStyle(hWnd, msg, wParam, lParam);
  121. }
  122. protected override void OnActivated(EventArgs e)
  123. {
  124. UpdateGlowActiveState();
  125. base.OnActivated(e);
  126. }
  127. protected override void OnDeactivated(EventArgs e)
  128. {
  129. UpdateGlowActiveState();
  130. base.OnDeactivated(e);
  131. }
  132. protected override void OnClosed(EventArgs e)
  133. {
  134. StopTimer();
  135. DestroyGlowWindows();
  136. base.OnClosed(e);
  137. }
  138. protected override void OnSourceInitialized(EventArgs e)
  139. {
  140. base.OnSourceInitialized(e);
  141. var hwndSource = this.GetHwndSource();
  142. if (hwndSource != null)
  143. {
  144. hwndSource.AddHook(HwndSourceHook);
  145. CreateGlowWindowHandles();
  146. }
  147. }
  148. #endregion
  149. #region private
  150. private IEnumerable<GlowEdge> LoadedGlowWindows => from w in _glowEdges where w != null select w;
  151. private bool IsGlowVisible
  152. {
  153. get => _isGlowVisible;
  154. set
  155. {
  156. if (_isGlowVisible != value)
  157. {
  158. _isGlowVisible = value;
  159. for (var i = 0; i < _glowEdges.Length; i++)
  160. {
  161. GetOrCreateGlowWindow(i).IsVisible = value;
  162. }
  163. }
  164. }
  165. }
  166. private GlowEdge GetOrCreateGlowWindow(int direction)
  167. {
  168. return _glowEdges[direction] ?? (_glowEdges[direction] = new GlowEdge(this, (Dock) direction)
  169. {
  170. ActiveGlowColor = ActiveGlowColor,
  171. InactiveGlowColor = InactiveGlowColor,
  172. IsActive = IsActive
  173. });
  174. }
  175. private static void OnResizeModeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  176. {
  177. var customChromeWindow = (GlowWindow) obj;
  178. customChromeWindow.UpdateGlowVisibility(false);
  179. }
  180. private static void OnGlowColorChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) => ((GlowWindow) obj).UpdateGlowColors();
  181. private void UpdateGlowColors()
  182. {
  183. using (DeferGlowChanges())
  184. {
  185. foreach (var current in LoadedGlowWindows)
  186. {
  187. current.ActiveGlowColor = ActiveGlowColor;
  188. current.InactiveGlowColor = InactiveGlowColor;
  189. }
  190. }
  191. }
  192. private void UpdateGlowVisibility(bool delayIfNecessary)
  193. {
  194. var shouldShowGlow = ShouldShowGlow;
  195. if (shouldShowGlow != IsGlowVisible)
  196. {
  197. if (SystemParameters.MinimizeAnimation && shouldShowGlow && delayIfNecessary)
  198. {
  199. if (_makeGlowVisibleTimer != null)
  200. {
  201. _makeGlowVisibleTimer.Stop();
  202. }
  203. else
  204. {
  205. _makeGlowVisibleTimer = new DispatcherTimer
  206. {
  207. Interval = TimeSpan.FromMilliseconds(200.0)
  208. };
  209. _makeGlowVisibleTimer.Tick += OnDelayedVisibilityTimerTick;
  210. }
  211. _makeGlowVisibleTimer.Start();
  212. return;
  213. }
  214. StopTimer();
  215. IsGlowVisible = shouldShowGlow;
  216. }
  217. }
  218. private void StopTimer()
  219. {
  220. if (_makeGlowVisibleTimer != null)
  221. {
  222. _makeGlowVisibleTimer.Stop();
  223. _makeGlowVisibleTimer.Tick -= OnDelayedVisibilityTimerTick;
  224. _makeGlowVisibleTimer = null;
  225. }
  226. }
  227. private void OnDelayedVisibilityTimerTick(object sender, EventArgs e)
  228. {
  229. StopTimer();
  230. UpdateGlowWindowPositions(false);
  231. }
  232. private void UpdateGlowWindowPositions(bool delayIfNecessary)
  233. {
  234. using (DeferGlowChanges())
  235. {
  236. UpdateGlowVisibility(delayIfNecessary);
  237. foreach (var current in LoadedGlowWindows) current.UpdateWindowPos();
  238. }
  239. }
  240. private IDisposable DeferGlowChanges() => new ChangeScope(this);
  241. private void UpdateGlowActiveState()
  242. {
  243. using (DeferGlowChanges())
  244. {
  245. foreach (var current in LoadedGlowWindows)
  246. {
  247. current.IsActive = IsActive;
  248. }
  249. }
  250. }
  251. private void DestroyGlowWindows()
  252. {
  253. for (var i = 0; i < _glowEdges.Length; i++)
  254. {
  255. using (_glowEdges[i])
  256. {
  257. _glowEdges[i] = null;
  258. }
  259. }
  260. }
  261. private void WmWindowPosChanging(IntPtr lParam)
  262. {
  263. var windowpos = (InteropValues.WINDOWPOS) Marshal.PtrToStructure(lParam, typeof(InteropValues.WINDOWPOS));
  264. if ((windowpos.flags & 2u) == 0u && (windowpos.flags & 1u) == 0u && windowpos.cx > 0 && windowpos.cy > 0)
  265. {
  266. var rect = new Rect(windowpos.x, windowpos.y, windowpos.cx, windowpos.cy);
  267. rect = rect.DeviceToLogicalUnits();
  268. if (_useLogicalSizeForRestore)
  269. {
  270. rect = _logicalSizeForRestore;
  271. _logicalSizeForRestore = Rect.Empty;
  272. _useLogicalSizeForRestore = false;
  273. }
  274. var logicalRect = GetOnScreenPosition(rect);
  275. logicalRect = logicalRect.LogicalToDeviceUnits();
  276. windowpos.x = (int) logicalRect.X;
  277. windowpos.y = (int) logicalRect.Y;
  278. Marshal.StructureToPtr(windowpos, lParam, true);
  279. }
  280. }
  281. private void UpdateZOrderOfOwner(IntPtr hwndOwner)
  282. {
  283. var lastOwnedWindow = IntPtr.Zero;
  284. InteropMethods.EnumThreadWindows(InteropMethods.GetCurrentThreadId(), delegate (IntPtr hwnd, IntPtr lParam)
  285. {
  286. if (InteropMethods.GetWindow(hwnd, 4) == hwndOwner) lastOwnedWindow = hwnd;
  287. return true;
  288. }, IntPtr.Zero);
  289. if (lastOwnedWindow != IntPtr.Zero && InteropMethods.GetWindow(hwndOwner, 3) != lastOwnedWindow)
  290. InteropMethods.SetWindowPos(hwndOwner, lastOwnedWindow, 0, 0, 0, 0, 19);
  291. }
  292. private void UpdateZOrderOfThisAndOwner()
  293. {
  294. if (_updatingZOrder) return;
  295. try
  296. {
  297. _updatingZOrder = true;
  298. var windowInteropHelper = new WindowInteropHelper(this);
  299. var handle = windowInteropHelper.Handle;
  300. foreach (var current in LoadedGlowWindows)
  301. {
  302. var window = InteropMethods.GetWindow(current.Handle, 3);
  303. if (window != handle) InteropMethods.SetWindowPos(current.Handle, handle, 0, 0, 0, 0, 19);
  304. handle = current.Handle;
  305. }
  306. var owner = windowInteropHelper.Owner;
  307. if (owner != IntPtr.Zero) UpdateZOrderOfOwner(owner);
  308. }
  309. finally
  310. {
  311. _updatingZOrder = false;
  312. }
  313. }
  314. private void WmWindowPosChanged(IntPtr lParam)
  315. {
  316. try
  317. {
  318. var windowpos = (InteropValues.WINDOWPOS) Marshal.PtrToStructure(lParam, typeof(InteropValues.WINDOWPOS));
  319. UpdateGlowWindowPositions((windowpos.flags & 64u) == 0u);
  320. UpdateZOrderOfThisAndOwner();
  321. }
  322. catch
  323. {
  324. // ignored
  325. }
  326. }
  327. private Rect GetOnScreenPosition(Rect floatRect)
  328. {
  329. var result = floatRect;
  330. floatRect = floatRect.LogicalToDeviceUnits();
  331. ScreenHelper.FindMaximumSingleMonitorRectangle(floatRect, out _, out var rect2);
  332. if (!floatRect.IntersectsWith(rect2))
  333. {
  334. ScreenHelper.FindMonitorRectsFromPoint(InteropMethods.GetCursorPos(), out _, out rect2);
  335. rect2 = rect2.DeviceToLogicalUnits();
  336. if (result.Width > rect2.Width) result.Width = rect2.Width;
  337. if (result.Height > rect2.Height) result.Height = rect2.Height;
  338. if (rect2.Right <= result.X) result.X = rect2.Right - result.Width;
  339. if (rect2.Left > result.X + result.Width) result.X = rect2.Left;
  340. if (rect2.Bottom <= result.Y) result.Y = rect2.Bottom - result.Height;
  341. if (rect2.Top > result.Y + result.Height) result.Y = rect2.Top;
  342. }
  343. return result;
  344. }
  345. private static InteropValues.MONITORINFO MonitorInfoFromWindow(IntPtr hWnd)
  346. {
  347. var hMonitor = InteropMethods.MonitorFromWindow(hWnd, 2);
  348. var result = default(InteropValues.MONITORINFO);
  349. result.cbSize = (uint) Marshal.SizeOf(typeof(InteropValues.MONITORINFO));
  350. InteropMethods.GetMonitorInfo(hMonitor, ref result);
  351. return result;
  352. }
  353. private IntPtr WmNcActivate(IntPtr hWnd, IntPtr wParam) => InteropMethods.DefWindowProc(hWnd, InteropValues.WM_NCACTIVATE, wParam, InteropMethods.HRGN_NONE);
  354. private bool IsAeroSnappedToMonitor(IntPtr hWnd)
  355. {
  356. var monitorinfo = MonitorInfoFromWindow(hWnd);
  357. var logicalRect = new Rect(Left, Top, Width, Height);
  358. logicalRect = logicalRect.LogicalToDeviceUnits();
  359. return MathHelper.AreClose(monitorinfo.rcWork.Height, logicalRect.Height) && MathHelper.AreClose(monitorinfo.rcWork.Top, logicalRect.Top);
  360. }
  361. private void WmSysCommand(IntPtr hWnd, IntPtr wParam)
  362. {
  363. var num = InteropMethods.GET_SC_WPARAM(wParam);
  364. if (num == InteropValues.SC_MOVE)
  365. {
  366. InteropMethods.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero,
  367. InteropValues.RedrawWindowFlags.Invalidate | InteropValues.RedrawWindowFlags.NoChildren |
  368. InteropValues.RedrawWindowFlags.UpdateNow | InteropValues.RedrawWindowFlags.Frame);
  369. }
  370. if ((num == InteropValues.SC_MAXIMIZE || num == InteropValues.SC_MINIMIZE || num == InteropValues.SC_MOVE ||
  371. num == InteropValues.SC_SIZE) && WindowState == WindowState.Normal && !IsAeroSnappedToMonitor(hWnd))
  372. {
  373. _logicalSizeForRestore = new Rect(Left, Top, Width, Height);
  374. }
  375. if (num == InteropValues.SC_MOVE && WindowState == WindowState.Maximized && _logicalSizeForRestore == Rect.Empty)
  376. {
  377. _logicalSizeForRestore = new Rect(Left, Top, Width, Height);
  378. }
  379. if (num == InteropValues.SC_RESTORE && WindowState != WindowState.Minimized &&
  380. _logicalSizeForRestore.Width > 0.0 && _logicalSizeForRestore.Height > 0.0)
  381. {
  382. Left = _logicalSizeForRestore.Left;
  383. Top = _logicalSizeForRestore.Top;
  384. Width = _logicalSizeForRestore.Width;
  385. Height = _logicalSizeForRestore.Height;
  386. _useLogicalSizeForRestore = true;
  387. }
  388. }
  389. private IntPtr CallDefWindowProcWithoutVisibleStyle(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
  390. {
  391. var flag = VisualHelper.ModifyStyle(hWnd, InteropValues.WS_VISIBLE, 0);
  392. var result = InteropMethods.DefWindowProc(hWnd, msg, wParam, lParam);
  393. if (flag) VisualHelper.ModifyStyle(hWnd, 0, InteropValues.WS_VISIBLE);
  394. return result;
  395. }
  396. private void CreateGlowWindowHandles()
  397. {
  398. for (var i = 0; i < _glowEdges.Length; i++) GetOrCreateGlowWindow(i).EnsureHandle();
  399. }
  400. #endregion
  401. }