What is the glass effect? You can call a code.

Source: Internet
Author: User

Recently I learned self-developed WPF. I checked the information on the net, all of which are dependency attributes, routing events, animation effects, and so on ..... they are not very easy to understand, and there are also various small effects, such as the glass effects of forms, the Code is also a variety of, generally written based on a form, if someone else wants to use it, they should first look at the useful ones and transplant them. Why not write it into a class? It is best for everyone to call the class and click a method. After all, it is impossible for developers to understand what they write. First, time is not allowed, and second, the capability is limited .... third, what leaders need is results .....

Okay.

Of course, this is a whiteboard, and there is no content on it. The OnSourceInitialized method is rewritten in the background, and the code will be OK after a sentence is added.

View Code

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

Here, Glass is the main implementation class. You can save this class and re-use the OnSourceInitialized method when using it. Just call the Load method in it. The Thickness (-1) what does that mean? It is the distance between the four borders of the form, and-1 is the meaning of the whole form. The Code is as follows:

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 MARGINS15 {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 dwmexico tendframeworkclientarea (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 ("glass effect unavailable"); 41 window. background = System. windows. media. brushes. transparent; 42 HwndSource. fromHwnd (hwnd ). compositionTarget. backgroundColor = Colors. transparent; 43 MARGINS margins = new MARGINS (margin); 44 dwmexico tendframeworkclientarea (hwnd, ref margins); 45 return true; 46} 47}

Some people may say, aren't you doing heavy load? Thickness (-1) is included, so that it is OK to directly pass this when the entire form is called. Indeed, I think most of the time is about the effect of the entire form. If there are few four sides, let's look at the effect first. Let's Replace the Thickness parameter in OnSourceInitialized
Thickness (22.2, 33.3, 44.4, 55.5). That's right. He is a doule and supports decimal places. Even if you write 99.9999, I have no opinion -,-

We can see that there is no effect in the middle. It seems monotonous. Add a small color animation.

View Code

1 TextBlock textBlock = new TextBlock (); 2 private void Window_Loaded (object sender, RoutedEventArgs e) 3 {4 Grid grid = new Grid (); 5 // define color Animation 6 ColorAnimation blackToWhite = new ColorAnimation (Colors. white, Colors. black, new Duration (TimeSpan. fromSeconds (2); 7 blackToWhite. autoReverse = true; 8 blackToWhite. repeatBehavior = RepeatBehavior. forever; 9 // define the paint brush and start the animation 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 // defines the text effect 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 // defines the timer 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}

I wrote the code directly in the background, and the results will be directly run. Remember to add some namespaces and reference the Glass. Of course, it is unnecessary to put it in the same level directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.