先看看效果圖
目前網上找到了2種實現方式,一種是 .NET Framework4.5及以后有自帶的 WindowChrome 效果,一種是 WindowsAPI dwmapi.dll ,但這兩種在win10下面都會失效。win10如何實現在下一篇講。
1.WindowChrome 效果設置較為簡單,代碼如下。WindowChrome更多用法可以查閱官方文檔:https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.shell.windowchrome?view=netframework-4.7.2
<Window x:Class="WpfApp1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="Window1" Height="300" Width="300" Background="Transparent" WindowStartupLocation="CenterScreen"> <WindowChrome.WindowChrome> <WindowChrome GlassFrameThickness="-1"/> </WindowChrome.WindowChrome> <Grid> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock FontSize="40" TextAlignment="Center"> Hello Wolrd </TextBlock> <TextBlock FontSize="12" TextAlignment="Center" Margin="0,30,0,0">使用 WindowChrome 實現模糊透明</TextBlock> </StackPanel> </Grid> </Window>
2.調用 dwmapi.dll API
頁面代碼:
<Window x:Class="WpfApp1.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="Window2" Height="300" Width="300" WindowStartupLocation="CenterScreen"> <Grid> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock FontSize="40" TextAlignment="Center"> Hello Wolrd </TextBlock> <TextBlock FontSize="12" TextAlignment="Center" Margin="0,30,0,0">使用 Windows 的 dwmapi 實現模糊透明</TextBlock> </StackPanel> </Grid> </Window>
后端代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WpfApp1 { /// <summary> /// Window2.xaml 的交互邏輯 /// </summary> public partial class Window2 : Window { public Window2() { InitializeComponent(); } [StructLayout(LayoutKind.Sequential)] private struct MARGINS { public MARGINS(Thickness t) { Left = (int)t.Left; Right = (int)t.Right; Top = (int)t.Top; Bottom = (int)t.Bottom; } public int Left; public int Right; public int Top; public int Bottom; } [DllImport("dwmapi.dll", PreserveSig = false)] private static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset); [DllImport("dwmapi.dll", PreserveSig = false)] private static extern bool DwmIsCompositionEnabled(); /// <summary> /// win7 /// </summary> /// <param name="window"></param> /// <param name="margin"></param> /// <returns></returns> public static bool ExtendGlassFrame(Window window) { if (!DwmIsCompositionEnabled()) return false; IntPtr hwnd = new WindowInteropHelper(window).Handle; if (hwnd == IntPtr.Zero) throw new InvalidOperationException("The Window must be shown before extending glass."); // 將WPF和Win32透視圖的背景設置為透明 window.Background = Brushes.Transparent; HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent; MARGINS margins = new MARGINS(new Thickness(-1)); DwmExtendFrameIntoClientArea(hwnd, ref margins); return true; } protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); ExtendGlassFrame(this); } } }
這兩種都能實現透明模糊效果(win7下),但都各自有各自的特點,WindowChrome 的窗口內容都需要自行設置,細心看兩個窗口就會發現WindowChrome 的窗口標題都沒了,標題高度,邊距,調整窗口大小的作用范圍等,什么都可以自定義,自我感覺比較難調(可能只是因為我菜),比較適合做自定義窗口,標題欄自定義,標題欄上面還可以加其他按鈕,可以隱藏系統標題欄上面的按鈕,還有個特點是程序啟動時立即生效,dwmapi.dll 會有一個加載的延遲,使用 dwmapi.dll 則窗口內容標題等都是保留原始窗口的內容。