又是玻璃效果?调用一句代码就OK了

时间:2023-03-10 05:18:17
又是玻璃效果?调用一句代码就OK了

原文 http://www.cnblogs.com/lan-mei/archive/2012/05/11/2495740.html

最近自学WPF,网上一查资料,全是依赖属性,路由事件,动画效果等等.....都不大那么易懂,还有各种小效果,像窗体的玻璃效果就很多,给的代码也是各种各样,一般都是依据一个窗体写的,别人想用的话要先看那些有用,在移植等等,为什么不把他写成一个类呢?最好大家调用一下类,点个方法就OK,毕竟很多时候开发想把写的那些都弄懂是不可能,一是时间不允许,二是能力有限....三是领导要的是效果.....

  好了,直接上图了

又是玻璃效果?调用一句代码就OK了

当然,这个是白板,上面没有放东西.后台就重写了OnSourceInitialized方法,在加一句代码就OK了

1         protected override void OnSourceInitialized(EventArgs e)
2 {
3 base.OnSourceInitialized(e);
4 Glass.Load(this,new Thickness(-1));
5 }

其中的Glass就是主要的实现类了,你可以保存这个类,用的时候就重新一下OnSourceInitialized方法,在里面调用Load方法就好了,那个Thickness(-1)是什么意思呢?就是窗体的4个边框的距离了,-1就是整个窗体的意思,代码如下

又是玻璃效果?调用一句代码就OK了
 1     class Glass
2 {
3 public static bool Load(Window _win)
4 {
5 return ExtendGlassFrame(_win, new Thickness(-1));
6 }
7
8 public static bool Load(Window _win, Thickness _margin)
9 {
10 return ExtendGlassFrame(_win, _margin);
11 }
12
13 [StructLayout(LayoutKind.Sequential)]
14 struct MARGINS
15 {
16 public MARGINS(Thickness t)
17 {
18 Left = (int)t.Left;
19 Right = (int)t.Right;
20 Top = (int)t.Top;
21 Buttom = (int)t.Bottom;
22 }
23 public int Left, Right, Top, Buttom;
24 }
25
26 [DllImport("dwmapi.dll", PreserveSig = false)]
27 static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
28
29 [DllImport("dwmapi.dll", PreserveSig = false)]
30 static extern bool DwmIsCompositionEnabled();
31
32 static bool ExtendGlassFrame(Window window, Thickness margin)
33 {
34 if (!DwmIsCompositionEnabled())
35 {
36 return false;
37 }
38 IntPtr hwnd = new WindowInteropHelper(window).Handle;
39 if (hwnd == IntPtr.Zero)
40 throw new InvalidOperationException("无法使用玻璃效果");
41 window.Background = System.Windows.Media.Brushes.Transparent;
42 HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
43 MARGINS margins = new MARGINS(margin);
44 DwmExtendFrameIntoClientArea(hwnd, ref margins);
45 return true;
46 }
47 }
又是玻璃效果?调用一句代码就OK了

咦,有人可能会说,你不是做了重载了吗?Thickness(-1)都包含进去了呀,是为了方便整个窗体调用的时候,直接传个this就OK了。的确,我想大多数时候要的都是整个窗体的效果,只要4个边的是比较少的,先看看效果吧,我们把上面OnSourceInitialized里面的Thickness参数换为
Thickness(22.2,33.3,44.4,55.5),没错,他是doule的,支持小数,就算你写个99.9999我也是没有意见的-,-

又是玻璃效果?调用一句代码就OK了

可以看到,中间没有设置到的就没有效果了.好像单调了一点,最后加个颜色的小动画吧

又是玻璃效果?调用一句代码就OK了
 1         TextBlock textBlock = new TextBlock();
2 private void Window_Loaded(object sender, RoutedEventArgs e)
3 {
4 Grid grid = new Grid();
5 //定义颜色动画
6 ColorAnimation blackToWhite = new ColorAnimation(Colors.White, Colors.Black, new Duration(TimeSpan.FromSeconds(2)));
7 blackToWhite.AutoReverse = true;
8 blackToWhite.RepeatBehavior = RepeatBehavior.Forever;
9 //定义画笔,开始动画
10 SolidColorBrush scb = new SolidColorBrush(Colors.Black);
11 scb.BeginAnimation(SolidColorBrush.ColorProperty, blackToWhite);
12
13 textBlock.Text = DateTime.Now.ToString();
14 textBlock.FontSize = 30;
15 textBlock.TextEffects = new TextEffectCollection();
16
17 //定义文本效果
18 TextEffect tfe = new TextEffect();
19 tfe.Foreground = scb;
20 tfe.PositionStart = 0;
21 tfe.PositionCount = int.MaxValue;
22 textBlock.TextEffects.Add(tfe);
23 grid.Children.Add(textBlock);
24
25 //定义计时器
26 DispatcherTimer Mytimer = new DispatcherTimer();
27 Mytimer.Interval = TimeSpan.FromSeconds(1);
28 Mytimer.Tick+=new EventHandler(Mytimer_Tick);
29 Mytimer.Start();
30
31 this.AddChild(grid);
32 }
33
34 void Mytimer_Tick(object sender, EventArgs e)
35 {
36 textBlock.Text = DateTime.Now.ToString();
37 }
38
39 protected override void OnSourceInitialized(EventArgs e)
40 {
41 base.OnSourceInitialized(e);
42 Glass.Load(this);
43 }
又是玻璃效果?调用一句代码就OK了

直接用后台代码写的了,效果就直接运行了看吧,记住要加一些命名空间哦,还有引用那个Glass,当然你放在同级目录下就没有必要了