閱讀導航git
本文介紹使用FluentWPF控件庫實現亞克力效果的窗體。github
使用 .Net Core 3.1 建立名爲 「AcrylicWindow」 的WPF模板項目,添加三個Nuget庫:MaterialDesignThemes、MaterialDesignColors和FluentWPF,其中亞克力效果是由FluentWPF控件庫實現的。express
如下爲三個庫具體版本:c#
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="FluentWPF" version="0.4.0" targetFramework="net461" /> <package id="MaterialDesignColors" version="1.1.2" targetFramework="net461" /> <package id="MaterialDesignThemes" version="2.4.1.1101" targetFramework="net461" /> </packages>
解決方案主要文件目錄組織結構:微信
文件【App.xaml】,在StartupUri中設置啓動的視圖【MainWindow.xaml】,並在【Application.Resources】節點增長MaterialDesignThemes和FluentWPF控件庫的樣式文件:app
<Application x:Class="AcrylicWindow.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!-- FluentWPF Controls --> <ResourceDictionary Source="pack://application:,,,/FluentWPF;component/Styles/Controls.xaml" /> <!-- Material Design Controls--> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
文件【MainWindow.xaml】,引入MaterialDesignThemes和FluentWPF控件庫的命名空間,源碼以下:ide
<Window x:Class="AcrylicWindow.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:fw="clr-namespace:SourceChord.FluentWPF;assembly=FluentWPF" mc:Ignorable="d" fw:AcrylicWindow.Enabled="True" fw:AcrylicWindow.AcrylicWindowStyle="None" MouseDown="Window_MouseDown" FontSize="15" Height="600" Width="1080" WindowStyle="None" AllowsTransparency="True" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="350"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid fw:PointerTracker.Enabled="True" Background="#01FFFFFF"> <StackPanel> <StackPanel Orientation="Horizontal"> <Button Foreground="Black" Background="{x:Null}" BorderBrush="{x:Null}"> <materialDesign:PackIcon Kind="ArrowLeft"/> </Button> <TextBlock Text="Settings" FontSize="12" Margin="10" VerticalAlignment="Center"/> </StackPanel> <ListViewItem Padding="0"> <StackPanel Orientation="Horizontal"> <materialDesign:PackIcon Kind="Home" Margin="15"/> <TextBlock Text="Home" VerticalAlignment="Center"/> </StackPanel> </ListViewItem> <TextBlock Text="System" Margin="15" FontWeight="DemiBold"/> <ListView FontSize="15"> <ListViewItem Padding="1"> <StackPanel Orientation="Horizontal"> <materialDesign:PackIcon Kind="Monitor" Margin="15"/> <TextBlock Text="Video" VerticalAlignment="Center"/> </StackPanel> </ListViewItem> <ListViewItem Padding="1"> <StackPanel Orientation="Horizontal"> <materialDesign:PackIcon Kind="Headphones" Margin="15"/> <TextBlock Text="Audio" VerticalAlignment="Center"/> </StackPanel> </ListViewItem> <ListViewItem Padding="1"> <StackPanel Orientation="Horizontal"> <materialDesign:PackIcon Kind="MessageOutline" Margin="15"/> <TextBlock Text="Notifications" VerticalAlignment="Center"/> </StackPanel> </ListViewItem> <ListViewItem Padding="1"> <StackPanel Orientation="Horizontal"> <materialDesign:PackIcon Kind="BatteryOutline" Margin="15"/> <TextBlock Text="Battery" VerticalAlignment="Center"/> </StackPanel> </ListViewItem> </ListView> </StackPanel> </Grid> <Grid Grid.Column="1" Background="White"> </Grid> </Grid> </Window>
關鍵點說一下:佈局
後臺代碼【MainWindow.xaml.cs】實現鼠標左鍵拖動窗體功能:動畫
private void Window_MouseDown(object sender, MouseButtonEventArgs e) { DragMove(); }
文中代碼已經所有給出,可直接Copy代碼,按解決方案目錄組織代碼文件便可運行,另附原做者視頻及源碼【見3.本文參考】、站長成功編譯的Demo(點擊下載->AcrylicWindow)。
除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載。
轉載請註明本文地址:https://dotnet9.com/7372.html
歡迎掃描下方二維碼關注 Dotnet9 的微信公衆號,本站會及時推送最新技術文章
時間如流水,只能流去不流回!
點擊【閱讀原文】,本站還有更多技術類文章等着您哦,此刻順便爲我點個「再看」可好?