WindowAttach.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Input;
  5. using System.Windows.Interop;
  6. using HandyControl.Data;
  7. using HandyControl.Tools.Interop;
  8. namespace HandyControl.Controls;
  9. public static class WindowAttach
  10. {
  11. public static readonly DependencyProperty IsDragElementProperty = DependencyProperty.RegisterAttached(
  12. "IsDragElement", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIsDragElementChanged));
  13. public static void SetIsDragElement(DependencyObject element, bool value)
  14. => element.SetValue(IsDragElementProperty, ValueBoxes.BooleanBox(value));
  15. public static bool GetIsDragElement(DependencyObject element)
  16. => (bool) element.GetValue(IsDragElementProperty);
  17. private static void OnIsDragElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  18. {
  19. if (d is UIElement ctl)
  20. {
  21. if ((bool) e.NewValue)
  22. {
  23. ctl.MouseLeftButtonDown += DragElement_MouseLeftButtonDown;
  24. }
  25. else
  26. {
  27. ctl.MouseLeftButtonDown -= DragElement_MouseLeftButtonDown;
  28. }
  29. }
  30. }
  31. private static void DragElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  32. {
  33. if (sender is DependencyObject obj && e.ButtonState == MouseButtonState.Pressed)
  34. {
  35. System.Windows.Window.GetWindow(obj)?.DragMove();
  36. }
  37. }
  38. public static readonly DependencyProperty IgnoreAltF4Property = DependencyProperty.RegisterAttached(
  39. "IgnoreAltF4", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIgnoreAltF4Changed));
  40. private static void OnIgnoreAltF4Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  41. {
  42. if (d is System.Windows.Window window)
  43. {
  44. if ((bool) e.NewValue)
  45. {
  46. window.PreviewKeyDown += Window_PreviewKeyDown;
  47. }
  48. else
  49. {
  50. window.PreviewKeyDown -= Window_PreviewKeyDown;
  51. }
  52. }
  53. }
  54. private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
  55. {
  56. if (e.Key == Key.System && e.SystemKey == Key.F4)
  57. {
  58. e.Handled = true;
  59. }
  60. }
  61. public static void SetIgnoreAltF4(DependencyObject element, bool value)
  62. => element.SetValue(IgnoreAltF4Property, ValueBoxes.BooleanBox(value));
  63. public static bool GetIgnoreAltF4(DependencyObject element)
  64. => (bool) element.GetValue(IgnoreAltF4Property);
  65. public static readonly DependencyProperty ShowInTaskManagerProperty = DependencyProperty.RegisterAttached(
  66. "ShowInTaskManager", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.TrueBox, OnShowInTaskManagerChanged));
  67. private static void OnShowInTaskManagerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  68. {
  69. if (d is System.Windows.Window window)
  70. {
  71. var v = (bool) e.NewValue;
  72. window.SetCurrentValue(System.Windows.Window.ShowInTaskbarProperty, v);
  73. if (v)
  74. {
  75. window.SourceInitialized -= Window_SourceInitialized;
  76. }
  77. else
  78. {
  79. window.SourceInitialized += Window_SourceInitialized;
  80. }
  81. }
  82. }
  83. private static void Window_SourceInitialized(object sender, EventArgs e)
  84. {
  85. if (sender is System.Windows.Window window)
  86. {
  87. var _ = new WindowInteropHelper(window)
  88. {
  89. Owner = InteropMethods.GetDesktopWindow()
  90. };
  91. }
  92. }
  93. public static void SetShowInTaskManager(DependencyObject element, bool value)
  94. => element.SetValue(ShowInTaskManagerProperty, ValueBoxes.BooleanBox(value));
  95. public static bool GetShowInTaskManager(DependencyObject element)
  96. => (bool) element.GetValue(ShowInTaskManagerProperty);
  97. public static readonly DependencyProperty HideWhenClosingProperty = DependencyProperty.RegisterAttached(
  98. "HideWhenClosing", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnHideWhenClosingChanged));
  99. private static void OnHideWhenClosingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  100. {
  101. if (d is System.Windows.Window window)
  102. {
  103. var v = (bool) e.NewValue;
  104. if (v)
  105. {
  106. window.Closing += Window_Closing;
  107. }
  108. else
  109. {
  110. window.Closing -= Window_Closing;
  111. }
  112. }
  113. }
  114. private static void Window_Closing(object sender, CancelEventArgs e)
  115. {
  116. if (sender is System.Windows.Window window)
  117. {
  118. window.Hide();
  119. e.Cancel = true;
  120. }
  121. }
  122. public static void SetHideWhenClosing(DependencyObject element, bool value)
  123. => element.SetValue(HideWhenClosingProperty, ValueBoxes.BooleanBox(value));
  124. public static bool GetHideWhenClosing(DependencyObject element)
  125. => (bool) element.GetValue(HideWhenClosingProperty);
  126. public static readonly DependencyProperty ExtendContentToNonClientAreaProperty = DependencyProperty.RegisterAttached(
  127. "ExtendContentToNonClientArea", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox));
  128. public static void SetExtendContentToNonClientArea(DependencyObject element, bool value)
  129. => element.SetValue(ExtendContentToNonClientAreaProperty, ValueBoxes.BooleanBox(value));
  130. public static bool GetExtendContentToNonClientArea(DependencyObject element)
  131. => (bool) element.GetValue(ExtendContentToNonClientAreaProperty);
  132. }