using PDF_Office.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_Office.CustomControl.CompositeControl { /// /// ThicknessContent.xaml 的交互逻辑 /// 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 (Exception ex) { } } }), 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(); } } }