WPF 可拖拽的玻璃风 Popup

先上效果图 ( )

Ⅰ XAML部分 GameVisual.xaml


    
        
            
                
                
            
            
            
            
                
                
            

            
            

            
        
    

Ⅱ C#部分 GameVisual.xaml.cs

    public partial class GameVisual : Popup
    {
        private Point _dragStartPopupPosition;
        private Point _dragStartMousePosition;
        private bool _isDragging;

        private void TopBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var border = (Border)sender;
            _dragStartPopupPosition = new Point(HorizontalOffset, VerticalOffset);
            _dragStartMousePosition = e.GetPosition(Application.Current.MainWindow); // 使用主窗口作为参考
            _isDragging = true;
            border.CaptureMouse();
            e.Handled = true;
        }
        private void TopBorder_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDragging)
            {
                // 获取相对于主窗口的当前鼠标位置
                Point currentMousePosition = e.GetPosition(Application.Current.MainWindow);

                // 计算总偏移量(基于初始位置)
                double totalOffsetX = currentMousePosition.X - _dragStartMousePosition.X;
                double totalOffsetY = currentMousePosition.Y - _dragStartMousePosition.Y;

                // 直接设置新位置(而不是累加)
                HorizontalOffset = _dragStartPopupPosition.X + totalOffsetX;
                VerticalOffset = _dragStartPopupPosition.Y + totalOffsetY;

                e.Handled = true;
            }
        }
        private void TopBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_isDragging)
            {
                _isDragging = false;
                ((Border)sender).ReleaseMouseCapture();
                e.Handled = true;
            }
        }
    }

 Ⅲ 样式实现 ( 明暗主题切换 )

这部分功能使用了MinimalisticWPF库,可用NuGet安装,不过样式并不是核心部分,所以仅作为补充。

    [Theme(nameof(Background), typeof(Light), ["#99FFFFFF"])]
    [Theme(nameof(Background), typeof(Dark), ["#AA1e1e1e"])]
    [Theme(nameof(Foreground), typeof(Light), ["Black"])]
    [Theme(nameof(Foreground), typeof(Dark), ["White"])]
    public partial class GameVisual : Popup
    {
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(GameVisual), new PropertyMetadata(new CornerRadius(10)));

        public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }
        public static readonly DependencyProperty BackgroundProperty =
            DependencyProperty.Register("Background", typeof(Brush), typeof(GameVisual), new PropertyMetadata(Brushes.White));

        public Brush Foreground
        {
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }
        }
        public static readonly DependencyProperty ForegroundProperty =
            DependencyProperty.Register("Foreground", typeof(Brush), typeof(GameVisual), new PropertyMetadata(Brushes.Black));
    }

你可能感兴趣的:(wpf)