最近可能要开始学习Microsoft CRM了,加上自己有一个项目需要完成可能没有太多的时间花在3.0上了.
本来想翻译下,不过看看这篇文章基本上都是代码所以也就算了.例子最后还有一张截图,可以在原文处找到.
原文地址:http://www.xamlog.com/2006/07/26/bitmap-effects-in-wpf/
Similar to Flash, there are a number of programmatic bitmap effects available in Windows Presentation Foundation. This tutorial demonstrates how to apply these effects in WPF.
Overview
The primary bitmap effects supported in WPF include the following:
· Bevel
· Blur
· Drop Shadow
· Embossed
· Outer Glow
Bitmap effects are available as part of the System.Windows.Media.Effects namespace and are defined as a BitmapObject.
|
Name
|
Description
|
|
BevelBitmapEffect
|
Creates a bevel which raises the surface of the image according to a specified curve.
|
|
BlurBitmapEffect
|
Simulates looking at an object through an out-of-focus lens.
|
|
DropShadowBitmapEffect
|
Applies a shadow behind a visual object at a slight offset. The offset is determined by mimicking a casting shadow from an imaginary light source.
|
|
EmbossBitmapEffect
|
Creates a bump mapping of the visual object to give the impression of depth and texture from an artificial light source.
|
|
OuterGlowBitmapEffect
|
Creates a halo of color around objects or areas of color.
|
Element names and descriptions taken from Microsoft's WinFX SDK
These effects can be applied in the code-behind or using straight XAML in WPF . To apply a drop-shadow to a button using C#, we first create a button in XAML:
<Button x:Name="myButton" Width="200" Height="40" Content="Button" />
Then, to apply the drop-shadow, we create a new bitmap effect in C# and apply it to our button, in this case "myButton":
private void Window1_Loaded(object sender, EventArgs e)
{
DropShadowBitmapEffect myDropShadowEffect = new DropShadowBitmapEffect();
myDropShadowEffect.Color = (Color)ColorConverter.ConvertFromString("#000000");
myDropShadowEffect.Softness = .4;
myDropShadowEffect.ShadowDepth = 7;
myButton.BitmapEffect = myDropShadowEffect;
}
However, it is also possible to do this entirely in XAML. The following XAML acheives the same effect:
<Button x:Name="myButton" Width="200" Height="40" Content="Button">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="#000000" Softness=".4" ShadowDepth="7" />
</Button.BitmapEffect>
</Button>
Moreover, it is also possible to combine effects using the BitmapEffectGroup tag:
<Button.BitmapEffect>
<BitmapEffectGroup>
<OuterGlowBitmapEffect GlowColor="DarkGreen" GlowSize="5"/>
<DropShadowBitmapEffect ShadowDepth="5" Softness=".4"/>
<BevelBitmapEffect BevelWidth="6"/>
</BitmapEffectGroup>
</Button.BitmapEffect>
A Working Example
To better illustrate what's possible using bitmap effects in WPF, let's create a group of buttons, each with its own bitmap effect. To make the example more interactive, we will "bind" a value of each effect to a slider.
<Window.Resources>
<!-- global button style-->
<Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="Tahoma" />
<Setter Property="FontSize" Value="24" />
<Setter Property="Height" Value="65" />
<Setter Property="Margin" Value="20,20,20,0" />
</Style>
<!-- global slider style-->
<Style x:Key="{x:Type Slider}" TargetType="{x:Type Slider}">
<Setter Property="Margin" Value="20,10,20,20" />
<Setter Property="AutoToolTipPlacement" Value="TopLeft"/>
</Style>
</Window.Resources>
<Window.Background>
<!-- background gradient -->
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStop Color="#C4CBD8" Offset="0" />
<GradientStop Color="#E6EAF5" Offset="0.5" />
<GradientStop Color="#CFD7E2" Offset="0.9" />
<GradientStop Color="#C4CBD8" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Window.Background>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Margin="20">
<Button Content="Drop Shadow">
<Button.BitmapEffect>
<DropShadowBitmapEffect
ShadowDepth="{Binding Path=Value,ElementName=sliderShadow}"
/>
</Button.BitmapEffect>
</Button>
<Slider Minimum="0" Maximum="10" x:Name="sliderShadow" Value="5"/>
<Button Content="Bevel">
<Button.BitmapEffect>
<BevelBitmapEffect
BevelWidth="{Binding Path=Value,ElementName=sliderBevel}"
/>
</Button.BitmapEffect>
</Button>
<Slider Minimum="0" Maximum="20" x:Name="sliderBevel" Value="14"/>
<Button Content="Blur">
<Button.BitmapEffect>
<BlurBitmapEffect
Radius="{Binding Path=Value,ElementName=sliderBlur}"
/>
</Button.BitmapEffect>
</Button>
<Slider Minimum="0" Maximum="10" x:Name="sliderBlur" Value="3"/>
</StackPanel>
<StackPanel Grid.Column="1" Margin="20">
<Button Content="Emboss">
<Button.BitmapEffect>
<EmbossBitmapEffect
Relief="{Binding ElementName=sliderEmboss, Path=Value}"
/>
</Button.BitmapEffect>
</Button>
<Slider Minimum="0" Maximum="1" x:Name="sliderEmboss" Value=".85" />
<Button Content="Outer Glow">
<Button.BitmapEffect>
<OuterGlowBitmapEffect
GlowColor="DarkGreen"
GlowSize="{Binding ElementName=sliderGlow,Path=Value}"
/>
</Button.BitmapEffect>
</Button>
<Slider Minimum="0" Maximum="20" x:Name="sliderGlow" Value="15"/>
<Button Content="Combined">
<Button.BitmapEffect>
<BitmapEffectGroup>
<OuterGlowBitmapEffect GlowColor="DarkGreen" GlowSize="5"/>
<DropShadowBitmapEffect ShadowDepth="5" Softness=".4"/>
<BevelBitmapEffect BevelWidth="6"/>
</BitmapEffectGroup>
</Button.BitmapEffect>
</Button>
</StackPanel>
</Grid>