PopControlHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 List<MenuItem> MenuItems { get; private set; }
  200. private object CommandTarget { get; set; }
  201. public CustomPopMenu(ContextMenu popMenu,object commandTarget)
  202. {
  203. PopMenu = popMenu;
  204. CommandTarget = commandTarget;
  205. GetMenuItems();
  206. }
  207. private void GetMenuItems()
  208. {
  209. if (PopMenu == null || PopMenu.Items == null)
  210. return;
  211. MenuItems = new List<MenuItem>();
  212. foreach (var item in PopMenu.Items)
  213. {
  214. if (item as MenuItem != null)
  215. {
  216. MenuItems.Add(item as MenuItem);
  217. }
  218. }
  219. }
  220. //显示或隐藏所有菜单项
  221. public void AllMenuVisibility(bool isVisual)
  222. {
  223. if (MenuItems == null) return;
  224. foreach (var item in MenuItems)
  225. {
  226. item.Visibility = (isVisual ? Visibility.Visible : Visibility.Collapsed);
  227. }
  228. }
  229. private MenuItem ContainterOfIndex(int index)
  230. {
  231. if (MenuItems == null || MenuItems.Count <= index || index < 0)
  232. return null;
  233. MenuItem menuItem = MenuItems[index] as MenuItem;
  234. return menuItem;
  235. }
  236. private MenuItem ContainterOfTag(string tag)
  237. {
  238. if (MenuItems == null || string.IsNullOrEmpty(tag))
  239. return null;
  240. var menuItem = MenuItems.FirstOrDefault(temp => temp.Tag.ToString() == tag);
  241. return menuItem;
  242. }
  243. public void SetVisibilityProperty(int index, bool isVisual)
  244. {
  245. var menuItem = ContainterOfIndex(index);
  246. if (menuItem != null)
  247. {
  248. menuItem.Visibility = (isVisual?Visibility.Visible:Visibility.Collapsed);
  249. }
  250. }
  251. public void SetVisibilityProperty(string tag, bool isVisual)
  252. {
  253. var menuItem = ContainterOfTag(tag);
  254. if (menuItem != null)
  255. {
  256. menuItem.Visibility = (isVisual ? Visibility.Visible : Visibility.Collapsed);
  257. }
  258. }
  259. public void SetTagProperty(int index, string tag)
  260. {
  261. var menuItem = ContainterOfIndex(index);
  262. if (menuItem != null)
  263. {
  264. menuItem.Tag = tag;
  265. }
  266. }
  267. public void SetHeaderProperty(int index, string header)
  268. {
  269. var menuItem = ContainterOfIndex(index);
  270. if (menuItem != null)
  271. {
  272. menuItem.Header = header;
  273. }
  274. }
  275. //设置UI内容
  276. public void SetMenuUI(int index, UIElement controls)
  277. {
  278. PopMenu.Items.Insert(index, controls);
  279. GetMenuItems();
  280. }
  281. //按索引号设置菜单
  282. public void SetMenuBinding(int index, ICommand command,object comTarget = null)
  283. {
  284. var menuItem = ContainterOfIndex(index);
  285. if (menuItem != null)
  286. {
  287. if (comTarget == null)
  288. {
  289. if (CommandTarget is UIElement)
  290. {
  291. menuItem.CommandTarget = (UIElement)CommandTarget;
  292. }
  293. menuItem.CommandParameter = CommandTarget;
  294. }
  295. else
  296. {
  297. if(comTarget is UIElement)
  298. {
  299. menuItem.CommandTarget = (UIElement)comTarget;
  300. }
  301. menuItem.CommandParameter = comTarget;
  302. }
  303. menuItem.Command = command;
  304. }
  305. }
  306. //按Tag设置菜单
  307. public void SetMenuBinding(string tag, ICommand command, object comTarget = null)
  308. {
  309. var menuItem = ContainterOfTag(tag);
  310. if (menuItem != null)
  311. {
  312. if (comTarget == null)
  313. menuItem.CommandTarget = (UIElement)CommandTarget;
  314. else
  315. menuItem.CommandTarget = (UIElement)comTarget;
  316. menuItem.Command = command;
  317. }
  318. }
  319. //按索引号设置子菜单
  320. public void SetSubMenuBinding(int index, int subIndex, ICommand command, object comTarget = null)
  321. {
  322. var menuItem = ContainterOfIndex(index);
  323. if (menuItem != null && menuItem.Items != null && menuItem.Items.Count > 0 && subIndex >= 0)
  324. {
  325. if(menuItem.Items.Count > subIndex)
  326. {
  327. var item = menuItem.Items[subIndex];
  328. if(item is RadioButton)
  329. {
  330. var radioItem = item as RadioButton;
  331. radioItem.CommandParameter = radioItem.IsChecked;
  332. if (comTarget == null)
  333. radioItem.DataContext = (UIElement)CommandTarget;
  334. else
  335. radioItem.DataContext = (UIElement)comTarget;
  336. radioItem.Command = command;
  337. }
  338. else if(item is MenuItem)
  339. {
  340. var menuitem = item as MenuItem;
  341. if (comTarget == null)
  342. menuitem.CommandTarget = (UIElement)CommandTarget;
  343. else
  344. menuitem.CommandTarget = (UIElement)comTarget;
  345. menuItem.Command = command;
  346. }
  347. }
  348. }
  349. }
  350. public void SetSubMenuBinding(string tag, string subTag, ICommand command, object comTarget = null)
  351. {
  352. var menuItem = ContainterOfTag(tag);
  353. if (menuItem != null && menuItem.Items != null && menuItem.Items.Count > 0)
  354. {
  355. object theItem = null;
  356. foreach(var item in menuItem.Items)
  357. {
  358. if(item is RadioButton)
  359. {
  360. var radioItem = item as RadioButton;
  361. if(radioItem.Tag != null && radioItem.Tag.ToString() == subTag)
  362. {
  363. theItem = item;
  364. break;
  365. }
  366. }
  367. else if(item is MenuItem)
  368. {
  369. var menuitem = item as MenuItem;
  370. if (menuitem.Tag != null && menuitem.Tag.ToString() == subTag)
  371. {
  372. theItem = item;
  373. break;
  374. }
  375. }
  376. }
  377. if(theItem != null)
  378. {
  379. if (theItem is RadioButton)
  380. {
  381. var radioTheItem = theItem as RadioButton;
  382. radioTheItem.CommandParameter = radioTheItem.IsChecked;
  383. if (comTarget == null)
  384. radioTheItem.DataContext = (UIElement)CommandTarget;
  385. else
  386. radioTheItem.DataContext = (UIElement)comTarget;
  387. radioTheItem.Command = command;
  388. }
  389. else if(theItem is MenuItem)
  390. {
  391. var menuTheitem = theItem as MenuItem;
  392. if (comTarget == null)
  393. menuTheitem.CommandTarget = (UIElement)CommandTarget;
  394. else
  395. menuTheitem.CommandTarget = (UIElement)comTarget;
  396. menuTheitem.Command = command;
  397. }
  398. }
  399. }
  400. }
  401. }
  402. #endregion
  403. }