用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>
posted on 2007-12-13 22:30 Jeffery Sun 阅读(1956) 评论(6)  编辑 收藏

  回复  引用    
2007-12-13 22:54 | Flair [未注册用户]
...只是用managed c++而已
  回复  引用  查看    
2007-12-14 09:20 | panlei      
请问一下,一般的winform控件在WPF里面可以使用吗???
  回复  引用  查看    
2007-12-14 12:36 | Jeffery Sun      
不管是WinForm的控件还是MFC的控件窗口,都可以Host到WPF的窗口中。具体Host的方法稍后我会做个简单介绍。
  回复  引用  查看    
2007-12-14 17:20 | 周银辉      
晕~~~managed c++,你用任意一款.NET支持的语言都可以啊
  回复  引用  查看    
2007-12-14 20:46 | Jeffery Sun      
Managed C++是支持.Net的语言中用户最少的,但是它也为C++程序员带来了很多方便,C++程序员基本上可以不用学习就可以使用C++/CLI + WPF + C++进行开发,也可以很容易使用已有的C++代码,包括Dll,Lib,COM等。以及MFC,STL等。

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-12-13 22:35 编辑过


相关链接: