WPF implements a gradient fade-in and fade-out login window

Source: Internet
Author: User

WPF implements a gradient fade-in and fade-out login window
1. Implementation Principle

1.1 Use the UIElement. OpacityMask attribute to change the opacity of the object area. It can make specific areas of elements transparent or partially transparent to achieve relatively new results.

1.2 The OpacityMask attribute accepts any paint brush. You can use the LinearGradientBrush linear gradient Paint Brush to animation each color point in the gradient paint brush.

2. Gradient fade-in implementation

The gradient fade-in effect can be implemented by triggering the Loaded event through the event trigger, so it can be implemented only in the front-end XAML language.

2.1 set the OpacityMask attribute of the object

<Grid.OpacityMask>    <LinearGradientBrush StartPoint="0.5,1" EndPoint="0.5,0">        <GradientStop Color="#00000000" Offset="0"/>        <GradientStop Color="#00000000" Offset="1"/>        <GradientStop Color="#00000000" Offset="1"/>    </LinearGradientBrush></Grid.OpacityMask>

2.2 set object event triggers

<Grid.Triggers>    <EventTrigger RoutedEvent="Loaded">        <EventTrigger.Actions>            <BeginStoryboard>                <Storyboard>                    <DoubleAnimation From="1" To="0" Duration="0:0:1.5" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[1].Offset"/>                    <DoubleAnimation From="1" To="0" Duration="0:0:1" BeginTime="0:0:0.5" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Offset"/>                    <ColorAnimation To="#FF000000" Duration="0" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Color"/>                </Storyboard>            </BeginStoryboard>        </EventTrigger.Actions>    </EventTrigger></Grid.Triggers>
3. Implement gradient fade-out

Gradient fade-out effect. Because event trigger events need to be triggered by routing events, you need to use backend code.

3.1 prepare the animation and painting resources for gradual fade-out

<Window.Resources>    <Storyboard x:Key="ClosedStoryboard" Storyboard.TargetName="LoginGrid">        <DoubleAnimation From="1" To="0" Duration="0:0:2" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[1].Offset"/>        <DoubleAnimation From="1" To="0" Duration="0:0:1.5" BeginTime="0:0:0.5"Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Offset"/>        <ColorAnimation To="#00000000" Duration="0" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Color"/>    </Storyboard>    <LinearGradientBrush x:Key="ClosedBrush" StartPoint="0.5,0" EndPoint="0.5,1">        <GradientStop Color="#FF000000" Offset="0"/>        <GradientStop Color="#FF000000" Offset="1"/>        <GradientStop Color="#FF000000" Offset="1"/>    </LinearGradientBrush></Window.Resources>

3.2 The backend is implemented through the Click Event of the BIND button.

private void btnCancel_Click(object sender, RoutedEventArgs e){    this.IsEnabled = false;    LoginGrid.OpacityMask = this.Resources["ClosedBrush"] as LinearGradientBrush;    Storyboard std = this.Resources["ClosedStoryboard"] as Storyboard;    std.Completed += delegate { this.Close(); };    std.Begin();}
4. Running Effect

4.1 as follows

 


Wpf how do I fade in and out an image before running a method?

First make an image Opacity animation .. then execute your method at the end of the animation.
 
How is the effect of the program fade in and out?

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> fade-in and fade-out effect </title>
<SCRIPT>
Var intTimeStep = 20;
Var isIe = (window. ActiveXObject )? True: false;
Var intAlphaStep = (isIe )? 5: 0. 05;
Var curSObj = null;
Var myobj = null;
Var mypacity = null;
Var curOpacity = null;
Function startObjVisible (objId)
{
CurSObj = document. getElementById (objId );
SetObjState ();
}
Function setObjState (evTarget)
{
If (curSObj. style. display = "") {curOpacity = 1; mypacity = 0; setObjClose ();}
Else {
If (isIe)
{
CurSObj.style.css Text = 'display: none; Z-INDEX: 1; FILTER: alpha (opacity = 0); POSITION: absolute ;';
CurSObj. filters. alpha. opacity = 0;
} Else
{
CurSObj. style. opacity = 0;
Myobj. style. opacity = 1;
}
CurSObj. style. display = '';
Document. getElementById ("MyDiv"). style. display = "none ";
CurOpacity = 0;
Mypacity = 1;
SetObjOpen ();
}
}
Function setObjOpen ()
{
If (isIe)
{
CurSObj. filters. alpha. opacity + = intAlphaStep;
If (curSObj. filters. alpha. opacity <100) setTimeout ('setobjopen () ', intTimeStep );
} Else {
CurOpacity + = intAlphaStep;
CurSObj. style. opacity = curOpacity;
If (curOpacity <1) setTimeout ('setobjopen () ', intTimeStep );
}
}
Function setObjClose ()
{
If (isIe)
{
CurSObj. filters. alpha. opacity-= intAlphaStep;
If (curSObj. filters. alpha. opacity> 0 ){
SetTimeout ('setobjclose () ', intTimeStep );}
Else {curSObj. style. display = "none" ...... the remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.