又是玻璃效果?調用一句代碼就OK了


  最近自學WPF,網上一查資料,全是依賴屬性,路由事件,動畫效果等等.....都不大那么易懂,還有各種小效果,像窗體的玻璃效果就很多,給的代碼也是各種各樣,一般都是依據一個窗體寫的,別人想用的話要先看那些有用,在移植等等,為什么不把他寫成一個類呢?最好大家調用一下類,點個方法就OK,畢竟很多時候開發想把寫的那些都弄懂是不可能,一是時間不允許,二是能力有限....三是領導要的是效果.....

  好了,直接上圖了

當然,這個是白板,上面沒有放東西.后台就重寫了OnSourceInitialized方法,在加一句代碼就OK了

View Code
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就是整個窗體的意思,代碼如下

View Code
 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     }

咦,有人可能會說,你不是做了重載了嗎?Thickness(-1)都包含進去了呀,是為了方便整個窗體調用的時候,直接傳個this就OK了。的確,我想大多數時候要的都是整個窗體的效果,只要4個邊的是比較少的,先看看效果吧,我們把上面OnSourceInitialized里面的Thickness參數換為
Thickness(22.2,33.3,44.4,55.5),沒錯,他是doule的,支持小數,就算你寫個99.9999我也是沒有意見的-,-

可以看到,中間沒有設置到的就沒有效果了.好像單調了一點,最后加個顏色的小動畫吧

View Code
 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         }

直接用后台代碼寫的了,效果就直接運行了看吧,記住要加一些命名空間哦,還有引用那個Glass,當然你放在同級目錄下就沒有必要了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM