WPF预设有Aero, Classic, Luna, Royale主题, WPF程序会根据Windows主题决定WPF程序所使用的控件风格, 而且当Windows主题不是Aero, Luna或Royale, 而是其他主题的话, WPF将会采用比较丑的Classic主题, 那我怎么让WPF程序使用指定的主题呢?

其实很简单, 下面实例如何设定为Aero主题:
在WPF项目中添加PresentationFramework.Aero.dll这个引用, 然后在程序的Resources中加入
    <Application.Resources>
        
<ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/aero.normalcolor.xaml" />
    
</Application.Resources>
这相当于把PresentationFramework.Aero程序集中的空间风格代码引用到当前程序中.
注意, 项目必须引用主题对应的Assembly, 比如PresentationFramework.Aero.dll, PresentationFramework.Royale.dll等等, 它们可以在"Program Files\Reference Assemblies\Microsoft\Framework\v3.0" 或者 GAC中找到.
上面的XAML代码也可以使用相应的C#代码代替, 比如在Application的Startup事件处理函数中加入代码:
    protected override void OnStartup(StartupEventArgs e)
    {
        
base.OnStartup(e);
        Uri aero 
= new Uri("/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/aero.normalcolor.xaml", UriKind.Relative);
        Resources.MergedDictionaries.Add(Application.LoadComponent(aero) 
as ResourceDictionary); 
    }
当然也可以在Window的Resources中加入这个ResourceDictionary, 那样就只会在这个Window中采用此主题.

要指定其他主题的方法也差不多, 也就是引用相关dll和添加Resources.
WPF中的Aero主题和Windows Vista里的Aero主题控件风格完全一样, 包括譬如MouseEnter时的动画效果, 下面是在启用了Luna Element 4 视觉效果的Windows 2003系统运行的使用了Aero主题的WPF程序截图:



但WPF里的Aero主题在非Windows Vista系统是没有玻璃效果的.. 那不是GDI就能做到的东西..
Posted on 2006-11-08 18:07  Adrian H.  阅读(5918)  评论(6编辑  收藏  举报