Windows 7 扩展玻璃效果(Aero Glass)
???? Windows 7 操作系統(tǒng)默認(rèn)具有一款玻璃效果主題(Aero Glass)。如果選擇了該款主題,所有的應(yīng)用程序標(biāo)題欄都會(huì)處于玻璃透明效果(如下圖)。這個(gè)功能是由Desktop Window Manager(DWM)服務(wù)支持的。
???? 默認(rèn)情況下,我們編寫(xiě)的應(yīng)用程序在Windows 7 中也只有標(biāo)題欄和窗口框架會(huì)具備玻璃效果,其他區(qū)域仍是不透明狀態(tài)(如下圖)。如果想將程序整體都改為上圖IE 窗口的效果,可以使用DWM API 將玻璃區(qū)域進(jìn)行擴(kuò)展。
首先,從dwmapi.dll 中調(diào)取DwmExtendFrameIntoClientArea 方法。
[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);創(chuàng)建方法ExtendAeroGlass 方法,可將WPF Window窗口的Aero Glass 區(qū)域擴(kuò)展。
private void ExtendAeroGlass(Window window) { try { // 為WPF程序獲取窗口句柄 IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle; HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr); mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent; // 設(shè)置Margins MARGINS margins = new MARGINS(); // 擴(kuò)展Aero Glass margins.cxLeftWidth = -1; margins.cxRightWidth = -1; margins.cyTopHeight = -1; margins.cyBottomHeight = -1; int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); if (hr < 0) { MessageBox.Show("DwmExtendFrameIntoClientArea Failed"); } } catch (DllNotFoundException) { Application.Current.MainWindow.Background = Brushes.White; } }簡(jiǎn)單制作一個(gè)WPF 界面。
<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>補(bǔ)充CheckBox 點(diǎn)擊事件,在其中啟用ExtendAeroGlass 方法。
private void CheckBox_Checked(object sender, RoutedEventArgs e) { if checkBox.IsChecked.Value) { this.Background = Brushes.Transparent; ExtendAeroGlass(this); } else { this.Background = Brushes.White; } }演示效果
運(yùn)行程序后,默認(rèn)界面狀態(tài)。
點(diǎn)擊"Extend AeroGlass" 選框,界面中<Grid> 也將呈現(xiàn)玻璃效果。
Windows API
???? 通過(guò)Windows API Code Pack 可以對(duì)Aero Glass 效果進(jìn)行開(kāi)啟或關(guān)閉。在程序中加入Microsoft.WindowsAPICodePack.Shell 命名空間,調(diào)整AeroGlassCompositioinEnabled 完成開(kāi)/關(guān)Aero Glass的效果。
GlassWindow.AeroGlassCompositionEnabled = checkBox.IsChecked.Value;源代碼
WpfAeroGlass.zip
轉(zhuǎn)載于:https://blog.51cto.com/186067/1280594
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Windows 7 扩展玻璃效果(Aero Glass)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CCNA找工作指南
- 下一篇: Linq To Entity 的增删改查