Windows 7 has a Glass effect topic (Aero Glass) by default ). If this topic is selected, the title bar of all applications will be in the transparent glass effect (for example ). This function is supported by the Desktop Window Manager (DWM) service.
By default, only the title bar and window frame of the compiled application in Windows 7 will have the glass effect, and other areas will still be opaque (for example ). If you want to change the overall effect of the program to IE window, you can use dwm api to expand the glass area.
First, retrieve the dwmapi. dll method from dwmapi.
[StructLayout(LayoutKind.Sequential)]public struct MARGINS{ public int cxLeftWidth; public int cxRightWidth; public int cyTopHeight; public int cyBottomHeight; };[DllImport("DwmApi.dll")]public static extern int DwmExtendFrameIntoClientArea( IntPtr hwnd, ref MARGINS pMarInset);
The ExtendAeroGlass method is used to expand the Aero Glass area of the WPF Window.
Private void ExtendAeroGlass (Window window) {try {// obtain the Window handle IntPtr mainWindowPtr = new window winterophelper (window) for the WPF program ). handle; HwndSource mainWindowSrc = HwndSource. fromHwnd (mainWindowPtr); mainWindowSrc. compositionTarget. backgroundColor = Colors. transparent; // set Margins MARGINS margins = new MARGINS (); // extend Aero Glass margins. cxLeftWidth =-1; margins. cxRightWidth =-1; margins. cyTopHeight =-1; margins. cyBottomHeight =-1; int hr = dwmexico tendframeworkclientarea (mainWindowSrc. handle, ref margins); if (hr <0) {MessageBox. show ("dwmexico tendframeworkclientarea Failed") ;}} catch (DllNotFoundException) {Application. current. mainWindow. background = Brushes. white ;}}
Create a simple WPF interface.
<Window x:Class="WpfAeroGlass.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid x:Name="layout"> <Button x:Name="btn" Content="Button" Margin="191,66,202,211" /> <CheckBox x:Name="checkBox" Content="Extend AeroGlass" Click="CheckBox_Checked" Height="24" Width="121" /> </Grid></Window>
Add the CheckBox Click Event and enable the ExtendAeroGlass method.
private void CheckBox_Checked(object sender, RoutedEventArgs e){ if (checkBox.IsChecked.Value) { this.Background = Brushes.Transparent; ExtendAeroGlass(this); } else { this.Background = Brushes.White; }}Demo Effect
After running the program, the default Interface status is displayed.
Click the "Extend AeroGlass" check box. The <Grid> pane displays the glass effect.
Windows API
You can use the Windows API Code Pack to enable or disable the Aero Glass effect. Add the Microsoft. WindowsAPICodePack. Shell namespace to the program and adjust AeroGlassCompositioinEnabled to enable/disable Aero Glass.
GlassWindow.AeroGlassCompositionEnabled = checkBox.IsChecked.Value;
Source code
WpfAeroGlass.zip