123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using static PDF_Office.CustomControl.ColorDropBox;
- namespace PDF_Office.Helper
- {
- #region 悬浮窗口
- public class AdornerPresenter : Adorner
- {
- private VisualCollection VisualChildren;
- private ContentPresenter Content;
- public AdornerPresenter(UIElement adornedElement) : base(adornedElement)
- {
- VisualChildren = new VisualCollection(this);
- Content = new ContentPresenter();
- VisualChildren.Add(Content);
- }
- protected override Size MeasureOverride(Size constraint)
- {
- Content.Measure(constraint);
- return Content.DesiredSize;
- }
- protected override Size ArrangeOverride(Size finalSize)
- {
- Content.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
- return Content.RenderSize;
- }
- protected override Visual GetVisualChild(int index)
- {
- return VisualChildren[index];
- }
- protected override int VisualChildrenCount
- {
- get
- {
- return VisualChildren.Count;
- }
- }
- public object VisualContent
- {
- get
- {
- return Content.Content;
- }
- set
- {
- Content.Content = value;
- }
- }
- }
- //悬浮窗口
- //hwnd:鼠标点击区域所在的窗口或控件
- //targetPop:悬浮窗口
- //offset:位于某窗口的位置,默认为整个主窗口的位置
- //Helper.PopControlHelper popControlHelper;
- // if (popControlHelper == null)
- // popControlHelper = new Helper.PopControlHelper(sender as Control, new CustomControl.CompositeControl.SlidContentPop());
- // else
- // popControlHelper.UpdatePointOffset(Mouse.GetPosition(App.Current.MainWindow));
- // popControlHelper.ShowLayer();
- public class PopControlHelper : UserControl
- {
- private AdornerLayer popLayer;
- private AdornerPresenter layerPresent;
- private bool layerAdded = false;
- private Canvas popCanvas;
- private UserControl TargetPop;//悬浮窗口
- private Control HwndControl;//悬浮窗口的上一级窗口,即鼠标点击的区域所在的窗口
- private Point offset;//悬浮窗口,位于某窗口的位置
- public PopControlHelper(Control hwnd, UserControl targetPop, Point offset)
- {
- this.offset = offset;
- HwndControl = hwnd;
- TargetPop = targetPop;
- InitData();
- }
- public PopControlHelper(Control hwnd, UserControl targetPop)
- {
- //默认为主窗口的位置
- offset = Mouse.GetPosition(App.Current.MainWindow);
- HwndControl = hwnd;
- TargetPop = targetPop;
- InitData();
- }
- public void UpdatePointOffset(Point point)
- {
- offset = point;
- }
- public void ShowLayer(DependencyObject dependencyObject = null)
- {
- if (layerAdded)
- {
- RemoveFromLayer();
- return;
- }
- AddToLayer(dependencyObject);
- }
- public void RemoveLayer()
- {
- if (layerAdded)
- {
- RemoveFromLayer();
- return;
- }
- }
- //绑定鼠标点击整个窗口区域的事件
- private void InitData()
- {
- if (popLayer == null)
- {
- Window parentWnd = Window.GetWindow(HwndControl);
- if (parentWnd != null && parentWnd.Content is UIElement && TargetPop != null)
- {
- parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
- {
- if (layerAdded)
- {
- try
- {
- Point p = Mouse.GetPosition(TargetPop);
- double width = 0;
- double height = 0;
- if (double.IsNaN(TargetPop.Width) == false && double.IsNaN(TargetPop.Height) == false)
- {
- width = TargetPop.Width;
- height = TargetPop.Height;
- }
- else
- {
- width = TargetPop.ActualWidth;
- height = TargetPop.ActualHeight;
- }
- if (p.X < 0 || p.Y < 0 || p.X > width || p.Y > height)
- {
- RemoveFromLayer();
- }
- }
- catch (Exception ex)
- {
- }
- }
- }), true);
- }
- }
- }
- private void AddToLayer(DependencyObject dependencyObject = null)
- {
- Window parentWnd = Window.GetWindow(HwndControl);
- popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
- if (popLayer != null && TargetPop != null && !layerAdded)
- {
- if (layerPresent == null)
- {
- layerPresent = new AdornerPresenter(popLayer);
- }
- popLayer.Add(layerPresent);
- if (popCanvas == null)
- {
- popCanvas = new Canvas();
- popCanvas.Children.Add(TargetPop);
- layerPresent.VisualContent = popCanvas;
- }
- TargetPop.Visibility = Visibility.Visible;
- TargetPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
- TargetPop.SetValue(Canvas.TopProperty, offset.Y + HwndControl.ActualHeight + 2);
- if (offset.X + TargetPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
- {
- TargetPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - TargetPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
- }
- else
- {
- TargetPop.SetValue(Canvas.LeftProperty, offset.X);
- }
- layerAdded = true;
- }
- }
- private void RemoveFromLayer()
- {
- if (popLayer != null && layerPresent != null && layerAdded)
- {
- popLayer.Remove(layerPresent);
- layerAdded = false;
- }
- }
- }
- #endregion
- #region 自定义菜单
- //自定义菜单:事件绑定、内容等
- public class CustomPopMenu
- {
- public ContextMenu PopMenu { get; private set; }
- public List<MenuItem> MenuItems { get; private set; }
- private object CommandTarget { get; set; }
- public CustomPopMenu(ContextMenu popMenu,object commandTarget)
- {
- PopMenu = popMenu;
- CommandTarget = commandTarget;
- GetMenuItems();
- }
- private void GetMenuItems()
- {
- if (PopMenu == null || PopMenu.Items == null)
- return;
- MenuItems = new List<MenuItem>();
- foreach (var item in PopMenu.Items)
- {
- if (item as MenuItem != null)
- {
- MenuItems.Add(item as MenuItem);
- }
- }
- }
- //显示或隐藏所有菜单项
- public void AllMenuVisibility(bool isVisual)
- {
- if (MenuItems == null) return;
- foreach (var item in MenuItems)
- {
- item.Visibility = (isVisual ? Visibility.Visible : Visibility.Collapsed);
- }
- }
- private MenuItem ContainterOfIndex(int index)
- {
- if (MenuItems == null || MenuItems.Count <= index || index < 0)
- return null;
- MenuItem menuItem = MenuItems[index] as MenuItem;
- return menuItem;
- }
- private MenuItem ContainterOfTag(string tag)
- {
- if (MenuItems == null || string.IsNullOrEmpty(tag))
- return null;
- var menuItem = MenuItems.FirstOrDefault(temp => temp.Tag.ToString() == tag);
- return menuItem;
- }
- public void SetVisibilityProperty(int index, bool isVisual)
- {
- var menuItem = ContainterOfIndex(index);
- if (menuItem != null)
- {
- menuItem.Visibility = (isVisual?Visibility.Visible:Visibility.Collapsed);
- }
- }
- public void SetVisibilityProperty(string tag, bool isVisual)
- {
- var menuItem = ContainterOfTag(tag);
- if (menuItem != null)
- {
- menuItem.Visibility = (isVisual ? Visibility.Visible : Visibility.Collapsed);
- }
- }
- public void SetTagProperty(int index, string tag)
- {
- var menuItem = ContainterOfIndex(index);
- if (menuItem != null)
- {
- menuItem.Tag = tag;
- }
- }
- public void SetHeaderProperty(int index, string header)
- {
- var menuItem = ContainterOfIndex(index);
- if (menuItem != null)
- {
- menuItem.Header = header;
- }
- }
- //按索引号设置菜单
- public void SetMenuBinding(int index, ICommand command,object comTarget = null)
- {
- var menuItem = ContainterOfIndex(index);
- if (menuItem != null)
- {
- if (comTarget == null)
- menuItem.CommandTarget = (UIElement)CommandTarget;
- else
- menuItem.CommandTarget = (UIElement)comTarget;
- menuItem.Command = command;
- }
- }
- //按Tag设置菜单
- public void SetMenuBinding(string tag, ICommand command, object comTarget = null)
- {
- var menuItem = ContainterOfTag(tag);
- if (menuItem != null)
- {
- if (comTarget == null)
- menuItem.CommandTarget = (UIElement)CommandTarget;
- else
- menuItem.CommandTarget = (UIElement)comTarget;
- menuItem.Command = command;
- }
- }
- //按索引号设置子菜单
- public void SetSubMenuBinding(int index, int subIndex, ICommand command, object comTarget = null)
- {
- var menuItem = ContainterOfIndex(index);
- if (menuItem != null && menuItem.Items != null && menuItem.Items.Count > 0 && subIndex >= 0)
- {
- if(menuItem.Items.Count > subIndex)
- {
- var item = menuItem.Items[subIndex];
- if(item is RadioButton)
- {
- var radioItem = item as RadioButton;
- radioItem.CommandParameter = radioItem.IsChecked;
- if (comTarget == null)
- radioItem.DataContext = (UIElement)CommandTarget;
- else
- radioItem.DataContext = (UIElement)comTarget;
- radioItem.Command = command;
- }
- else if(item is MenuItem)
- {
- var menuitem = item as MenuItem;
-
- if (comTarget == null)
- menuitem.CommandTarget = (UIElement)CommandTarget;
- else
- menuitem.CommandTarget = (UIElement)comTarget;
-
- menuItem.Command = command;
- }
- }
-
- }
- }
- public void SetSubMenuBinding(string tag, string subTag, ICommand command, object comTarget = null)
- {
- var menuItem = ContainterOfTag(tag);
- if (menuItem != null && menuItem.Items != null && menuItem.Items.Count > 0)
- {
- object theItem = null;
- foreach(var item in menuItem.Items)
- {
- if(item is RadioButton)
- {
- var radioItem = item as RadioButton;
- if(radioItem.Tag != null && radioItem.Tag.ToString() == subTag)
- {
- theItem = item;
- break;
- }
- }
- else if(item is MenuItem)
- {
- var menuitem = item as MenuItem;
- if (menuitem.Tag != null && menuitem.Tag.ToString() == subTag)
- {
- theItem = item;
- break;
- }
- }
- }
- if(theItem != null)
- {
- if (theItem is RadioButton)
- {
- var radioTheItem = theItem as RadioButton;
- radioTheItem.CommandParameter = radioTheItem.IsChecked;
- if (comTarget == null)
- radioTheItem.DataContext = (UIElement)CommandTarget;
- else
- radioTheItem.DataContext = (UIElement)comTarget;
- radioTheItem.Command = command;
- }
- else if(theItem is MenuItem)
- {
- var menuTheitem = theItem as MenuItem;
- if (comTarget == null)
- menuTheitem.CommandTarget = (UIElement)CommandTarget;
- else
- menuTheitem.CommandTarget = (UIElement)comTarget;
- menuTheitem.Command = command;
- }
- }
- }
- }
- }
- #endregion
- }
|