日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF 使用FontAwesome字体图标

發(fā)布時間:2023/12/4 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF 使用FontAwesome字体图标 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

要搞點小軟件,又不想使用圖標和圖標類庫,突然想起FontAwesome,試了一下,還挺方便的,先弄了幾個最常用的圖標試一下,弄了幾個按鈕的樣式,看一下效果:

看一下fontAwesome使用方法:

首先從官網(wǎng)下載字體,地址:http://www.fontawesome.com.cn/

下載后,把其中的 fontawesome-webfont.ttf?復制到軟件目錄,

添加一個幾個資源字典:

Base.xaml :?

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:sys="clr-namespace:System;assembly=mscorlib"><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="Colors.xaml"/></ResourceDictionary.MergedDictionaries><sys:Double x:Key="FontSizeMini" >12</sys:Double><sys:Double x:Key="FontSizeNormal" >14</sys:Double><sys:Double x:Key="FontSizeLarge" >16</sys:Double><FontFamily x:Key="FontAwesome">pack://application;,,,/Fonts/#FontAwesome</FontFamily><!--FontAwesome Icons Strings--><sys:String x:Key="SettingIcon">&#xf013;</sys:String><sys:String x:Key="MinsizeIcon">&#xf2d1;</sys:String><sys:String?x:Key="MaxsizeIcon">&#xf2d0;</sys:String><sys:String?x:Key="CloseIcon">&#xf00d;</sys:String><Style x:Key="BaseStyle" TargetType="{x:Type Control}"><Setter Property="FontFamily" Value="微軟雅黑"/><Setter Property="FontSize" Value="{StaticResource FontSizeNormal}"/><Setter Property="BorderThickness" Value="0"/> </Style><Style x:Key="IconButtonBaseStyle" TargetType="Button"><Setter Property="FontFamily" Value="{StaticResource FontAwesome}"/><Setter Property="BorderThickness" Value="0"/> </Style> </ResourceDictionary>

Colors.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Color x:Key="Black">#000000</Color><SolidColorBrush x:Key="BlackBrush" Color="{StaticResource Black}"/><Color x:Key="White">#FFFFFF</Color><SolidColorBrush x:Key="WhiteBrush" Color="{StaticResource White}"/><Color x:Key="TransWhite">#33dddddd</Color><SolidColorBrush x:Key="TransWhiteBrush" Color="{StaticResource TransWhite}"/><Color x:Key="Gray">#B8BABC</Color><SolidColorBrush x:Key="GrayBrush" Color="{StaticResource Gray}"/><Color x:Key="VeryDarkGray">#C0C0C0</Color><SolidColorBrush x:Key="VeryDarkGrayBrush" Color="{StaticResource VeryDarkGray}"/><Color x:Key="DarkGray">#C3C0C3</Color><SolidColorBrush x:Key="DarkGrayBrush" Color="{StaticResource DarkGray}"/><Color x:Key="LightGray">#ECECEC</Color><SolidColorBrush x:Key="LightGrayBrush" Color="{StaticResource LightGray}"/><Color x:Key="VeryLightGray">#F3F3F3</Color><SolidColorBrush x:Key="VeryLightGrayBrush" Color="{StaticResource VeryLightGray}"/><Color x:Key="ButtonBack">#07BDFD</Color><SolidColorBrush x:Key="ButtonBackBrush" Color="{StaticResource ButtonBack}"/><Color x:Key="ButtonHoverBack">#1FC7FD</Color><SolidColorBrush x:Key="ButtonHoverBackBrush" Color="{StaticResource ButtonHoverBack}"/> </ResourceDictionary>

ButtonStyles.xaml (重點)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:GQ"><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="Base.xaml"/></ResourceDictionary.MergedDictionaries><Style TargetType="{x:Type Button}" x:Key="IgnoreHover"><Setter Property="Background" Value="Transparent"/><Setter Property="BorderThickness" Value="0"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Button}"><Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}"><ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/></Border></ControlTemplate></Setter.Value></Setter> </Style><Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}"><Setter Property="Background" Value="{StaticResource ButtonBackBrush}"/><Setter Property="Foreground" Value="{StaticResource WhiteBrush}"/><Setter Property="FocusVisualStyle" Value="{x:Null}"/><Setter Property="Padding" Value="20 10"/><Setter Property="Margin" Value="10"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ButtonBase}"><Border x:Name="border"CornerRadius="6"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"Background="{TemplateBinding Background}" SnapsToDevicePixels="True"><Grid><TextBlock Text="{TemplateBinding Content}" Focusable="False" FontFamily="{TemplateBinding FontFamily}"FontSize="{TemplateBinding FontSize}"HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/></Grid></Border><ControlTemplate.Triggers><EventTrigger RoutedEvent="MouseEnter"><BeginStoryboard><Storyboard><ColorAnimation To="{StaticResource ButtonHoverBack}" Duration="0:0:0.3" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color"/></Storyboard></BeginStoryboard></EventTrigger><EventTrigger RoutedEvent="MouseLeave"><BeginStoryboard><Storyboard><ColorAnimation From="{StaticResource ButtonHoverBack}" Duration="0:0:0.3" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color"/></Storyboard></BeginStoryboard></EventTrigger><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" TargetName="border" Value="{StaticResource VeryDarkGrayBrush}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter> </Style><Style x:Key="IconButton" TargetType="Button" BasedOn="{StaticResource IconButtonBaseStyle}"><Setter Property="Background" Value="Transparent"/><Setter Property="Foreground" Value="{StaticResource WhiteBrush}"/><Setter Property="Margin" Value="0"/><Setter Property="Height" Value="32"/><Setter Property="Padding" Value="5"/><Setter Property="Width" Value="{Binding ActualHeight,RelativeSource={RelativeSource Self}}"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ButtonBase}"><Border x:Name="border"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"Background="{TemplateBinding Background}" SnapsToDevicePixels="True"><Viewbox><TextBlock Text="{TemplateBinding Content}" Focusable="False" FontFamily="{TemplateBinding FontFamily}"HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" FontSize="{TemplateBinding FontSize}"SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/></Viewbox></Border><ControlTemplate.Triggers><EventTrigger RoutedEvent="MouseEnter"><BeginStoryboard><Storyboard><ColorAnimation To="{StaticResource TransWhite}" Duration="0:0:0.2" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color"/></Storyboard></BeginStoryboard></EventTrigger><EventTrigger RoutedEvent="MouseLeave"><BeginStoryboard><Storyboard><ColorAnimation From="{StaticResource TransWhite}" Duration="0:0:0.2" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color"/></Storyboard></BeginStoryboard></EventTrigger><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" TargetName="border" Value="{StaticResource VeryDarkGrayBrush}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter> </Style><Style x:Key="IconGrowButton" TargetType="{x:Type Button}" BasedOn="{StaticResource IconButtonBaseStyle}"><Setter Property="Background" Value="Transparent"/><Setter Property="Foreground" Value="{StaticResource WhiteBrush}"/><Setter Property="Margin" Value="0"/><Setter Property="Height" Value="32"/><Setter Property="Padding" Value="5"/><Setter Property="Width" Value="{Binding ActualHeight,RelativeSource={RelativeSource Self}}"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ButtonBase}"><Border x:Name="border"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"Background="{TemplateBinding Background}" SnapsToDevicePixels="True"RenderTransformOrigin="0.5,0.5"><Border.RenderTransform><ScaleTransform/></Border.RenderTransform><Viewbox><TextBlock Text="{TemplateBinding Content}" Focusable="False" FontFamily="{TemplateBinding FontFamily}"HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" FontSize="{TemplateBinding FontSize}"SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/></Viewbox></Border><ControlTemplate.Triggers><EventTrigger RoutedEvent="MouseEnter"><BeginStoryboard><Storyboard><DoubleAnimation To="1.3" Duration="0:0:0.2" Storyboard.TargetName="border" Storyboard.TargetProperty="(RenderTransform).(ScaleTransform.ScaleX)"/><DoubleAnimation To="1.3" Duration="0:0:0.2" Storyboard.TargetName="border" Storyboard.TargetProperty="(RenderTransform).(ScaleTransform.ScaleY)"/></Storyboard></BeginStoryboard></EventTrigger><EventTrigger RoutedEvent="MouseLeave"><BeginStoryboard><Storyboard><DoubleAnimation To="1" Duration="0:0:0.2" Storyboard.TargetName="border" Storyboard.TargetProperty="(RenderTransform).(ScaleTransform.ScaleX)"/><DoubleAnimation To="1" Duration="0:0:0.2" Storyboard.TargetName="border" Storyboard.TargetProperty="(RenderTransform).(ScaleTransform.ScaleY)"/></Storyboard></BeginStoryboard></EventTrigger><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" TargetName="border" Value="{StaticResource VeryDarkGrayBrush}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter> </Style><Style x:Key="TextButton" TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}"><Setter Property="Background" Value="Transparent"/><Setter Property="Foreground" Value="{StaticResource WhiteBrush}"/><Setter Property="BorderThickness" Value="0"/><Setter Property="Padding" Value="20 10"/><Setter Property="Margin" Value="0 10"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ButtonBase}"><Border x:Name="border"CornerRadius="6"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"Background="{TemplateBinding Background}" SnapsToDevicePixels="True"><TextBlock Text="{TemplateBinding Content}" Focusable="False" FontFamily="{TemplateBinding FontFamily}"FontSize="{TemplateBinding FontSize}"HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/></Border><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Foreground" Value="{StaticResource DarkGrayBrush}"/></Trigger><Trigger Property="IsEnabled" Value="False"><Setter Property="Foreground" Value="{StaticResource VeryDarkGrayBrush}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter> </Style>????<!--上述5個樣式 參考自 https://www.youtube.com/playlist?list=PLrW43fNmjaQVYF4zgsD0oL9Iv6u23PI6M有改動,不完全一樣,你可以根據(jù)需要自行修改--> </ResourceDictionary>

然后在App.xaml內(nèi)添加所有資源字典:

<Application x:Class="GQ.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:GQ"StartupUri="/Windows/LoginWindow.xaml"xmlns:sys="clr-namespace:System;assembly=mscorlib"xmlns:arr="clr-namespace:System.Collections.Generic;assembly=mscorlib"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/Styles/Base.xaml"/><ResourceDictionary Source="/Styles/Panel3DStyle.xaml"/><ResourceDictionary Source="/Styles/ButtonStyles.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources> </Application>

最后,創(chuàng)建一個LoginWindow.xaml,測試這幾個樣式,并使用FontAwesome:

<Window x:Class="GQ.LoginWindow"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:GQ"mc:Ignorable="d"AllowsTransparency="True"WindowStyle="None"Background="Gray"WindowStartupLocation="CenterScreen"Title="MainWindow" Height="470" Width="497"><Grid><StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"><StackPanel Orientation="Horizontal" HorizontalAlignment="Center"><Button FontFamily="{StaticResource FontAwesome}" Content="{StaticResource SettingIcon}"/><Button FontFamily="{StaticResource FontAwesome}" Content="{StaticResource MinsizeIcon}"/><Button FontFamily="{StaticResource FontAwesome}" Content="{StaticResource MaxsizeIcon}"/><Button FontFamily="{StaticResource FontAwesome}" Content="{StaticResource CloseIcon}"/></StackPanel><StackPanel Orientation="Horizontal" HorizontalAlignment="Center"><Button Style="{StaticResource IconGrowButton}" Content="{StaticResource SettingIcon}"/><Button Style="{StaticResource IconGrowButton}" Content="{StaticResource MinsizeIcon}"/><Button Style="{StaticResource IconGrowButton}" Content="{StaticResource MaxsizeIcon}"/><Button Style="{StaticResource IconGrowButton}" Content="{StaticResource CloseIcon}"/></StackPanel><StackPanel Orientation="Horizontal" HorizontalAlignment="Center"><Button Style="{StaticResource IconButton}" Content="{StaticResource SettingIcon}"/><Button Style="{StaticResource IconButton}" Content="{StaticResource MinsizeIcon}"/><Button Style="{StaticResource IconButton}" Content="{StaticResource MaxsizeIcon}"/><Button Style="{StaticResource IconButton}" Content="{StaticResource CloseIcon}"/></StackPanel><StackPanel Orientation="Horizontal" HorizontalAlignment="Center"><Button Style="{StaticResource TextButton}" FontFamily="{StaticResource FontAwesome}" Content="{StaticResource SettingIcon}"/><Button Style="{StaticResource TextButton}" FontFamily="{StaticResource FontAwesome}" Content="{StaticResource MinsizeIcon}"/><Button Style="{StaticResource TextButton}" FontFamily="{StaticResource FontAwesome}" Content="{StaticResource MaxsizeIcon}"/><Button Style="{StaticResource TextButton}" FontFamily="{StaticResource FontAwesome}" Content="{StaticResource CloseIcon}"/></StackPanel></StackPanel></Grid> </Window>

這樣控件就能使用完成了,雖然看起來很麻煩,但收益還是不小的,以后再添加別的圖標,就很方便了。

效果圖:

當然嘍,做正經(jīng)的軟件,圖標肯定是由設計給的,不是用這些網(wǎng)上的這些圖標,只能自己玩玩,嘿嘿

如果喜歡,點個贊唄~

總結(jié)

以上是生活随笔為你收集整理的WPF 使用FontAwesome字体图标的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。