用C++代码中实现WPF动画 -- Opacity Animation

                        用C++代码中实现WPF动画 -- Opacity Animation

    很多人都习惯使用Blend来帮助编辑XAML文件,生成很多动画。但在实际开发中,用代码来实现动画还是很实用的,而且代码的逻辑开发能力更强,更容易控制,这方面C#的例子已经很多了,下面我介绍几个C++的例子。
    首先介绍少渐隐渐现,也就是Alpha Animation。代码如下

     /**/ /*
    *  Take Label for example
    
*/

    
//  1, Find the lable by its name, The name define in the xaml file
    Label ^  pColorLabel  =  (Label ^ )page -> FindName( " ColorAnimationLabel " );

    
//  2, Define a DoubleAnimation object
    DoubleAnimation ^  pDoubleAnimation  =  gcnew DoubleAnimation();

    
//  3, Set from to and duration
    pDoubleAnimation -> From  =   1 ;
    pDoubleAnimation
-> To  =   0 ;
    pDoubleAnimation
-> Duration  =  Duration(TimeSpan::FromSeconds( 3 ));

    
//  4, Create a storyboard(Timeline)
    Storyboard ^  pStoryboard  =  gcnew Storyboard();

    
//  5, Set the DoubleAnimation's target name
    pStoryboard -> SetTargetName(pDoubleAnimation, _T( " ColorAnimationLabel " ));

    
//  6, Set the DoubleAnimation's property
    pStoryboard -> SetTargetProperty(pDoubleAnimation, gcnew PropertyPath(Label::OpacityProperty));

    
//  7, Add the DoubleAnimation object to the storyboard
    pStoryboard -> Children -> Add(pDoubleAnimation);

    
//  8, Start the animation
    pStoryboard -> Begin(pColorLabel);

上面代码所用的XAML如下
< Page
    
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    
>
  
< Grid >
    
< DockPanel >
      
< Button  Name ="ColorAnmationButton"  Width ="100"  Height ="50"  Background ="LightBlue" > Color Anmation </ Button >
      
< Label  Name ="ColorAnimationLabel"  Width ="200"  Height ="50"  Background ="Red" >
      
</ Label >
    
</ DockPanel >
  
</ Grid >
</ Page >

转载于:https://www.cnblogs.com/sun9411323/archive/2007/12/13/994227.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值