123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- 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.CompositeControl
- {
- /// <summary>
- /// ThicknessContent.xaml 的交互逻辑
- /// </summary>
- public partial class SlidContent : UserControl
- {
- #region 定义
- private AdornerLayer popLayer;
- private AdornerPresenter layerPresent;
- private bool layerAdded = false;
- private Canvas popCanvas;
- private SlidContentPop SlidOpacityPop;
- public event RoutedEventHandler SelectedValueChanged;
- public double Value
- {
- get { return (double)GetValue(_valueProperty); }
- set { SetValue(_valueProperty, value); }
- }
- public static readonly DependencyProperty _valueProperty =
- DependencyProperty.Register("Value", typeof(double), typeof(SlidContent), new PropertyMetadata(0.0));
- public double InitValue
- {
- get { return (double)GetValue(_initValueProperty); }
- set { SetValue(_initValueProperty, value); }
- }
- public static readonly DependencyProperty _initValueProperty =
- DependencyProperty.Register("InitValue", typeof(double), typeof(SlidContent), new PropertyMetadata(-1.0, InitValuePropertyChanged));
- private static void InitValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var control = d as SlidContent;
- var newdouble = (double)e.NewValue;
- if (control != null && newdouble >= 0.0)
- {
- control.SlidOpacityPop.SetOpacity(newdouble);
- }
- }
- #endregion
- public SlidContent()
- {
- InitializeComponent();
- Loaded += ColorDropBox_Loaded;
- Unloaded += ColorDropBox_Unloaded;
- MouseDown += ColorDropBox_MouseDown;
- SlidOpacityPop = new SlidContentPop();
- SlidOpacityPop.OpacityValueChangedHandler += colorPop_OpacityValueChangedHandler;
- }
- private void colorPop_OpacityValueChangedHandler(object sender, double e)
- {
- Value = (double)e;
- SelectedValueChanged?.Invoke(this, null);
- }
- private void ColorDropBox_Unloaded(object sender, RoutedEventArgs e)
- {
- if (layerAdded)
- {
- RemoveFromLayer();
- return;
- }
- }
- private void ColorDropBox_MouseDown(object sender, MouseButtonEventArgs e)
- {
- //e.Handled = true;
- //if (layerAdded)
- //{
- // RemoveFromLayer();
- // return;
- //}
- //AddToLayer();
- }
- 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 && SlidOpacityPop != null && !layerAdded)
- {
- if (layerPresent == null)
- {
- layerPresent = new AdornerPresenter(popLayer);
- }
- popLayer.Add(layerPresent);
- if (popCanvas == null)
- {
- popCanvas = new Canvas();
- popCanvas.Children.Add(SlidOpacityPop);
- layerPresent.VisualContent = popCanvas;
- }
- Point offset = GetOffsetToWindow(dependencyObject);
- SlidOpacityPop.Visibility = Visibility.Visible;
- SlidOpacityPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
- SlidOpacityPop.SetValue(Canvas.TopProperty, offset.Y + this.ActualHeight + 2);
- if (offset.X + SlidOpacityPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
- {
- SlidOpacityPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - SlidOpacityPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
- }
- else
- {
- SlidOpacityPop.SetValue(Canvas.LeftProperty, offset.X);
- }
- layerAdded = true;
- BtnOpeContenu.IsChecked = true;
- }
- }
- private void RemoveFromLayer()
- {
- if (popLayer != null && layerPresent != null && layerAdded)
- {
- popLayer.Remove(layerPresent);
- layerAdded = false;
- BtnOpeContenu.IsChecked = false;
- }
- }
- private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
- {
- if (popLayer == null)
- {
- Window parentWnd = Window.GetWindow(this);
- if (parentWnd != null && parentWnd.Content is UIElement && SlidOpacityPop != 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(SlidOpacityPop), Canvas.GetTop(SlidOpacityPop));
- Point popRightBottom = new Point(SlidOpacityPop.ActualWidth, SlidOpacityPop.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;
- }
- //如果是有展开的UI,超过悬浮窗口,就判断点击的位置是否为展开的UI上;
- //例如下拉框展开的选项列表UI超出悬浮窗口
- if (SlidOpacityPop != null)
- {
- if (SlidOpacityPop.isOpenLayout)
- return;
- }
- RemoveFromLayer();
- }
- catch
- {
- }
- }
- }), true);
- }
- }
- }
- 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 SetSliOpacity(double newopacity)
- {
- SlidOpacityPop.OpacitySlider.Value = newopacity;
- }
- private void BtnOpeContenu_Click(object sender, RoutedEventArgs e)
- {
- if (layerAdded)
- {
- RemoveFromLayer();
- return;
- }
- AddToLayer();
- }
- }
- }
|