Condividi tramite


Procedura: creare un effetto visivo di ombreggiatura

Aggiornamento: novembre 2007

L'oggetto DropShadowBitmapEffect può essere utilizzato per creare un effetto simile a un'ombra dietro un oggetto visibile. Di seguito viene elencata una serie di esempi che illustrano quanto indicato:

  • Come utilizzare un markup semplice per applicare l'effetto a un oggetto.

  • Come utilizzare un oggetto Style per applicare l'effetto a uno o più oggetti.

  • Come utilizzare il codice per applicare l'effetto a un oggetto.

  • Come utilizzare un'animazione per animare le proprietà di un effetto applicato a un oggetto.

Nota: in tutti gli esempi riportati di seguito viene applicato un singolo effetto a un oggetto. Per applicare più effetti, è possibile utilizzare BitmapEffectGroup. Per alcuni esempi, vedere Procedura: creare più effetti visivi.

Esempio

Nell'esempio seguente viene mostrato come utilizzare un oggetto DropShadowBitmapEffect per creare un'ombra dietro un oggetto 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>

Nella figura riportata di seguito viene illustrato l'effetto creato nell'esempio precedente.

Schermata: DropShadowBitmapEffect

Nell'esempio riportato di seguito viene illustrato come utilizzare un oggetto Style per applicare un oggetto DropShadowBitmapEffect a qualsiasi oggetto Button nella pagina quando viene selezionato.

<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>

Nell'esempio riportato di seguito viene illustrato come utilizzare il codice per applicare un oggetto DropShadowBitmapEffect a un oggetto Button quando si fa clic su di esso.

Di seguito viene riportata la sintassi Extensible Application Markup Language (XAML) relativa all'esempio.

<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>

Di seguito viene riportato il codice per la gestione dell'evento relativo al markup di esempio.

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;

        }

    }
}

Nell'esempio seguente viene mostrato come animare le proprietà ShadowDepth e Softness dell'oggetto DropShadowBitmapEffect in modo che venga visualizzato come oggetto Button che si solleva dalla superficie dello schermo una volta che è stato fatto clic su di esso.

<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>

Vedere anche

Attività

Procedura: creare un effetto alone sul bordo esterno di un oggetto

Procedura: applicare un effetto di sfocatura a un oggetto Visual

Procedura: creare più effetti visivi

Procedura: animare più effetti visivi

Concetti

Cenni preliminari sugli effetti bitmap