SlidContent.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #endregion
  40. public SlidContent()
  41. {
  42. InitializeComponent();
  43. Loaded += ColorDropBox_Loaded;
  44. Unloaded += ColorDropBox_Unloaded;
  45. MouseDown += ColorDropBox_MouseDown;
  46. SlidOpacityPop = new SlidContentPop();
  47. SlidOpacityPop.OpacityValueChangedHandler += colorPop_OpacityValueChangedHandler;
  48. }
  49. private void colorPop_OpacityValueChangedHandler(object sender, double e)
  50. {
  51. Value = (double)e;
  52. SelectedValueChanged?.Invoke(this, null);
  53. }
  54. private void ColorDropBox_Unloaded(object sender, RoutedEventArgs e)
  55. {
  56. if (layerAdded)
  57. {
  58. RemoveFromLayer();
  59. return;
  60. }
  61. }
  62. private void ColorDropBox_MouseDown(object sender, MouseButtonEventArgs e)
  63. {
  64. e.Handled = true;
  65. if (layerAdded)
  66. {
  67. RemoveFromLayer();
  68. return;
  69. }
  70. AddToLayer();
  71. }
  72. public void ShowLayer(DependencyObject dependencyObject = null)
  73. {
  74. if (layerAdded)
  75. {
  76. RemoveFromLayer();
  77. return;
  78. }
  79. AddToLayer(dependencyObject);
  80. }
  81. private void AddToLayer(DependencyObject dependencyObject = null)
  82. {
  83. Window parentWnd = Window.GetWindow(this);
  84. popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
  85. if (popLayer != null && SlidOpacityPop != null && !layerAdded)
  86. {
  87. if (layerPresent == null)
  88. {
  89. layerPresent = new AdornerPresenter(popLayer);
  90. }
  91. popLayer.Add(layerPresent);
  92. if (popCanvas == null)
  93. {
  94. popCanvas = new Canvas();
  95. popCanvas.Children.Add(SlidOpacityPop);
  96. layerPresent.VisualContent = popCanvas;
  97. }
  98. Point offset = GetOffsetToWindow(dependencyObject);
  99. SlidOpacityPop.Visibility = Visibility.Visible;
  100. SlidOpacityPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
  101. SlidOpacityPop.SetValue(Canvas.TopProperty, offset.Y + this.ActualHeight + 2);
  102. if (offset.X + SlidOpacityPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
  103. {
  104. SlidOpacityPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - SlidOpacityPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
  105. }
  106. else
  107. {
  108. SlidOpacityPop.SetValue(Canvas.LeftProperty, offset.X);
  109. }
  110. layerAdded = true;
  111. }
  112. }
  113. private void RemoveFromLayer()
  114. {
  115. if (popLayer != null && layerPresent != null && layerAdded)
  116. {
  117. popLayer.Remove(layerPresent);
  118. layerAdded = false;
  119. }
  120. }
  121. private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
  122. {
  123. if (popLayer == null)
  124. {
  125. Window parentWnd = Window.GetWindow(this);
  126. if (parentWnd != null && parentWnd.Content is UIElement && SlidOpacityPop != null)
  127. {
  128. parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
  129. {
  130. if (layerAdded)
  131. {
  132. try
  133. {
  134. Window checkWindow = Window.GetWindow(this);
  135. Point clickPoint = param.GetPosition(checkWindow);
  136. Point leftTop = TransformToVisual(checkWindow).Transform(new Point(0, 0));
  137. Point rightBottom = TransformToVisual(checkWindow).Transform(new Point(ActualWidth, ActualHeight));
  138. Point popLeftTop = new Point(Canvas.GetLeft(SlidOpacityPop), Canvas.GetTop(SlidOpacityPop));
  139. Point popRightBottom = new Point(SlidOpacityPop.ActualWidth, SlidOpacityPop.ActualHeight);
  140. Rect dropboxRect = new Rect(leftTop.X, leftTop.Y, rightBottom.X - leftTop.X, rightBottom.Y - leftTop.Y);
  141. Rect popboxRect = new Rect(popLeftTop.X, popLeftTop.Y, popRightBottom.X, popRightBottom.Y);
  142. if (dropboxRect.Contains(clickPoint) || popboxRect.Contains(clickPoint))
  143. {
  144. return;
  145. }
  146. //如果是有展开的UI,超过悬浮窗口,就判断点击的位置是否为展开的UI上;
  147. //例如下拉框展开的选项列表UI超出悬浮窗口
  148. if (SlidOpacityPop != null)
  149. {
  150. if (SlidOpacityPop.isOpenLayout)
  151. return;
  152. }
  153. RemoveFromLayer();
  154. }
  155. catch (Exception ex)
  156. {
  157. }
  158. }
  159. }), true);
  160. }
  161. }
  162. }
  163. public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
  164. {
  165. Window parentWnd = Window.GetWindow(this);
  166. if (dependencyObject != null)
  167. {
  168. var frame = dependencyObject as FrameworkElement;
  169. if (frame != null)
  170. {
  171. return frame.TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  172. }
  173. }
  174. return TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  175. }
  176. public void SetSliOpacity(double newopacity)
  177. {
  178. SlidOpacityPop.OpacitySlider.Value = newopacity;
  179. }
  180. }
  181. }