SlidContent.xaml.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. BtnOpeContenu.IsChecked = true;
  128. }
  129. }
  130. private void RemoveFromLayer()
  131. {
  132. if (popLayer != null && layerPresent != null && layerAdded)
  133. {
  134. popLayer.Remove(layerPresent);
  135. layerAdded = false;
  136. BtnOpeContenu.IsChecked = false;
  137. }
  138. }
  139. private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
  140. {
  141. if (popLayer == null)
  142. {
  143. Window parentWnd = Window.GetWindow(this);
  144. if (parentWnd != null && parentWnd.Content is UIElement && SlidOpacityPop != null)
  145. {
  146. parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
  147. {
  148. if (layerAdded)
  149. {
  150. try
  151. {
  152. Window checkWindow = Window.GetWindow(this);
  153. Point clickPoint = param.GetPosition(checkWindow);
  154. Point leftTop = TransformToVisual(checkWindow).Transform(new Point(0, 0));
  155. Point rightBottom = TransformToVisual(checkWindow).Transform(new Point(ActualWidth, ActualHeight));
  156. Point popLeftTop = new Point(Canvas.GetLeft(SlidOpacityPop), Canvas.GetTop(SlidOpacityPop));
  157. Point popRightBottom = new Point(SlidOpacityPop.ActualWidth, SlidOpacityPop.ActualHeight);
  158. Rect dropboxRect = new Rect(leftTop.X, leftTop.Y, rightBottom.X - leftTop.X, rightBottom.Y - leftTop.Y);
  159. Rect popboxRect = new Rect(popLeftTop.X, popLeftTop.Y, popRightBottom.X, popRightBottom.Y);
  160. if (dropboxRect.Contains(clickPoint) || popboxRect.Contains(clickPoint))
  161. {
  162. return;
  163. }
  164. //如果是有展开的UI,超过悬浮窗口,就判断点击的位置是否为展开的UI上;
  165. //例如下拉框展开的选项列表UI超出悬浮窗口
  166. if (SlidOpacityPop != null)
  167. {
  168. if (SlidOpacityPop.isOpenLayout)
  169. return;
  170. }
  171. RemoveFromLayer();
  172. }
  173. catch (Exception ex)
  174. {
  175. }
  176. }
  177. }), true);
  178. }
  179. }
  180. }
  181. public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
  182. {
  183. Window parentWnd = Window.GetWindow(this);
  184. if (dependencyObject != null)
  185. {
  186. var frame = dependencyObject as FrameworkElement;
  187. if (frame != null)
  188. {
  189. return frame.TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  190. }
  191. }
  192. return TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  193. }
  194. public void SetSliOpacity(double newopacity)
  195. {
  196. SlidOpacityPop.OpacitySlider.Value = newopacity;
  197. }
  198. private void BtnOpeContenu_Click(object sender, RoutedEventArgs e)
  199. {
  200. if (layerAdded)
  201. {
  202. RemoveFromLayer();
  203. return;
  204. }
  205. AddToLayer();
  206. }
  207. }
  208. }