SlidContent.xaml.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace PDF_Office.CustomControl.CompositeControl
  18. {
  19. public class AdornerPresenter : Adorner
  20. {
  21. private VisualCollection VisualChildren;
  22. private ContentPresenter Content;
  23. public AdornerPresenter(UIElement adornedElement) : base(adornedElement)
  24. {
  25. VisualChildren = new VisualCollection(this);
  26. Content = new ContentPresenter();
  27. VisualChildren.Add(Content);
  28. }
  29. protected override Size MeasureOverride(Size constraint)
  30. {
  31. Content.Measure(constraint);
  32. return Content.DesiredSize;
  33. }
  34. protected override Size ArrangeOverride(Size finalSize)
  35. {
  36. Content.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
  37. return Content.RenderSize;
  38. }
  39. protected override Visual GetVisualChild(int index)
  40. {
  41. return VisualChildren[index];
  42. }
  43. protected override int VisualChildrenCount
  44. {
  45. get
  46. {
  47. return VisualChildren.Count;
  48. }
  49. }
  50. public object VisualContent
  51. {
  52. get
  53. {
  54. return Content.Content;
  55. }
  56. set
  57. {
  58. Content.Content = value;
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// ThicknessContent.xaml 的交互逻辑
  64. /// </summary>
  65. public partial class SlidContent : UserControl
  66. {
  67. private AdornerLayer popLayer;
  68. private AdornerPresenter layerPresent;
  69. private bool layerAdded = false;
  70. private Canvas popCanvas;
  71. private SlidContentPop colorPop;
  72. public EventHandler<double> SelectedValueChanged;
  73. public event PropertyChangedEventHandler PropertyChanged;
  74. protected void OnPropertyChanged([CallerMemberName] string name = null)
  75. {
  76. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  77. }
  78. private Color? _selectedColor;
  79. public Color? SelectedColor
  80. {
  81. get
  82. {
  83. return _selectedColor;
  84. }
  85. set
  86. {
  87. if (_selectedColor != value)
  88. {
  89. _selectedColor = value;
  90. OnPropertyChanged("SelectedColor");
  91. }
  92. }
  93. }
  94. public SlidContent()
  95. {
  96. InitializeComponent();
  97. Loaded += ColorDropBox_Loaded;
  98. Unloaded += ColorDropBox_Unloaded;
  99. MouseDown += ColorDropBox_MouseDown;
  100. colorPop = new SlidContentPop();
  101. colorPop.OpacityValueChangedHandler += colorPop_OpacityValueChangedHandler;
  102. }
  103. private void colorPop_OpacityValueChangedHandler(object sender, double e)
  104. {
  105. SelectedValueChanged?.Invoke(this, e);
  106. }
  107. private void ColorDropBox_Unloaded(object sender, RoutedEventArgs e)
  108. {
  109. if (layerAdded)
  110. {
  111. RemoveFromLayer();
  112. return;
  113. }
  114. }
  115. private void ColorDropBox_MouseDown(object sender, MouseButtonEventArgs e)
  116. {
  117. e.Handled = true;
  118. if (layerAdded)
  119. {
  120. RemoveFromLayer();
  121. return;
  122. }
  123. AddToLayer();
  124. }
  125. public void ShowLayer(DependencyObject dependencyObject = null)
  126. {
  127. if (layerAdded)
  128. {
  129. RemoveFromLayer();
  130. return;
  131. }
  132. AddToLayer(dependencyObject);
  133. }
  134. private void AddToLayer(DependencyObject dependencyObject = null)
  135. {
  136. Window parentWnd = Window.GetWindow(this);
  137. popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
  138. if (popLayer != null && colorPop != null && !layerAdded)
  139. {
  140. if (layerPresent == null)
  141. {
  142. layerPresent = new AdornerPresenter(popLayer);
  143. }
  144. popLayer.Add(layerPresent);
  145. if (popCanvas == null)
  146. {
  147. popCanvas = new Canvas();
  148. popCanvas.Children.Add(colorPop);
  149. layerPresent.VisualContent = popCanvas;
  150. // colorPop.ColorSelected += ColorPop_ColorSelected;
  151. }
  152. Point offset = GetOffsetToWindow(dependencyObject);
  153. colorPop.Visibility = Visibility.Visible;
  154. colorPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
  155. colorPop.SetValue(Canvas.TopProperty, offset.Y + this.ActualHeight + 2);
  156. if (offset.X + colorPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
  157. {
  158. colorPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - colorPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
  159. }
  160. else
  161. {
  162. colorPop.SetValue(Canvas.LeftProperty, offset.X);
  163. }
  164. layerAdded = true;
  165. }
  166. }
  167. private void RemoveFromLayer()
  168. {
  169. if (popLayer != null && layerPresent != null && layerAdded)
  170. {
  171. popLayer.Remove(layerPresent);
  172. layerAdded = false;
  173. }
  174. }
  175. private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
  176. {
  177. if (popLayer == null)
  178. {
  179. Window parentWnd = Window.GetWindow(this);
  180. if (parentWnd != null && parentWnd.Content is UIElement && colorPop != null)
  181. {
  182. parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
  183. {
  184. if (layerAdded)
  185. {
  186. try
  187. {
  188. Window checkWindow = Window.GetWindow(this);
  189. Point clickPoint = param.GetPosition(checkWindow);
  190. Point leftTop = TransformToVisual(checkWindow).Transform(new Point(0, 0));
  191. Point rightBottom = TransformToVisual(checkWindow).Transform(new Point(ActualWidth, ActualHeight));
  192. Point popLeftTop = new Point(Canvas.GetLeft(colorPop), Canvas.GetTop(colorPop));
  193. Point popRightBottom = new Point(colorPop.ActualWidth, colorPop.ActualHeight);
  194. Rect dropboxRect = new Rect(leftTop.X, leftTop.Y, rightBottom.X - leftTop.X, rightBottom.Y - leftTop.Y);
  195. Rect popboxRect = new Rect(popLeftTop.X, popLeftTop.Y, popRightBottom.X, popRightBottom.Y);
  196. if (dropboxRect.Contains(clickPoint) || popboxRect.Contains(clickPoint))
  197. {
  198. return;
  199. }
  200. RemoveFromLayer();
  201. }
  202. catch (Exception ex)
  203. {
  204. }
  205. }
  206. }), true);
  207. }
  208. }
  209. }
  210. private void ColorPop_ColorSelected(object sender, Color e)
  211. {
  212. if (SelectedColor != e)
  213. {
  214. //NormalColorRectControl.Visibility = Visibility.Collapsed;
  215. //TransparentRectControl.Visibility = Visibility.Collapsed;
  216. //if (e != Colors.Transparent)
  217. //{
  218. // NormalColorRectControl.Visibility = Visibility.Visible;
  219. // NormalColorRectControl.Fill = new SolidColorBrush(e);
  220. //}
  221. //else
  222. //{
  223. // TransparentRectControl.Visibility = Visibility.Visible;
  224. //}
  225. SelectedColor = e;
  226. // SelectedValueChanged?.Invoke(this, SelectedColor);
  227. }
  228. RemoveFromLayer();
  229. }
  230. public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
  231. {
  232. Window parentWnd = Window.GetWindow(this);
  233. if (dependencyObject != null)
  234. {
  235. var frame = dependencyObject as FrameworkElement;
  236. if (frame != null)
  237. {
  238. return frame.TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  239. }
  240. }
  241. return TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  242. }
  243. public void SetThemeColors(List<Color> themeColorList)
  244. {
  245. if (colorPop != null)
  246. {
  247. // colorPop.ThemeColors.Clear();
  248. //foreach (Color themeColor in themeColorList)
  249. //{
  250. // colorPop.ThemeColors.Add(themeColor);
  251. //}
  252. }
  253. }
  254. public void SetSliOpacity(double newopacity)
  255. {
  256. colorPop.OpacitySlider.Value = newopacity;
  257. }
  258. }
  259. }