Apply the Area effect to the entire form in Vista
Recently, when looking for other resources, I accidentally found a program that can apply the Area effect to the entire form. I felt quite interesting. I checked the code and it was quite simple.
First, two APIs are required: DwmIsCompositionEnabled and dwmextendframeworkclientarea.
DwmIsCompositionEnabled is used to determine whether the system has enabled the Area effect. If it is set to False, the Area effect is disabled. In either case, the system does not support the Area effect, or the effect is disabled manually. If it is manually disabled, you can still make your program Area effect. Otherwise, it is not possible.
Dwmextendframeworkclientarea is used to make your form Area effect. It can be an entire form or a part of the form. The first parameter of this function is to apply the Handle of the Area effect form. The second parameter is a MARGINS structure that specifies where the Area effect is applied to the form, if it is all, you can set it to-1. If you cancel the Area, you can set it to 0.
Let's take a look at the declarations of these two APIs.
1 [DllImport ("dwmapi. dll", PreserveSig = false)]
2 staticexternvoid dwmextendframeworkclientarea (IntPtr hwnd, ref MARGINS margins );
3
4 [DllImport ("dwmapi. dll", PreserveSig = false)]
5 staticexternbool DwmIsCompositionEnabled ();
The following is the implementation code.
Set Area effect
1 publicstaticbool Enable (Window window Window)
2 {
3 try
4 {
5 if (! DwmIsCompositionEnabled ())
6 returnfalse;
7
8 IntPtr hwnd = new WindowInteropHelper (window). Handle;
9 if (hwnd = IntPtr. Zero)
10 thrownew InvalidOperationException ("The Window must be shown before extending glass .");
11
12 // Set the background to transparent from both the WPF and Win32 perspectives
13 window. Background = System. Windows. Media. Brushes. Transparent;
14 HwndSource. FromHwnd (hwnd). CompositionTarget. BackgroundColor = Colors. Transparent;
15
16 MARGINS margins = new MARGINS (-1 );
17 dwmextendframeworkclientarea (hwnd, ref margins );
18 returntrue;
19}
20 catch (Exception ex)
21 {
22 // cocould not change glass, but continue
23 System. Diagnostics. Debug. WriteLine (ex. Message );
24 returnfalse;
25}
26}
Cancel Area effect
1 publicstaticbool Disable (Window window Window)
2 {
3 try
4 {
5 if (! DwmIsCompositionEnabled ())
6 returnfalse;
7
8 IntPtr hwnd = new WindowInteropHelper (window). Handle;
9 if (hwnd = IntPtr. Zero)
10 thrownew InvalidOperationException ("The Window must be shown before extending glass .");
11
12 // Set the background to transparent from both the WPF and Win32 perspectives
13 window. Background = new SolidColorBrush (System. Windows. SystemColors. WindowColor );
14 HwndSource. FromHwnd (hwnd). CompositionTarget. BackgroundColor = System. Windows. SystemColors. WindowColor;
15
16 MARGINS margins = new MARGINS (0 );
17 dwmextendframeworkclientarea (hwnd, ref margins );
18 returntrue;
19}
20 catch (Exception ex)
21 {
22 // cocould not change glass, but continue
23 System. Diagnostics. Debug. WriteLine (ex. Message );
24 returnfalse;
25}
26}
Author: "Li Yuan's drama"