123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- using PDF_Master.Helper;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace PDF_Master.CustomControl
- {
- /// <summary>
- /// ColorDropBox.xaml 的交互逻辑
- /// </summary>
- public partial class ColorDropBox : UserControl, INotifyPropertyChanged
- {
- private AdornerLayer popLayer;
- private AdornerPresenter layerPresent;
- private bool layerAdded = false;
- private Canvas popCanvas;
- private ColorDropBoxPop colorPop;
- public event EventHandler<Color?> SelectedColorChanged;
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged([CallerMemberName] string name = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- private Color? _selectedColor;
- public Color? SelectedColor
- {
- get
- {
- return _selectedColor;
- }
- set
- {
- if (_selectedColor != value)
- {
- _selectedColor = value;
- OnPropertyChanged("SelectedColor");
- }
- }
- }
- public bool HasTransparentColor
- {
- get
- {
- if (colorPop != null)
- {
- return colorPop.TransparentPanel.Visibility == Visibility.Visible;
- }
- return false;
- }
- set
- {
- if (colorPop != null)
- {
- colorPop.SetTransparentPanelVisible(value);
- }
- }
- }
- public ColorDropBox()
- {
- InitializeComponent();
- Loaded += ColorDropBox_Loaded;
- Unloaded += ColorDropBox_Unloaded;
- MouseDown += ColorDropBox_MouseDown;
- colorPop = new ColorDropBoxPop();
- }
- private void ColorDropBox_Unloaded(object sender, RoutedEventArgs e)
- {
- if (layerAdded)
- {
- RemoveFromLayer();
- return;
- }
- }
- private void ColorDropBox_MouseDown(object sender, MouseButtonEventArgs e)
- {
- e.Handled = true;
- ShowLayer();
- }
- public void ShowLayer(DependencyObject dependencyObject = null)
- {
- if (layerAdded)
- {
- RemoveFromLayer();
- return;
- }
- AddToLayer(dependencyObject);
- }
- private void AddToLayer(DependencyObject dependencyObject = null)
- {
- Window parentWnd = Window.GetWindow(this);
- popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
- if (popLayer != null && colorPop != null &&!layerAdded)
- {
- if (layerPresent == null)
- {
- layerPresent = new AdornerPresenter(popLayer);
- }
- popLayer.Add(layerPresent);
- if (popCanvas == null)
- {
- popCanvas = new Canvas();
- popCanvas.Children.Add(colorPop);
- layerPresent.VisualContent = popCanvas;
- colorPop.ColorSelected += ColorPop_ColorSelected;
- }
- Point offset = GetOffsetToWindow(dependencyObject);
- colorPop.Visibility = Visibility.Visible;
- colorPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
- colorPop.SetValue(Canvas.TopProperty, offset.Y + this.ActualHeight + 2);
- if (offset.X + colorPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
- {
- colorPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - colorPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
- }
- else
- {
- colorPop.SetValue(Canvas.LeftProperty, offset.X);
- }
- layerAdded = true;
- }
- }
- private void RemoveFromLayer()
- {
- if (popLayer != null && layerPresent != null && layerAdded)
- {
- popLayer.Remove(layerPresent);
- layerAdded = false;
- }
- }
- private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
- {
- if (popLayer == null)
- {
- Window parentWnd = Window.GetWindow(this);
- if (parentWnd != null && parentWnd.Content is UIElement && colorPop != null)
- {
- parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
- {
- if (layerAdded)
- {
- try
- {
- Window checkWindow = Window.GetWindow(this);
- Point clickPoint = param.GetPosition(checkWindow);
- Point leftTop = TransformToVisual(checkWindow).Transform(new Point(0, 0));
- Point rightBottom = TransformToVisual(checkWindow).Transform(new Point(ActualWidth, ActualHeight));
- Point popLeftTop = new Point(Canvas.GetLeft(colorPop), Canvas.GetTop(colorPop));
- Point popRightBottom = new Point(colorPop.ActualWidth, colorPop.ActualHeight);
- Rect dropboxRect = new Rect(leftTop.X, leftTop.Y, rightBottom.X - leftTop.X, rightBottom.Y - leftTop.Y);
- Rect popboxRect = new Rect(popLeftTop.X, popLeftTop.Y, popRightBottom.X, popRightBottom.Y);
- if (dropboxRect.Contains(clickPoint) || popboxRect.Contains(clickPoint))
- {
- return;
- }
- RemoveFromLayer();
- }
- catch(Exception ex)
- {
- }
-
- }
- }), true);
- }
- }
- }
- private void ColorPop_ColorSelected(object sender, Color e)
- {
- if (SelectedColor != e)
- {
- // NormalColorRectControl.Visibility = Visibility.Collapsed;
- // TransparentRectControl.Visibility = Visibility.Collapsed;
- if (e != Colors.Transparent)
- {
- // NormalColorRectControl.Visibility = Visibility.Visible;
- // NormalColorRectControl.Fill = new SolidColorBrush(e);
- }
- else
- {
- // TransparentRectControl.Visibility = Visibility.Visible;
- }
- SelectedColor = e;
- SelectedColorChanged?.Invoke(this, SelectedColor);
- }
- RemoveFromLayer();
- }
- public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
- {
- Window parentWnd = Window.GetWindow(this);
- if (dependencyObject != null)
- {
- var frame = dependencyObject as FrameworkElement;
- if(frame != null)
- {
- return frame.TransformToAncestor(parentWnd).Transform(new Point(0, 0));
- }
- }
-
- return TransformToAncestor(parentWnd).Transform(new Point(0, 0));
- }
- public void SetThemeColors(List<Color> themeColorList)
- {
- if (colorPop != null)
- {
- colorPop.ThemeColors.Clear();
- foreach (Color themeColor in themeColorList)
- {
- colorPop.ThemeColors.Add(themeColor);
- }
- }
- }
- public List<Color> GetThemeColors()
- {
- if (colorPop != null)
- {
- return colorPop.ThemeColors.ToList();
- }
- return new List<Color>();
- }
- public List<Color> GetRecentlyColors()
- {
- if (colorPop != null)
- {
- return colorPop.RecentlyColors.ToList();
- }
- return new List<Color>();
- }
- public void SetSelectedColor(Color setColor)
- {
- if (SelectedColor != setColor)
- {
- // NormalColorRectControl.Visibility = Visibility.Collapsed;
- // TransparentRectControl.Visibility = Visibility.Collapsed;
- if (setColor != Colors.Transparent)
- {
- // NormalColorRectControl.Visibility = Visibility.Visible;
- // NormalColorRectControl.Fill = new SolidColorBrush(setColor);
- }
- else
- {
- // TransparentRectControl.Visibility = Visibility.Visible;
- }
- SelectedColor = setColor;
- if (colorPop != null)
- {
- colorPop.AddColorToRecent(setColor);
- }
- }
- }
- public void ClearSelectColor()
- {
- _selectedColor = null;
- // NormalColorRectControl.Visibility = Visibility.Collapsed;
- // TransparentRectControl.Visibility = Visibility.Collapsed;
- }
- }
- }
|