PopControlHelper.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using static PDF_Office.CustomControl.ColorDropBox;
  12. namespace PDF_Office.Helper
  13. {
  14. #region 悬浮窗口
  15. public class AdornerPresenter : Adorner
  16. {
  17. private VisualCollection VisualChildren;
  18. private ContentPresenter Content;
  19. public AdornerPresenter(UIElement adornedElement) : base(adornedElement)
  20. {
  21. VisualChildren = new VisualCollection(this);
  22. Content = new ContentPresenter();
  23. VisualChildren.Add(Content);
  24. }
  25. protected override Size MeasureOverride(Size constraint)
  26. {
  27. Content.Measure(constraint);
  28. return Content.DesiredSize;
  29. }
  30. protected override Size ArrangeOverride(Size finalSize)
  31. {
  32. Content.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
  33. return Content.RenderSize;
  34. }
  35. protected override Visual GetVisualChild(int index)
  36. {
  37. return VisualChildren[index];
  38. }
  39. protected override int VisualChildrenCount
  40. {
  41. get
  42. {
  43. return VisualChildren.Count;
  44. }
  45. }
  46. public object VisualContent
  47. {
  48. get
  49. {
  50. return Content.Content;
  51. }
  52. set
  53. {
  54. Content.Content = value;
  55. }
  56. }
  57. }
  58. //悬浮窗口
  59. //hwnd:鼠标点击区域所在的窗口或控件
  60. //targetPop:悬浮窗口
  61. //offset:位于某窗口的位置,默认为整个主窗口的位置
  62. //Helper.PopControlHelper popControlHelper;
  63. // if (popControlHelper == null)
  64. // popControlHelper = new Helper.PopControlHelper(sender as Control, new CustomControl.CompositeControl.SlidContentPop());
  65. // else
  66. // popControlHelper.UpdatePointOffset(Mouse.GetPosition(App.Current.MainWindow));
  67. // popControlHelper.ShowLayer();
  68. public class PopControlHelper : UserControl
  69. {
  70. private AdornerLayer popLayer;
  71. private AdornerPresenter layerPresent;
  72. private bool layerAdded = false;
  73. private Canvas popCanvas;
  74. private UserControl TargetPop;//悬浮窗口
  75. private Control HwndControl;//悬浮窗口的上一级窗口,即鼠标点击的区域所在的窗口
  76. private Point offset;//悬浮窗口,位于某窗口的位置
  77. public PopControlHelper(Control hwnd, UserControl targetPop, Point offset)
  78. {
  79. this.offset = offset;
  80. HwndControl = hwnd;
  81. TargetPop = targetPop;
  82. InitData();
  83. }
  84. public PopControlHelper(Control hwnd, UserControl targetPop)
  85. {
  86. //默认为主窗口的位置
  87. offset = Mouse.GetPosition(App.Current.MainWindow);
  88. HwndControl = hwnd;
  89. TargetPop = targetPop;
  90. InitData();
  91. }
  92. public void UpdatePointOffset(Point point)
  93. {
  94. offset = point;
  95. }
  96. public void ShowLayer(DependencyObject dependencyObject = null)
  97. {
  98. if (layerAdded)
  99. {
  100. RemoveFromLayer();
  101. return;
  102. }
  103. AddToLayer(dependencyObject);
  104. }
  105. public void RemoveLayer()
  106. {
  107. if (layerAdded)
  108. {
  109. RemoveFromLayer();
  110. return;
  111. }
  112. }
  113. //绑定鼠标点击整个窗口区域的事件
  114. private void InitData()
  115. {
  116. if (popLayer == null)
  117. {
  118. Window parentWnd = Window.GetWindow(HwndControl);
  119. if (parentWnd != null && parentWnd.Content is UIElement && TargetPop != null)
  120. {
  121. parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
  122. {
  123. if (layerAdded)
  124. {
  125. try
  126. {
  127. Point p = Mouse.GetPosition(TargetPop);
  128. double width = 0;
  129. double height = 0;
  130. if (double.IsNaN(TargetPop.Width) == false && double.IsNaN(TargetPop.Height) == false)
  131. {
  132. width = TargetPop.Width;
  133. height = TargetPop.Height;
  134. }
  135. else
  136. {
  137. width = TargetPop.ActualWidth;
  138. height = TargetPop.ActualHeight;
  139. }
  140. if (p.X < 0 || p.Y < 0 || p.X > width || p.Y > height)
  141. {
  142. RemoveFromLayer();
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. }
  148. }
  149. }), true);
  150. }
  151. }
  152. }
  153. private void AddToLayer(DependencyObject dependencyObject = null)
  154. {
  155. Window parentWnd = Window.GetWindow(HwndControl);
  156. popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
  157. if (popLayer != null && TargetPop != null && !layerAdded)
  158. {
  159. if (layerPresent == null)
  160. {
  161. layerPresent = new AdornerPresenter(popLayer);
  162. }
  163. popLayer.Add(layerPresent);
  164. if (popCanvas == null)
  165. {
  166. popCanvas = new Canvas();
  167. popCanvas.Children.Add(TargetPop);
  168. layerPresent.VisualContent = popCanvas;
  169. }
  170. TargetPop.Visibility = Visibility.Visible;
  171. TargetPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
  172. TargetPop.SetValue(Canvas.TopProperty, offset.Y + HwndControl.ActualHeight + 2);
  173. if (offset.X + TargetPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
  174. {
  175. TargetPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - TargetPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
  176. }
  177. else
  178. {
  179. TargetPop.SetValue(Canvas.LeftProperty, offset.X);
  180. }
  181. layerAdded = true;
  182. }
  183. }
  184. private void RemoveFromLayer()
  185. {
  186. if (popLayer != null && layerPresent != null && layerAdded)
  187. {
  188. popLayer.Remove(layerPresent);
  189. layerAdded = false;
  190. }
  191. }
  192. }
  193. #endregion
  194. #region 自定义菜单
  195. //自定义菜单:事件绑定、内容等
  196. public class CustomPopMenu
  197. {
  198. public ContextMenu PopMenu { get; private set; }
  199. public CustomPopMenu(ContextMenu popMenu)
  200. {
  201. PopMenu = popMenu;
  202. }
  203. private MenuItem ContainterOfIndex(int index)
  204. {
  205. if (PopMenu == null || PopMenu.Items.Count < index || index < 0)
  206. return null;
  207. MenuItem menuItem = PopMenu.Items[index] as MenuItem;
  208. return menuItem;
  209. }
  210. public void SetVisibilityProperty(int index, bool isVisual)
  211. {
  212. var menuItem = ContainterOfIndex(index);
  213. if (menuItem != null)
  214. {
  215. menuItem.Visibility = (isVisual?Visibility.Visible:Visibility.Collapsed);
  216. }
  217. }
  218. public void SetHeaderProperty(int index, string header)
  219. {
  220. var menuItem = ContainterOfIndex(index);
  221. if (menuItem != null)
  222. {
  223. menuItem.Header = header;
  224. }
  225. }
  226. public void SetMenuBinding(int index, object commandTarget, ICommand command)
  227. {
  228. var menuItem = ContainterOfIndex(index);
  229. if (menuItem != null)
  230. {
  231. menuItem.CommandTarget = (UIElement)commandTarget;
  232. menuItem.Command = command;
  233. }
  234. }
  235. }
  236. #endregion
  237. }