SlidContent.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using PDF_Office.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace PDF_Office.CustomControl.CompositeControl
  19. {
  20. /// <summary>
  21. /// ThicknessContent.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class SlidContent : UserControl
  24. {
  25. #region 定义
  26. private AdornerLayer popLayer;
  27. private AdornerPresenter layerPresent;
  28. private bool layerAdded = false;
  29. private Canvas popCanvas;
  30. private SlidContentPop SlidOpacityPop;
  31. public event RoutedEventHandler SelectedValueChanged;
  32. public double Value
  33. {
  34. get { return (double)GetValue(_valueProperty); }
  35. set { SetValue(_valueProperty, value); }
  36. }
  37. public static readonly DependencyProperty _valueProperty =
  38. DependencyProperty.Register("Value", typeof(double), typeof(SlidContent), new PropertyMetadata(0.0));
  39. public double InitValue
  40. {
  41. get { return (double)GetValue(_initValueProperty); }
  42. set { SetValue(_initValueProperty, value); }
  43. }
  44. public static readonly DependencyProperty _initValueProperty =
  45. DependencyProperty.Register("InitValue", typeof(double), typeof(SlidContent), new PropertyMetadata(-1.0, InitValuePropertyChanged));
  46. private static void InitValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  47. {
  48. var control = d as SlidContent;
  49. var newdouble = (double)e.NewValue;
  50. if (control != null && newdouble >= 0.0)
  51. {
  52. control.SlidOpacityPop.SetOpacity(newdouble);
  53. }
  54. }
  55. #endregion
  56. public SlidContent()
  57. {
  58. InitializeComponent();
  59. Loaded += ColorDropBox_Loaded;
  60. Unloaded += ColorDropBox_Unloaded;
  61. MouseDown += ColorDropBox_MouseDown;
  62. SlidOpacityPop = new SlidContentPop();
  63. SlidOpacityPop.OpacityValueChangedHandler += colorPop_OpacityValueChangedHandler;
  64. }
  65. private void colorPop_OpacityValueChangedHandler(object sender, double e)
  66. {
  67. Value = (double)e;
  68. SelectedValueChanged?.Invoke(this, null);
  69. }
  70. private void ColorDropBox_Unloaded(object sender, RoutedEventArgs e)
  71. {
  72. if (layerAdded)
  73. {
  74. RemoveFromLayer();
  75. return;
  76. }
  77. }
  78. private void ColorDropBox_MouseDown(object sender, MouseButtonEventArgs e)
  79. {
  80. e.Handled = true;
  81. if (layerAdded)
  82. {
  83. RemoveFromLayer();
  84. return;
  85. }
  86. AddToLayer();
  87. }
  88. public void ShowLayer(DependencyObject dependencyObject = null)
  89. {
  90. if (layerAdded)
  91. {
  92. RemoveFromLayer();
  93. return;
  94. }
  95. AddToLayer(dependencyObject);
  96. }
  97. private void AddToLayer(DependencyObject dependencyObject = null)
  98. {
  99. Window parentWnd = Window.GetWindow(this);
  100. popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
  101. if (popLayer != null && SlidOpacityPop != null && !layerAdded)
  102. {
  103. if (layerPresent == null)
  104. {
  105. layerPresent = new AdornerPresenter(popLayer);
  106. }
  107. popLayer.Add(layerPresent);
  108. if (popCanvas == null)
  109. {
  110. popCanvas = new Canvas();
  111. popCanvas.Children.Add(SlidOpacityPop);
  112. layerPresent.VisualContent = popCanvas;
  113. }
  114. Point offset = GetOffsetToWindow(dependencyObject);
  115. SlidOpacityPop.Visibility = Visibility.Visible;
  116. SlidOpacityPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
  117. SlidOpacityPop.SetValue(Canvas.TopProperty, offset.Y + this.ActualHeight + 2);
  118. if (offset.X + SlidOpacityPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
  119. {
  120. SlidOpacityPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - SlidOpacityPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
  121. }
  122. else
  123. {
  124. SlidOpacityPop.SetValue(Canvas.LeftProperty, offset.X);
  125. }
  126. layerAdded = true;
  127. }
  128. }
  129. private void RemoveFromLayer()
  130. {
  131. if (popLayer != null && layerPresent != null && layerAdded)
  132. {
  133. popLayer.Remove(layerPresent);
  134. layerAdded = false;
  135. }
  136. }
  137. private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
  138. {
  139. if (popLayer == null)
  140. {
  141. Window parentWnd = Window.GetWindow(this);
  142. if (parentWnd != null && parentWnd.Content is UIElement && SlidOpacityPop != null)
  143. {
  144. parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
  145. {
  146. if (layerAdded)
  147. {
  148. try
  149. {
  150. Window checkWindow = Window.GetWindow(this);
  151. Point clickPoint = param.GetPosition(checkWindow);
  152. Point leftTop = TransformToVisual(checkWindow).Transform(new Point(0, 0));
  153. Point rightBottom = TransformToVisual(checkWindow).Transform(new Point(ActualWidth, ActualHeight));
  154. Point popLeftTop = new Point(Canvas.GetLeft(SlidOpacityPop), Canvas.GetTop(SlidOpacityPop));
  155. Point popRightBottom = new Point(SlidOpacityPop.ActualWidth, SlidOpacityPop.ActualHeight);
  156. Rect dropboxRect = new Rect(leftTop.X, leftTop.Y, rightBottom.X - leftTop.X, rightBottom.Y - leftTop.Y);
  157. Rect popboxRect = new Rect(popLeftTop.X, popLeftTop.Y, popRightBottom.X, popRightBottom.Y);
  158. if (dropboxRect.Contains(clickPoint) || popboxRect.Contains(clickPoint))
  159. {
  160. return;
  161. }
  162. //如果是有展开的UI,超过悬浮窗口,就判断点击的位置是否为展开的UI上;
  163. //例如下拉框展开的选项列表UI超出悬浮窗口
  164. if (SlidOpacityPop != null)
  165. {
  166. if (SlidOpacityPop.isOpenLayout)
  167. return;
  168. }
  169. RemoveFromLayer();
  170. }
  171. catch (Exception ex)
  172. {
  173. }
  174. }
  175. }), true);
  176. }
  177. }
  178. }
  179. public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
  180. {
  181. Window parentWnd = Window.GetWindow(this);
  182. if (dependencyObject != null)
  183. {
  184. var frame = dependencyObject as FrameworkElement;
  185. if (frame != null)
  186. {
  187. return frame.TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  188. }
  189. }
  190. return TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  191. }
  192. public void SetSliOpacity(double newopacity)
  193. {
  194. SlidOpacityPop.OpacitySlider.Value = newopacity;
  195. }
  196. }
  197. }