2012-04-25 143 views
1

有誰知道如何在WPF 中使用內部發光效果而不使用使用表達式混合或不贊成的BitmapEffects?WPF內部發光效果

樣品圖片:

enter image description here

例如,這裏是一些XAML與圖像和一些文本的按鈕。我想這個按鈕有一個內發光(不是外發光):(!注意這還沒完解決方案只是一個想法)

<Button Click="HandleDeleteRows" Style="{StaticResource ButtonCellStyle}"> 
    <DockPanel> 
     <Image Style="{StaticResource DeleteButtonImage}" /> 
     <TextBlock Style="{StaticResource DeleteButtonCaption}" /> 
    </DockPanel> 
</Button> 
+0

有可能是一種方法,我可以定義自己的影響類來做到這一點? – Alain 2012-04-25 16:38:21

回答

2

儘管上面的簡單示例由PompolutZ的回答解決了,但我並不想重寫控件的控件模板,我想在現實世界的示例中應用樣式 - 所以我開始定義自己的效果如下,說明here

第1步 - 寫一個HLSL .FX文件,將做你想要的效果。我放棄發光太複雜,因爲它需要邊緣檢測。我決定採用一系列相當容易實現的標準色彩,亮度,伽馬和飽和度調整,並讓我創建一些很好的視覺線索。他們使用常識很容易實現,並在線查找像素着色算法。

ColourAdjust.fx

sampler2D implicitInput : register(s0); 
float saturation : register(c0); 
float gamma : register(c1); 
float brightness : register(c2); 
float red_adjust : register(c3); 
float green_adjust : register(c4); 
float blue_adjust : register(c5); 

static const float max_gamma = 100; 

float4 main(float2 uv : TEXCOORD) : COLOR 
{ 
    float4 color = tex2D(implicitInput, uv); 
    float4 result; 

    // Apply greyscale desaturation 
    float gray = color.r * 0.3 + color.g * 0.59 + color.b *0.11; 
    result.r = (color.r - gray) * saturation + gray; 
    result.g = (color.g - gray) * saturation + gray; 
    result.b = (color.b - gray) * saturation + gray; 

    // Apply Gamma Adjustment (if it's not approximately 0.5 - which means no adjustment) 
    float gammafactor = gamma == 0 ? max_gamma : log(gamma)/log(0.5); 
    result.r = pow(result.r, gammafactor); 
    result.g = pow(result.g, gammafactor); 
    result.b = pow(result.b, gammafactor); 

    //Apply linear brightness adjustment 
    result.r += brightness + red_adjust; 
    result.g += brightness + green_adjust; 
    result.b += brightness + blue_adjust; 

    //Clamp brightness adjustment result to bounds 0 <= val <= 1 
    result.r = (result.r > 1 ? 1 : (result.r < 0 ? 0 : result.r)); 
    result.g = (result.g > 1 ? 1 : (result.g < 0 ? 0 : result.g)); 
    result.b = (result.b > 1 ? 1 : (result.b < 0 ? 0 : result.b)); 

    result.a = color.a; 
    return result; 
} 

步驟2 - 我不得不下載DirectX SDK的本地副本,這樣我可以編譯上面的HLSL代碼轉換成PS文件,該文件是什麼使用WPF - 讓我。

> > fxc.exe /T ps_2_0 /E PS /ColourAdjust.ps ColourAdjust.fx 

步驟3 - 寫ShaderEffect類,將經由DependencyProperties暴露效果參數。這裏是ColourAdjustEffect.cs

using System; 
using System.Reflection; 
using System.Windows; 
using System.Windows.Media; 
using System.Windows.Media.Effects; 

namespace WPF.Utilities.UI 
{ 
    public class ColourAdjustEffect : ShaderEffect 
    { 
     private static PixelShader _pixelShader = new PixelShader() { UriSource = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly() + ";component/Effects/ColourAdjust.ps") }; 
     public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(ColourAdjustEffect), 0); 
     public static readonly DependencyProperty SaturationProperty = DependencyProperty.Register("Saturation", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(1.0, PixelShaderConstantCallback(0), CoerceFactor)); 
     public static readonly DependencyProperty GammaProperty = DependencyProperty.Register("Gamma", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.5, PixelShaderConstantCallback(1), CoerceFactor)); 
     public static readonly DependencyProperty BrightnessAdjustmentProperty = DependencyProperty.Register("BrightnessAdjustment", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(2), CoerceBrightnessAdjustment)); 
     public static readonly DependencyProperty RedAdjustmentProperty = DependencyProperty.Register("RedAdjustment", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(3), CoerceBrightnessAdjustment)); 
     public static readonly DependencyProperty GreenAdjustmentProperty = DependencyProperty.Register("GreenAdjustment", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(4), CoerceBrightnessAdjustment)); 
     public static readonly DependencyProperty BlueAdjustmentProperty = DependencyProperty.Register("BlueAdjustment", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(5), CoerceBrightnessAdjustment)); 

     public ColourAdjustEffect() 
     { 
      PixelShader = _pixelShader; 

      UpdateShaderValue(InputProperty); 
      UpdateShaderValue(SaturationProperty); 
      UpdateShaderValue(GammaProperty); 
      UpdateShaderValue(BrightnessAdjustmentProperty); 
      UpdateShaderValue(RedAdjustmentProperty); 
      UpdateShaderValue(GreenAdjustmentProperty); 
      UpdateShaderValue(BlueAdjustmentProperty); 
     } 

     public Brush Input 
     { 
      get { return (Brush)GetValue(InputProperty); } 
      set { SetValue(InputProperty, value); } 
     } 

     /// <summary>A value between 0 and 1 to alter the amount of colour left in the image. 0 is entirely greyscale, and 1 is unaffected. Default is 1.</summary> 
     public double Saturation 
     { 
      get { return (double)GetValue(SaturationProperty); } 
      set { SetValue(SaturationProperty, value); } 
     } 

     /// <summary>A value between 0 and 1 to alter the lightness of the greyscale without altering true black or true white. 
     /// 0 shifts shades closer to true black, and 1 shifts shades closer to true white. Default is 0.5.</summary> 
     public double Gamma 
     { 
      get { return (double)GetValue(GammaProperty); } 
      set { SetValue(GammaProperty, value); } 
     } 

     /// <summary>A value between -1 and 1 to linearly move the end result closer to true black or true white respectively. 
     /// -1 will result in an entirely black image, +1 will result in an entirely white image. Default is 0.</summary> 
     public double BrightnessAdjustment 
     { 
      get { return (double)GetValue(BrightnessAdjustmentProperty); } 
      set { SetValue(BrightnessAdjustmentProperty, value); } 
     } 

     /// <summary>A value between -1 and 1 to linearly increase the Red component of the result. 
     /// -1 will remove all Red from the image, +1 will maximize all Red in the image. Default is 0.</summary> 
     public double RedAdjustment 
     { 
      get { return (double)GetValue(RedAdjustmentProperty); } 
      set { SetValue(RedAdjustmentProperty, value); } 
     } 
     /// <summary>A value between -1 and 1 to linearly increase the Green component of the result. 
     /// -1 will remove all Green from the image, +1 will maximize all Green in the image. Default is 0.</summary> 
     public double GreenAdjustment 
     { 
      get { return (double)GetValue(GreenAdjustmentProperty); } 
      set { SetValue(GreenAdjustmentProperty, value); } 
     } 
     /// <summary>A value between -1 and 1 to linearly increase the Blue component of the result. 
     /// -1 will remove all Blue from the image, +1 will maximize all Blue in the image. Default is 0.</summary> 
     public double BlueAdjustment 
     { 
      get { return (double)GetValue(BlueAdjustmentProperty); } 
      set { SetValue(BlueAdjustmentProperty, value); } 
     } 

     private static object CoerceFactor(DependencyObject d, object value) 
     { 
      double newFactor = (double)value; 

      if(newFactor < 0.0) return 0.0; 
      if(newFactor > 1.0) return 1.0; 
      return newFactor; 
     } 

     private static object CoerceBrightnessAdjustment(DependencyObject d, object value) 
     { 
      double newFactor = (double)value; 

      if(newFactor < -1.0) return -1.0; 
      if(newFactor > 1.0) return 1.0; 
      return newFactor; 
     } 
    } 
} 

步驟4:用你的影響在XAML:

<Setter Property="Effect"> 
    <Setter.Value> 
     <ui:ColourAdjustEffect Saturation="0" Gamma="0.6" 
           BrightnessAdjustment="-0.2" RedAdjustment="0.04" /> 
    </Setter.Value> 
</Setter> 

因此,雖然我沒有得到我的光暈效果,我有足夠的參數與我玩可以得到一個'突出'的視覺線索,這是我真正的目標。這裏的一些事情,我能夠用它做:

enter image description here

1

也許你可以嘗試這樣的事:

<Style x:Key="{x:Type Button}" TargetType="{x:Type Button}"> 
    <Style.Setters> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Grid> 
         <Border CornerRadius="10" BorderThickness="20"> 
          <Border.BorderBrush> 
           <LinearGradientBrush StartPoint="0, 0.5" EndPoint="1, 0.5"> 
            <GradientStop Color="LightGreen" Offset="0.0" /> 
            <GradientStop Color="Transparent" Offset="0.15" /> 
           </LinearGradientBrush> 
          </Border.BorderBrush> 
         </Border> 
         <Border CornerRadius="10" BorderThickness="20"> 
          <Border.BorderBrush> 
           <LinearGradientBrush StartPoint="0, 0.5" EndPoint="1, 0.5"> 
            <GradientStop Color="LightGreen" Offset="1.0" /> 
            <GradientStop Color="Transparent" Offset="0.85" /> 
           </LinearGradientBrush> 
          </Border.BorderBrush> 
         </Border> 
         <Border CornerRadius="10" BorderThickness="20"> 
          <Border.BorderBrush> 
           <LinearGradientBrush StartPoint="0.5, 0" EndPoint="0.5, 1"> 
            <GradientStop Color="LightGreen" Offset="0.0" /> 
            <GradientStop Color="Transparent" Offset="0.15" /> 
           </LinearGradientBrush> 
          </Border.BorderBrush> 
         </Border> 
         <Border CornerRadius="10" BorderThickness="20"> 
          <Border.BorderBrush> 
           <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5, 1"> 
            <GradientStop Color="LightGreen" Offset="1.0" /> 
            <GradientStop Color="Transparent" Offset="0.85" /> 
           </LinearGradientBrush> 
          </Border.BorderBrush> 
         </Border> 
         <Border BorderBrush="White" BorderThickness="2" CornerRadius="5" Margin="18"></Border> 
         <ContentPresenter HorizontalAlignment="Center" 
              VerticalAlignment="Center"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style.Setters> 
</Style> 

我們可以通過在網格中的某些內容周圍玩邊框來產生「發光」效果。我覺得它不會像InnerGlow BitmapEffect那麼靈活,但無論如何它已經過時。

+0

努力點的人,這是一個好主意。爲每個控制提出一個像這樣的新控制模板是一件很痛苦的事,但仍然是一個很好的解決方法。 – Alain 2012-05-03 12:22:53

+0

請注意,像這樣的漸變,你會得到一些醜陋的中間灰色的顏色。更好的辦法是使用'#0000ff00'而不是'Transparent' – ghord 2016-07-31 11:14:49