博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
抛砖引玉 【镜像控件】 WPF实现毛玻璃控件不要太简单
阅读量:5164 次
发布时间:2019-06-13

本文共 5067 字,大约阅读时间需要 16 分钟。

原文:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Vblegend_2013/article/details/83447420

源码已封装成 MirrorGrid类 可以直接在XAML里添加

根据需要可以把Grid 改为  button border等控件

注意 Target必须为当前控件下层的控件对象

 

加个BlurEffect就是毛玻璃效果

 

using System.Windows;using System.Windows.Controls;using System.Windows.Media;namespace WpfApp2{    ///     /// 镜像格子    ///     public class MirrorGrid : Grid    {        ///         /// 目标对象        ///         public FrameworkElement Target        {            get { return (FrameworkElement)GetValue(TargetProperty); }            set { SetValue(TargetProperty, value); }        }        ///         /// 目标对象属性        ///         public static readonly DependencyProperty TargetProperty =        DependencyProperty.Register("Target", typeof(FrameworkElement), typeof(MirrorGrid),        new FrameworkPropertyMetadata(null));        ///         /// 重写基类 Margin        ///         public new Thickness Margin        {            get { return (Thickness)GetValue(MarginProperty); }            set { SetValue(MarginProperty, value); }        }        public new static readonly DependencyProperty MarginProperty = DependencyProperty.Register("Margin", typeof(Thickness), typeof(MirrorGrid), new FrameworkPropertyMetadata(new Thickness(0), new PropertyChangedCallback(OnMarginChanged)));        private static void OnMarginChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)        {            ((FrameworkElement)target).Margin = (Thickness)e.NewValue;            //强制渲染            ((FrameworkElement)target).InvalidateVisual();        }        protected override void OnRender(DrawingContext dc)        {            var Rect = new Rect(0, 0, RenderSize.Width, RenderSize.Height);            if (Target == null)            {                dc.DrawRectangle(this.Background, null, Rect);            }            else            {                this.DrawImage(dc, Rect);            }        }        private void DrawImage(DrawingContext dc, Rect rect)        {            VisualBrush brush = new VisualBrush(Target)            {                Stretch = Stretch.Fill,            };            var tl = this.GetElementLocation(Target);            var sl = this.GetElementLocation(this);            var lx = (sl.X - tl.X) / Target.ActualWidth;            var ly = (sl.Y - tl.Y) / Target.ActualHeight;            var pw = this.ActualWidth / Target.ActualWidth;            var ph = this.ActualHeight / Target.ActualHeight;            brush.Viewbox = new Rect(lx, ly, pw, ph);            dc.DrawRectangle(brush, null, rect);        }        ///         /// 获取控件元素在窗口的实际位置        ///         ///         /// 
public Point GetElementLocation(FrameworkElement Control) { Point location = new Point(0, 0); FrameworkElement element = Control; while (element != null) { var Offset = VisualTreeHelper.GetOffset(element); location = location + Offset; element = (FrameworkElement)VisualTreeHelper.GetParent(element); } return location; } }}

 

测试源码

 

后台

using System.Windows;using System.Windows.Input;namespace WpfApp2{    ///     /// Window1.xaml 的交互逻辑    ///     public partial class Window1 : Window    {        public Window1()        {            InitializeComponent();        }        private bool isDragging;        private Point clickPosition;        private void thumb_MouseDown(object sender, MouseButtonEventArgs e)        {            isDragging = true;            var draggableElement = sender as UIElement;            clickPosition = e.GetPosition(this);            draggableElement.CaptureMouse();        }        private void thumb_MouseUp(object sender, MouseButtonEventArgs e)        {            isDragging = false;            var draggableElement = sender as UIElement;            draggableElement.ReleaseMouseCapture();        }        private void thumb_MouseMove(object sender, MouseEventArgs e)        {            var draggableElement = sender as UIElement;            if (isDragging && draggableElement != null)            {                Point currentPosition = e.GetPosition(this.Parent as UIElement);                Thickness s = new Thickness(thumb.Margin.Left + currentPosition.X - clickPosition.X, thumb.Margin.Top + currentPosition.Y - clickPosition.Y,0,0);                thumb.Margin = s;                clickPosition = currentPosition;            }        }    }}

 

 

posted on
2018-11-11 13:40 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/p/9941900.html

你可能感兴趣的文章
使用flask-mail发送电子邮件时出现的问题
查看>>
Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
查看>>
【C++】三大概念要分清--重载,隐藏(重定义,覆盖(重写)
查看>>
Condition原理以及使用
查看>>
[转]JQuery实现图片轮播效果
查看>>
记一次nginx部署yii2项目时502 bad gateway错误的排查
查看>>
算法-插入排序(Insertion sorting)
查看>>
python字典
查看>>
常用window命令
查看>>
Python命名空间和作用域
查看>>
前端效果——持续更新。。。
查看>>
开发和常用工具推荐清单
查看>>
(排序)快速排序QuickSort
查看>>
[asp.net]登录协同工作平台安全解决方式
查看>>
看到一篇很有意思的文章:在中国,电商赚钱的原因是穷人太多。
查看>>
WP7.1 应用程序发布到Marketplace
查看>>
04-Spring的注解开发
查看>>
微信小程序之登录态维护(十一)
查看>>
Linux卸载MySql——ubuntu版
查看>>
【转自心声】华为眼中管理者的18种惰怠行为
查看>>