次の方法で共有


方法 : ドロップ シャドウ視覚効果を作成する

更新 : 2007 年 11 月

DropShadowBitmapEffect を使用すると、表示オブジェクトの背後に影のような効果を作成できます。次の内容について一連の例を示します。

  • 1 つのオブジェクトに効果を適用するための簡単なマークアップの使用方法

  • 1 つまたは複数のオブジェクトに効果を適用するための Style の使用方法

  • オブジェクトへの効果を適用するためのコードの使用方法

  • オブジェクトに適用された効果のプロパティをアニメーション表示する方法

メモ : 次の例では、いずれもオブジェクトに対して効果を 1 つだけ適用しています。複数の効果を適用するには、BitmapEffectGroup を使用してください。例については「方法 : 複数の視覚効果を作成する」を参照してください。

使用例

DropShadowBitmapEffect を使用して Button の背後に影の外観を作成する方法を次の例に示します。

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">

  <StackPanel>

    <Button Margin="50" Width="200">DropShadow Under this Button
      <Button.BitmapEffect>

      <!-- <BitmapEffectGroup> would go here if you wanted to apply more 
             then one effect to the TextBox. However, in this example only  
             one effect is being applied so BitmapEffectGroup does not need  
             to be included. -->

        <!-- The DropShadowBitmapEffect has several important properties that
             determine characteristics of the drop shadow: 
             - Color: Specifies the color of the drop shadow (in this example black).
             - ShadowDepth: Specifies how far displaced the shadow is from the object
               casting the shadow. Default is 20.
             - Direction: Specifies in what direction the shadow is cast from the object. 
               It is an angle between 0 and 360 with 0 starting on the right hand side 
               and moving counter-clockwise around the object. The value of 320 in this 
               example casts the shadow on the lower right hand side of the button.
             - Noise: Specifies how grainy the drop-shadow is. Range is between 0 and 1.
               Default is 0.
             - Softness: Specifies how soft the shadow. The range is between 0 and 1 with 1
               being the softest. Default is 0.5. 
             - Opacity: Specifies how transparent the shadow is. The range is between 0 
               and 1 with 1 being fully opaque and 0 fully transparent (not visible). The
               default is 1. -->
        <DropShadowBitmapEffect Color="Black" Direction="320" ShadowDepth="25" Softness="1" 
         Opacity="0.5"/>

      </Button.BitmapEffect>
    </Button>

  </StackPanel>

</Page>

前述の例で作成した効果を次の図に示します。

スクリーンショット : DropShadowBitmapEffect

次の例は、ページ上の任意の Button が押されたときに、このボタンに DropShadowBitmapEffect を適用するための Style の使用方法を示しています。

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">

  <!-- Resources define Styles for the entire page. -->
  <Page.Resources>

    <!-- This style applies to any Button on the page. -->
    <Style TargetType="{x:Type Button}">
      <Style.Triggers>

        <!-- When the Button is pressed, apply the drop shadow. -->
        <Trigger Property="IsPressed" Value="true">
          <Setter Property = "BitmapEffect" >
            <Setter.Value>
              <DropShadowBitmapEffect Color="Black" Direction="320"  
               ShadowDepth="25" Softness="1" Opacity="0.5"/>
            </Setter.Value>
          </Setter>
        </Trigger>
      </Style.Triggers>
    </Style>
  </Page.Resources>

  <StackPanel>

    <!-- The Style defined above applies to this Button which creates
         a drop shadow when the button is pressed down. -->
    <Button Width="200" >Press Me!</Button>

  </StackPanel>

</Page>

次の例では、Button がクリックされたときに、このボタンに DropShadowBitmapEffect を適用するためのコードの使用方法を示しています。

次に示すのは、この例に対する Extensible Application Markup Language (XAML) です。

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.DropShadowExample" >

  <StackPanel>

    <Button Click="OnClickDropShadowButton" Margin="50"  Width="200">
    Click to Create Drop Shadow!</Button>

  </StackPanel>

</Page>

マークアップ例のイベントを処理するコードを次に示します。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Effects;

namespace SDKSample
{

    public partial class DropShadowExample : Page
    {

        // Add Blur effect.
        void OnClickDropShadowButton(object sender, RoutedEventArgs args)
        {

            // Get a reference to the Button.
            Button myButton = (Button)sender;

            // Initialize a new DropShadowBitmapEffect that will be applied
            // to the Button.
            DropShadowBitmapEffect myDropShadowEffect = new DropShadowBitmapEffect();

            // Set the color of the shadow to Black.
            Color myShadowColor = new Color();
            myShadowColor.ScA = 1; // Note that the alpha value is ignored by Color property. The Opacity property is used to control the alpha.
            myShadowColor.ScB = 0;
            myShadowColor.ScG = 0;
            myShadowColor.ScR = 0;
            myDropShadowEffect.Color = myShadowColor;

            // Set the direction of where the shadow is cast to 320 degrees.
            myDropShadowEffect.Direction = 320;

            // Set the depth of the shadow being cast.
            myDropShadowEffect.ShadowDepth = 25;

            // Set the shadow softness to the maximum (range of 0-1).
            myDropShadowEffect.Softness = 1;

            // Set the shadow opacity to half opaque or in other words - half transparent.
            // The range is 0-1.
            myDropShadowEffect.Opacity = 0.5;

            // Apply the bitmap effect to the Button.
            myButton.BitmapEffect = myDropShadowEffect;

        }

    }
}

次の例は、DropShadowBitmapEffectShadowDepth および Softness にアニメーションを適用し、クリックすると画面上に浮き上がってくるように見える Button を作成する方法を示します。

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" >

  <StackPanel>

    <Button Margin="50" Width="200" Name="myButton">
      Click Me to Animate Drop Shadow!
      <Button.BitmapEffect>

        <!-- This BitmapEffect is targeted by the animation. -->
        <DropShadowBitmapEffect x:Name="myDropShadowBitmapEffect" Color="Black"   
         ShadowDepth="0" />
      </Button.BitmapEffect>
      <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
          <BeginStoryboard>
            <Storyboard>

              <!-- Animate the movement of the button. -->
              <ThicknessAnimation
               Storyboard.TargetProperty="Margin" Duration="0:0:0.5" 
               From="50,50,50,50" To="0,0,50,50" AutoReverse="True" />

              <!-- Animate shadow depth of the effect. -->
              <DoubleAnimation
               Storyboard.TargetName="myDropShadowBitmapEffect"
               Storyboard.TargetProperty="ShadowDepth"
               From="0" To="30" Duration="0:0:0.5"
               AutoReverse="True" />

              <!-- Animate shadow softness of the effect. As 
                   the Button appears to get farther from the shadow,  
                   the shadow gets softer. -->
              <DoubleAnimation
               Storyboard.TargetName="myDropShadowBitmapEffect"
               Storyboard.TargetProperty="Softness"
               From="0" To="1" Duration="0:0:0.5"
               AutoReverse="True" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Button.Triggers>
    </Button>

  </StackPanel>

</Page>

参照

処理手順

方法 : オブジェクトの外縁でグロー効果を作成する

方法 : ビジュアルにぼかし効果を適用する

方法 : 複数の視覚効果を作成する

方法 : 複数の視覚効果をアニメーション化する

概念

ビットマップ効果の概要