【WPF學習】第四十六章 效果

  WPF提供了可應用於任何元素的可視化效果。效果的目標是提供一種簡單的聲明式方法,從而改進文本、圖像、按鈕以及其餘控件的外觀。不是編寫本身的繪圖代碼,而是使用某個繼承自Effect的類(位於System.Windows.Media.Effects名稱空間中)以當即得到諸如模糊、輝光以及陰影等效果。編程

  下表列出了可供使用的的效果類:瀏覽器

表 效果類編輯器

   勿將上表列出的Effect類的派生類和位圖效果類相混淆,位圖效果派生類自BitmapEffect類,該類和Effect類位於相同的名稱空間中。儘管位圖效果具備相似的編程模型,但他們存在價格嚴重的侷限性:ide

  •   位圖效果不支持像素着色器,像素着色器是建立可重用效果的最強大、最靈活的方式。
  •   位圖效果是用非託管的代碼實現的,從而須要徹底信任的應用程序。因此,在基於瀏覽器的XBAP應用程序中不能使用位圖效果。
  •   位圖效果總使用軟件進行渲染,不使用顯卡資源。這使得它們的速度較慢,當處理大量元素或具備較大可視化表面的元素時尤爲如此。

  BitmapEffect類是在WPF的第一個版本中引入的,該版本沒有提供Effect類。爲了向後兼容,仍保留了位圖效果。工具

  接下里的幾節深刻分析效果模型,並演示上三個繼承自Effect的類:BlurEffect、DropShadowEffect以及ShaderEffect。性能

1、BlurEffect類動畫

  最簡單的WPF效果是BlurEffect類。該類模糊元素的內容,就想經過失焦透鏡觀察到得效果。經過增長Radiu屬性的值(默認值是5)可增長模糊程度。ui

  爲使用任何效果,須要建立適當的效果對象並設置相應元素的Effect屬性:spa

<Button Content="BlurEffect(Radius=2)" Margin="5" Padding="3">
            <Button.Effect>
                <BlurEffect Radius="2"></BlurEffect>
            </Button.Effect>
        </Button>

        <Button Content="Blurred (Radius=5)" Padding="5" Margin="3">
            <Button.Effect>
                <BlurEffect Radius="5"></BlurEffect>
            </Button.Effect>
        </Button>

        <Button Content="Blurred (Radius=20)" Padding="5" Margin="3">
            <Button.Effect>
                <BlurEffect Radius="20"></BlurEffect>
            </Button.Effect>
        </Button>

  下圖顯示了應用到一組按鈕的三個不一樣程度的模糊效果(Radiu屬性值分別爲二、5和20)。插件

2、DropShadowEffect類

  DropShadowEffect類在元素背後添加了輕微的偏移陰影。可以使用該類的幾個屬性,以下表所示:

表 DropShadowEffect類的屬性

   下面是實現這些陰影效果的標記:

<TextBlock FontSize="20" Margin="5">
            <TextBlock.Effect>
                <DropShadowEffect></DropShadowEffect>
            </TextBlock.Effect>
            <TextBlock.Text>Basic DropShawEffect</TextBlock.Text>
        </TextBlock>
        
        <TextBlock FontSize="20" Margin="5">
            <TextBlock.Effect>
                <DropShadowEffect Color="Blue"></DropShadowEffect>
            </TextBlock.Effect>
            <TextBlock.Text>Blue Color DropShawEffect</TextBlock.Text>
        </TextBlock>

        <TextBlock FontSize="20" Foreground="White" Margin="5">
            <TextBlock.Effect>
                <DropShadowEffect  BlurRadius="15"></DropShadowEffect>
            </TextBlock.Effect>
            <TextBlock.Text>Blurred Dropshadow with White text</TextBlock.Text>
        </TextBlock>

        <TextBlock FontSize="20" Foreground="Magenta" Margin="5">
            <TextBlock.Effect>
                <DropShadowEffect   ShadowDepth="0"></DropShadowEffect>
            </TextBlock.Effect>
            <TextBlock.Text>Close dropshadow</TextBlock.Text>
        </TextBlock>

        <TextBlock FontSize="20" Foreground="Magenta" Margin="5">
            <TextBlock.Effect>
                <DropShadowEffect   ShadowDepth="25"></DropShadowEffect>
            </TextBlock.Effect>
            <TextBlock.Text>Distant dropshadow</TextBlock.Text>
        </TextBlock>

  效果圖以下所示:

  沒有提供用來組合效果的類,這意味着一次只能爲一個元素應用一個效果。然而,有時可經過將元素添加到高層的容器中模擬多個效果(例如,爲TextBlock元素使用陰影效果,而後將其放入使用模糊效果的StackPanel面板中)。大多數狀況下,應避免這種變通方法,由於這種方法會成倍地增長渲染工做量並會下降性能。相反,應當查找可以完成全部內容的單個效果。

3、ShaderEffect類

  ShaderEffect類沒有提供就緒的效果。相反,它是一個抽象類,可繼承該類以建立本身的自定義像素着色器。經過使用ShaderEffect類(或從該類派生的自定義效果),可實現更多的效果,而不只侷限於模糊和陰影。

  可能與你所指望的相反,實現像素着色器的邏輯不是直接在效果類中使用C#代碼編寫的。相反,像素着色器使用高級着色語言(High Level Shader Lanaguage,HLSL)編寫的,該語言是Mircrosoft DirectX的一部分(使用這種語言的優勢是很明顯的——由於DirectX和HLSL已經存在許多年了,圖形開發人員已經建立了許多可在代碼中使用的像素着色器例程)。

  爲建立像素着色器,須要編寫和編譯HLSL代碼。要執行編譯,可以使用WIndows SDK for Windows 8中的fxc.exe命令工具;注意,Windows SDK for Windows 8也支持Windows 7,這從名稱中看不出來的。但更簡便的選項是使用免費的Shazzam工具。Shazzam提供了用於HLSL文件的編輯器,可以使用該工具在示例圖像上嘗試效果。該工具還提供了幾個像素着色器示例,可將它們做爲自定義效果的基礎。

  儘管製做本身的HLSL文件超出本章範圍,但下面將使用一個已有的HLSL文件。一旦將HLSL文件編譯成.ps文件,就能夠在項目中使用它了。只須要將文件添加到已有的WPF項目中,在Solution Explorer中選擇該文件,並將它的Build Action屬性設置爲Resource。最後必須建立一個繼承自ShaderEffect的自定義類並使用該資源。

  例如,若是正在使用自定義像素着色器(已經編譯到名爲Effect.ps的文件中),可以使用如下代碼:

public class CustomEffect:ShaderEffect
    {
        public CustomEffect()
        {
            Uri uri = new Uri("Effect.ps", UriKind.Relative);

            PixelShader = new PixelShader();
            PixelShader.UriSource = uri;
        }
    }

  如今能夠在任意窗口中使用這個自定義的像素着色器了。首先,經過以下所示的映射使名稱空間可用:

xmlns:local="clr-namespace:Drawing"

  如今建立自定義效果類的一個實例,並用它設置元素的Effect屬性:

<Image Name="img" Margin="5" Source="harpsichord.jpg">
            <Image.Effect>
                <local:CustomEffect></local:CustomEffect>
            </Image.Effect>
        </Image>

  該示例完整代碼以下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;

namespace Drawing
{
    public class CustomEffect:ShaderEffect
    {
        public CustomEffect()
        {
            Uri uri = new Uri("Effect.ps", UriKind.Relative);

            PixelShader = new PixelShader();
            PixelShader.UriSource = uri;
            UpdateShaderValue(InputProperty);
        }

        public static readonly DependencyProperty InputProperty =
            ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(CustomEffect), 0 /* assigned to sampler register S0 */);

        public Brush Input
        {
            get { return (Brush)GetValue(InputProperty); }
            set { SetValue(InputProperty, value); }
        }
    }
}
CustomEffect
<Window x:Class="Drawing.CustomPixelShader"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Drawing"
        Title="CustomPixelShader" Height="600" Width="305.639">
    <StackPanel>
        <Image Name="img" Margin="5" Source="harpsichord.jpg">
            <Image.Effect>
                <local:CustomEffect></local:CustomEffect>
            </Image.Effect>
        </Image>
        <CheckBox Name="chkEffect" Margin="5" Content="Effect enabled" IsChecked="True" Click="chkEffect_Click"></CheckBox>

    </StackPanel>
</Window>
CustomPixelShader
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Drawing
{
    /// <summary>
    /// CustomPixelShader.xaml 的交互邏輯
    /// </summary>
    public partial class CustomPixelShader : Window
    {
        public CustomPixelShader()
        {
            InitializeComponent();
        }

        private void chkEffect_Click(object sender, RoutedEventArgs e)
        {
            if (chkEffect.IsChecked != true)
                img.Effect = null;
            else
                img.Effect = new CustomEffect();
        }
    }
}
CustomPixelShader.xaml.cs

  最終效果圖以下所示:

                   

   若是使用採用特定輸入參數的像素着色器,須要作的工做筆上面的示例要更復雜一點。對與這種狀況,須要經過調用RegisterPixelShaderSamplerProperty()靜態方法建立相應的依賴性屬性。

  靈活的像素着色器就像在諸如Adobe Photoshop這樣的圖像軟件中使用的插件同樣強大。它能夠執行任何工做,從添加基本的陰影乃至更富有挑戰性的效果。如模糊、輝光、水波、浮雕和銳化等。當集合使用動畫實時改變着色器的參數時,像素着色器還能夠建立賞心悅目的效果。

相關文章
相關標籤/搜索