PopControlHelper.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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_Master.CustomControl.ColorDropBox;
  12. namespace PDF_Master.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. var menuItem = ContainterOfIndex(index);
  279. if (menuItem != null)
  280. {
  281. menuItem.Header = controls;
  282. }
  283. }
  284. //按索引号设置菜单
  285. public void SetMenuBinding(int index, ICommand command,object comTarget = null)
  286. {
  287. var menuItem = ContainterOfIndex(index);
  288. if (menuItem != null)
  289. {
  290. if (comTarget == null)
  291. {
  292. if (CommandTarget is UIElement)
  293. {
  294. menuItem.CommandTarget = (UIElement)CommandTarget;
  295. }
  296. menuItem.CommandParameter = CommandTarget;
  297. }
  298. else
  299. {
  300. if(comTarget is UIElement)
  301. {
  302. menuItem.CommandTarget = (UIElement)comTarget;
  303. }
  304. menuItem.CommandParameter = comTarget;
  305. }
  306. menuItem.Command = command;
  307. }
  308. }
  309. //按Tag设置菜单
  310. public void SetMenuBinding(string tag, ICommand command, object comTarget = null)
  311. {
  312. var menuItem = ContainterOfTag(tag);
  313. if (menuItem != null)
  314. {
  315. if (comTarget == null)
  316. menuItem.CommandTarget = (UIElement)CommandTarget;
  317. else
  318. menuItem.CommandTarget = (UIElement)comTarget;
  319. menuItem.Command = command;
  320. }
  321. }
  322. //按索引号设置子菜单
  323. public void SetSubMenuBinding(int index, int subIndex, ICommand command, object comTarget = null,bool isChecked = false)
  324. {
  325. var menuItem = ContainterOfIndex(index);
  326. if (menuItem != null && menuItem.Items != null && menuItem.Items.Count > 0 && subIndex >= 0)
  327. {
  328. if(menuItem.Items.Count > subIndex)
  329. {
  330. var item = menuItem.Items[subIndex];
  331. if(item is RadioButton)
  332. {
  333. var radioItem = item as RadioButton;
  334. radioItem.IsChecked = isChecked;
  335. if (CommandTarget == null)
  336. CommandTarget = comTarget;
  337. radioItem.DataContext = CommandTarget;
  338. radioItem.CommandParameter = radioItem;
  339. radioItem.Command = command;
  340. }
  341. else if(item is MenuItem)
  342. {
  343. var submenuitem = item as MenuItem;
  344. if (comTarget == null)
  345. {
  346. if (CommandTarget is UIElement)
  347. {
  348. submenuitem.CommandTarget = (UIElement)CommandTarget;
  349. }
  350. if (isChecked)
  351. {
  352. submenuitem.DataContext = CommandTarget;
  353. submenuitem.CommandParameter = submenuitem;//isChecked为true时,可拿MenuItem
  354. }
  355. else
  356. {
  357. submenuitem.DataContext = CommandTarget;
  358. submenuitem.CommandParameter = CommandTarget;
  359. }
  360. }
  361. else
  362. {
  363. if (comTarget is UIElement)
  364. {
  365. submenuitem.CommandTarget = (UIElement)comTarget;
  366. }
  367. if (isChecked)
  368. {
  369. submenuitem.CommandParameter = submenuitem;//isChecked为true时,可拿MenuItem
  370. }
  371. else
  372. {
  373. submenuitem.CommandParameter = CommandTarget;
  374. }
  375. }
  376. submenuitem.Command = command;
  377. }
  378. }
  379. }
  380. }
  381. public void SetSubMenuBinding(string tag, string subTag, ICommand command, object comTarget = null)
  382. {
  383. var menuItem = ContainterOfTag(tag);
  384. if (menuItem != null && menuItem.Items != null && menuItem.Items.Count > 0)
  385. {
  386. object theItem = null;
  387. foreach(var item in menuItem.Items)
  388. {
  389. if(item is RadioButton)
  390. {
  391. var radioItem = item as RadioButton;
  392. if(radioItem.Tag != null && radioItem.Tag.ToString() == subTag)
  393. {
  394. theItem = item;
  395. break;
  396. }
  397. }
  398. else if(item is MenuItem)
  399. {
  400. var menuitem = item as MenuItem;
  401. if (menuitem.Tag != null && menuitem.Tag.ToString() == subTag)
  402. {
  403. theItem = item;
  404. break;
  405. }
  406. }
  407. }
  408. if(theItem != null)
  409. {
  410. if (theItem is RadioButton)
  411. {
  412. var radioTheItem = theItem as RadioButton;
  413. radioTheItem.CommandParameter = radioTheItem.IsChecked;
  414. if (comTarget == null)
  415. radioTheItem.DataContext = (UIElement)CommandTarget;
  416. else
  417. radioTheItem.DataContext = (UIElement)comTarget;
  418. radioTheItem.Command = command;
  419. }
  420. else if(theItem is MenuItem)
  421. {
  422. var menuTheitem = theItem as MenuItem;
  423. if (comTarget == null)
  424. menuTheitem.CommandTarget = (UIElement)CommandTarget;
  425. else
  426. menuTheitem.CommandTarget = (UIElement)comTarget;
  427. menuTheitem.Command = command;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. //菜单按钮类
  434. public class CusMenuItem
  435. {
  436. public Control control { get; private set; }//菜单按钮控件
  437. public int Level { get; private set; }//菜单按钮的层级数。Level为0是一级菜单
  438. public object Parameter { get; private set; }//核心处理的参数
  439. public object tag { get; private set; }//菜单控件Tag
  440. public void SetFlagControl(Control control)
  441. {
  442. this.control = control;
  443. tag = control.Tag;
  444. }
  445. public void SetParameter(object parameter)
  446. {
  447. Parameter = parameter;
  448. }
  449. }
  450. public class PopMenu
  451. {
  452. /// <summary>
  453. /// 菜单栏
  454. /// </summary>
  455. public ContextMenu ContMenu { get; private set; }
  456. /// <summary>
  457. /// 菜单按钮集合
  458. /// </summary>
  459. public List<CusMenuItem> Controls { get; private set; }
  460. public PopMenu(ContextMenu popMenu)
  461. {
  462. ContMenu = popMenu;
  463. }
  464. /// <summary>
  465. /// 打开右键菜单(parameter:commmand核心参数,uiParameter:响应复制粘贴等指令的参数)
  466. /// </summary>
  467. public ContextMenu OpenMenu(object parameter, object uiParameter = null)
  468. {
  469. if (Controls == null) return null;
  470. foreach(var item in Controls)
  471. {
  472. item.control.DataContext = parameter;
  473. if (uiParameter is UIElement && uiParameter != null)
  474. BindingParameter(item, parameter, (UIElement)uiParameter);
  475. else
  476. BindingParameter(item, parameter);
  477. }
  478. return ContMenu;
  479. }
  480. /// <summary>
  481. /// 右键菜单:创建菜单按钮、单选按钮等
  482. /// </summary>
  483. public CusMenuItem AddItem(Control control)
  484. {
  485. if (control == null) return null;
  486. CusMenuItem controlMenu = new CusMenuItem();
  487. controlMenu.SetFlagControl(control);
  488. if (Controls == null)
  489. Controls = new List<CusMenuItem>();
  490. ContMenu.Items.Add(controlMenu.control);
  491. Controls.Add(controlMenu);
  492. return controlMenu;
  493. }
  494. //在某菜单按钮的子级,新增子菜单按钮
  495. public CusMenuItem AddChild(string parentName, Control child)
  496. {
  497. if (Controls == null || child == null) return null;
  498. CusMenuItem childItem = null;
  499. foreach (var item in Controls)
  500. {
  501. var menu = item.control as MenuItem;
  502. if (menu != null && menu.Name == parentName)
  503. {
  504. childItem = new CusMenuItem();
  505. childItem.SetFlagControl(child);
  506. menu.Items.Add(child);
  507. Controls.Add(childItem);
  508. break;
  509. }
  510. }
  511. return childItem;
  512. }
  513. //菜单按钮事件绑定
  514. public void BindingEvent(CusMenuItem controlMenu,ICommand command)
  515. {
  516. if (controlMenu == null) return;
  517. if(controlMenu.control is RadioButton)
  518. {
  519. var Btn = (RadioButton)controlMenu.control;
  520. Btn.CommandParameter = controlMenu;
  521. Btn.Command = command;
  522. }
  523. else if(controlMenu.control is MenuItem)
  524. {
  525. var Btn = (MenuItem)controlMenu.control;
  526. Btn.CommandParameter = controlMenu;
  527. Btn.Command = command;
  528. }
  529. }
  530. /// <summary>
  531. /// 右键菜单时,菜单按钮command需要的参数
  532. /// </summary>
  533. private void BindingParameter(CusMenuItem controlMenu, object CommandParameter, UIElement uIElement = null)
  534. {
  535. if (controlMenu == null) return;
  536. if (controlMenu.control is RadioButton)
  537. {
  538. var Btn = (RadioButton)controlMenu.control;
  539. if (uIElement != null)
  540. Btn.CommandTarget = uIElement;
  541. Btn.CommandParameter = controlMenu;
  542. }
  543. else if (controlMenu.control is MenuItem)
  544. {
  545. var Btn = (MenuItem)controlMenu.control;
  546. if(uIElement != null)
  547. Btn.CommandTarget = uIElement;
  548. Btn.CommandParameter = controlMenu;
  549. }
  550. controlMenu.SetParameter(CommandParameter);
  551. }
  552. /// <summary>
  553. /// 菜单按钮是否可见
  554. /// </summary>
  555. public void SetVisual(string controlName, bool isVisible)
  556. {
  557. if (Controls == null) return;
  558. foreach(var item in Controls)
  559. {
  560. if(item.control.Name == controlName)
  561. {
  562. item.control.Visibility = (isVisible?Visibility.Visible:Visibility.Collapsed);
  563. break;
  564. }
  565. }
  566. }
  567. /// <summary>
  568. /// 菜单按钮选中状态(若菜单为单选按钮)
  569. /// </summary>
  570. public void SetIsChecked(string controlName, bool isChecked)
  571. {
  572. if (Controls == null) return;
  573. foreach (var item in Controls)
  574. {
  575. if (item.control.Name == controlName && (item.control as RadioButton) != null)
  576. {
  577. var btn = item.control as RadioButton;
  578. btn.IsChecked = isChecked;
  579. break;
  580. }
  581. }
  582. }
  583. }
  584. #endregion
  585. }