WPF中的模糊、阴影效果
-
BlurEffect类
<Window x:Class="项目10.MainWindow" 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:sys="clr-namespace:System;assembly=mscorlib" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:local="clr-namespace:项目10" mc:Ignorable="d" Title="MainWindow" Width="500" Height="350" WindowStartupLocation="CenterScreen" MouseLeftButtonDown="Window_MouseLeftButtonDown" MouseRightButtonDown="Window_MouseRightButtonDown"> <StackPanel> <Button Padding="5" Content="Blurred(Radius=0)"> <Button.Effect> <BlurEffect Radius="0"></BlurEffect> </Button.Effect> </Button> <Button Padding="5" Content="Blurred(Radius=2)"> <Button.Effect> <BlurEffect Radius="2"></BlurEffect> </Button.Effect> </Button> <Button Padding="5" Content="Blurred(Radius=5)"> <Button.Effect> <BlurEffect Radius="5"></BlurEffect> </Button.Effect> </Button> </StackPanel> </Window>
-
DropShadowEffect类
Color
设置背景的颜色BlurRadius
设置模糊的半径ShadowDepth
设置偏移的距离Opacity
设置透明度Direction
设置阴影的方向
BlurRadius 获取或设置一个值,该值指示阴影的模糊效果的半径。 CanFreeze 获取一个值,该值指示是否可将对象变为不可修改。(Inherited from Freezable) Color 获取或设置投影的颜色。 DependencyObjectType 获取对此实例的 CLR 类型进行包装的 DependencyObjectType。(Inherited from DependencyObject) Direction 获取或设置投影的方向。 Dispatcher 获取与此 Dispatcher 关联的 DispatcherObject。(Inherited from DispatcherObject) EffectMapping 当在派生类中重写时,通过效果转换鼠标输入和坐标系。(Inherited from Effect) HasAnimatedProperties 获取一个值,该值指示一个或多个 AnimationClock 对象是否与此对象的任何依赖项属性相关联。(Inherited from Animatable) IsFrozen 获取一个值,该值指示对象当前是否可修改。(Inherited from Freezable) IsSealed 获取一个值,该值指示此实例当前是否为密封的(只读)。(Inherited from DependencyObject) Opacity 获取或设置投影的不透明度。 RenderingBias 获取或设置一个值,该值指示系统在呈现投影时是注重速度还是注重质量。 ShadowDepth 获取或设置投影距纹理下方的距离。
<Window x:Class="项目10.MainWindow"
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:sys="clr-namespace:System;assembly=mscorlib"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:项目10"
mc:Ignorable="d"
Title="MainWindow" Width="500" Height="350" WindowStartupLocation="CenterScreen">
<StackPanel Margin="25">
<TextBlock FontSize="20" Margin="5">
<TextBlock.Effect>
<DropShadowEffect></DropShadowEffect>
</TextBlock.Effect>
Basic dropshadow
</TextBlock>
<TextBlock FontSize="20" Margin="5">
<TextBlock.Effect>
<DropShadowEffect Color="Blue"></DropShadowEffect>
</TextBlock.Effect>
Blue dropshadow
</TextBlock>
<TextBlock FontSize="20" Margin="5">
<TextBlock.Effect>
<DropShadowEffect Color="Blue" BlurRadius="3"></DropShadowEffect>
</TextBlock.Effect>
BlurRadius=3 dropshadow
</TextBlock>
<TextBlock FontSize="20" Margin="5">
<TextBlock.Effect>
<DropShadowEffect Color="Blue" BlurRadius="3" ShadowDepth="9"></DropShadowEffect>
</TextBlock.Effect>
ShadowDepth="6" dropshadow
</TextBlock>
<TextBlock FontSize="20" Margin="5">
<TextBlock.Effect>
<DropShadowEffect Color="Blue" BlurRadius="3" ShadowDepth="6"
Opacity="0.3"></DropShadowEffect>
</TextBlock.Effect>
Opacity="0.3" dropshadow
</TextBlock>
<TextBlock FontSize="20" Margin="5">
<TextBlock.Effect>
<DropShadowEffect Color="Blue" BlurRadius="3" ShadowDepth="6"
Opacity="0.3" Direction="45"></DropShadowEffect>
</TextBlock.Effect>
Direction="45" dropshadow
</TextBlock>
</StackPanel>
</Window>
- ShaderEffect类
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Reflection;
namespace 项目10 {
public class ThresholdEffect : ShaderEffect {
private static PixelShader _pixelShader =
new PixelShader() { UriSource = MakePackUri("ThresholdEffect.fx.ps") };
public ThresholdEffect()
{
PixelShader = _pixelShader;
UpdateShaderValue(InputProperty);
UpdateShaderValue(ThresholdProperty);
UpdateShaderValue(BlankColorProperty);
}
// MakePackUri is a utility method for computing a pack uri
// for the given resource.
public static Uri MakePackUri(string relativeFile)
{
Assembly a = typeof(ThresholdEffect).Assembly;
// Extract the short name.
string assemblyShortName = a.ToString().Split(',')[0];
string uriString = "pack://application:,,,/" +
assemblyShortName +
";component/" +
relativeFile;
return new Uri(uriString);
}
///////////////////////////////////////////////////////////////////////
#region Input dependency property
public Brush Input {
get { return (Brush)GetValue(InputProperty); }
set { SetValue(InputProperty, value); }
}
public static readonly DependencyProperty InputProperty =
ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(ThresholdEffect), 0);
#endregion
///////////////////////////////////////////////////////////////////////
#region Threshold dependency property
public double Threshold {
get { return (double)GetValue(ThresholdProperty); }
set { SetValue(ThresholdProperty, value); }
}
public static readonly DependencyProperty ThresholdProperty =
DependencyProperty.Register("Threshold", typeof(double), typeof(ThresholdEffect),
new UIPropertyMetadata(0.5, PixelShaderConstantCallback(0)));
#endregion
///////////////////////////////////////////////////////////////////////
#region BlankColor dependency property
public Color BlankColor {
get { return (Color)GetValue(BlankColorProperty); }
set { SetValue(BlankColorProperty, value); }
}
public static readonly DependencyProperty BlankColorProperty =
DependencyProperty.Register("BlankColor", typeof(Color), typeof(ThresholdEffect),
new UIPropertyMetadata(Colors.Transparent, PixelShaderConstantCallback(1)));
#endregion
}
}
<Window.Resources>
<local:ThresholdEffect x:Key="thresholdEffect" Threshold="0.25" BlankColor="Orange" />
</Window.Resources>
<Grid Effect="{StaticResource thresholdEffect}">
</Grid>