Explorar o código

悬浮窗口 - 添加悬浮窗口接口

chenrongqian@kdanmobile.com %!s(int64=2) %!d(string=hai) anos
pai
achega
869a4a4bee

+ 2 - 49
PDF Office/CustomControl/ColorDropBox.xaml.cs

@@ -1,4 +1,5 @@
-using System;
+using PDF_Office.Helper;
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Linq;
@@ -22,54 +23,6 @@ namespace PDF_Office.CustomControl
     /// </summary>
     public partial class ColorDropBox : UserControl, INotifyPropertyChanged
     {
-        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;
-                }
-            }
-        }
 
         private AdornerLayer popLayer;
         private AdornerPresenter layerPresent;

+ 41 - 122
PDF Office/CustomControl/CompositeControl/SlidContent.xaml.cs

@@ -1,4 +1,5 @@
-using System;
+using PDF_Office.Helper;
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Linq;
@@ -17,100 +18,45 @@ using System.Windows.Shapes;
 
 namespace PDF_Office.CustomControl.CompositeControl
 {
-    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;
-            }
-        }
-    }
     /// <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 colorPop;
-        public  EventHandler<double> SelectedValueChanged;
-        public event PropertyChangedEventHandler PropertyChanged; 
-        protected void OnPropertyChanged([CallerMemberName] string name = null)
-        {
-            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
-        }
-        private Color? _selectedColor;
-        public Color? SelectedColor
+        private SlidContentPop SlidOpacityPop;
+        public event RoutedEventHandler SelectedValueChanged;
+
+        public double Value
         {
-            get
-            {
-                return _selectedColor;
-            }
-            set
-            {
-                if (_selectedColor != value)
-                {
-                    _selectedColor = value;
-                    OnPropertyChanged("SelectedColor");
-                }
-            }
+            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));
+
+        #endregion
+
         public SlidContent()
         {
             InitializeComponent();
             Loaded += ColorDropBox_Loaded;
             Unloaded += ColorDropBox_Unloaded;
             MouseDown += ColorDropBox_MouseDown;
-            colorPop = new SlidContentPop();
-            colorPop.OpacityValueChangedHandler += colorPop_OpacityValueChangedHandler;
+            SlidOpacityPop = new SlidContentPop();
+            SlidOpacityPop.OpacityValueChangedHandler += colorPop_OpacityValueChangedHandler;
         }
 
         private void colorPop_OpacityValueChangedHandler(object sender, double e)
         {
-            SelectedValueChanged?.Invoke(this, e);
+            Value = (double)e;
+            SelectedValueChanged?.Invoke(this, null);
         }
 
         private void ColorDropBox_Unloaded(object sender, RoutedEventArgs e)
@@ -143,12 +89,11 @@ namespace PDF_Office.CustomControl.CompositeControl
             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 (popLayer != null && SlidOpacityPop != null && !layerAdded)
             {
                 if (layerPresent == null)
                 {
@@ -158,23 +103,21 @@ namespace PDF_Office.CustomControl.CompositeControl
                 if (popCanvas == null)
                 {
                     popCanvas = new Canvas();
-                    popCanvas.Children.Add(colorPop);
+                    popCanvas.Children.Add(SlidOpacityPop);
                     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);
+                SlidOpacityPop.Visibility = Visibility.Visible;
+                SlidOpacityPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
+                SlidOpacityPop.SetValue(Canvas.TopProperty, offset.Y + this.ActualHeight + 2);
 
-                if (offset.X + colorPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
+                if (offset.X + SlidOpacityPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
                 {
-                    colorPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - colorPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
+                    SlidOpacityPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - SlidOpacityPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
                 }
                 else
                 {
-                    colorPop.SetValue(Canvas.LeftProperty, offset.X);
+                    SlidOpacityPop.SetValue(Canvas.LeftProperty, offset.X);
                 }
                 layerAdded = true;
             }
@@ -193,7 +136,7 @@ namespace PDF_Office.CustomControl.CompositeControl
             if (popLayer == null)
             {
                 Window parentWnd = Window.GetWindow(this);
-                if (parentWnd != null && parentWnd.Content is UIElement && colorPop != null)
+                if (parentWnd != null && parentWnd.Content is UIElement && SlidOpacityPop != null)
                 {
                     parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
                     {
@@ -205,14 +148,23 @@ namespace PDF_Office.CustomControl.CompositeControl
                                 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);
+                                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)
@@ -226,27 +178,6 @@ namespace PDF_Office.CustomControl.CompositeControl
             }
         }
 
-        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;
-              //  SelectedValueChanged?.Invoke(this, SelectedColor);
-            }
-            RemoveFromLayer();
-        }
-
         public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
         {
             Window parentWnd = Window.GetWindow(this);
@@ -262,21 +193,9 @@ namespace PDF_Office.CustomControl.CompositeControl
             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 void  SetSliOpacity(double newopacity)
         {
-            colorPop.OpacitySlider.Value = newopacity;
+            SlidOpacityPop.OpacitySlider.Value = newopacity;
         }
 
     }

+ 1 - 1
PDF Office/CustomControl/CompositeControl/SlidContentPop.xaml

@@ -39,7 +39,7 @@
 
                 <Border Grid.Column="1" HorizontalAlignment="Right" Background="Transparent" BorderBrush="#E2E3E6" Width="80" Height="32" BorderThickness="0">
                     <Grid>
-                        <ComboBox Name="OpacityDropBox" BorderThickness="1" Background="Transparent" BorderBrush="#FFE2E3E6">
+                        <ComboBox Name="OpacityDropBox" SelectionChanged="OpacityDropBox_SelectionChanged" DropDownClosed="OpacityDropBox_DropDownClosed" DropDownOpened="OpacityDropBox_DropDownOpened" BorderThickness="1" Background="Transparent" BorderBrush="#FFE2E3E6">
                             <ComboBoxItem Content="25%" Height="32" VerticalContentAlignment="Center" Template="{StaticResource comboxitem}">
 
                             </ComboBoxItem>

+ 30 - 0
PDF Office/CustomControl/CompositeControl/SlidContentPop.xaml.cs

@@ -21,6 +21,7 @@ namespace PDF_Office.CustomControl.CompositeControl
     public partial class SlidContentPop : UserControl
     {
         public event EventHandler<double> OpacityValueChangedHandler;
+        public bool isOpenLayout { get; private set; }
         public SlidContentPop()
         {
             InitializeComponent();
@@ -30,5 +31,34 @@ namespace PDF_Office.CustomControl.CompositeControl
         {
             OpacityValueChangedHandler?.Invoke(this, e.NewValue);
         }
+
+        private void OpacityDropBox_DropDownClosed(object sender, EventArgs e)
+        {
+            isOpenLayout = false;
+        }
+
+        private void OpacityDropBox_DropDownOpened(object sender, EventArgs e)
+        {
+            isOpenLayout = true;
+        }
+
+        private void OpacityDropBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        {
+            switch(OpacityDropBox.SelectedIndex)
+            {
+                case 0:
+                    OpacitySlider.Value = 0.25;
+                    break;
+                case 1:
+                    OpacitySlider.Value = 0.50;
+                    break;
+                case 2:
+                    OpacitySlider.Value = 0.75;
+                    break;
+                case 3:
+                    OpacitySlider.Value = 1;
+                    break;
+            }
+        }
     }
 }

+ 215 - 0
PDF Office/Helper/PopControlHelper.cs

@@ -0,0 +1,215 @@
+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
+{
+    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;
+            }
+        }
+
+    }
+}

+ 1 - 0
PDF Office/PDF Office.csproj

@@ -324,6 +324,7 @@
     <Compile Include="Helper\HomePageEditHelper.cs" />
     <Compile Include="Helper\ObservableDictionary.cs" />
     <Compile Include="Helper\PasswordBoxHelper.cs" />
+    <Compile Include="Helper\PopControlHelper.cs" />
     <Compile Include="Helper\RichTextBoxHelper.cs" />
     <Compile Include="Helper\SDKLisenceHelper.cs" />
     <Compile Include="Helper\PictureConverter.cs" />