5

给WPF程序增加玻璃效果

 3 years ago
source link: https://www.cnblogs.com/TianFang/archive/2010/02/18/1669275.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在Vista中增加了一种Aero新界面,也就是我们所说的玻璃效果,这种效果比较酷,我也常常喜欢在自己写的小程序中加入这种效果。

实现这种效果并不难,也就是两个API,MSDN的文章将玻璃框扩展到 WPF 应用程序详细介绍了如何实现这种效果。在google上拿"C# DwmExtendFrameIntoClientArea"做关键字,也能搜到一大把。

我一般是用的CodeProject的文章Adding Glass Effect to WPF using Attached Properties上所述的那样,通过依赖属性注入这种效果,只要在窗口中加一句话src:GlassEffect.IsEnabled="True"即可使能这种效果,用起来非常方便。通过这种方式实现起来的效果如下:

看起来一起都非常简单,但这种方法只是仅仅实现了玻璃效果,还有几个不完善的地方:

  1. 这种方法只能实现全屏玻璃效果,不能实现部分玻璃效果。
  2. 实现玻璃效果后,我们往往还需要隐藏标题栏中的图标和文字。
  3. 那些玻璃化的部分不能像标题栏那样通过鼠标拖动窗口。

因此,我将那个代码改动了一下,增加了上述功能,代码如下:  

使用示例如下:

<Window x:Class="WpfApplication.MainWindow"
    … …
    src:GlassEffect.Info="{util:GlassInfo TopMargin=32, ShowIcon=False, ShowTitle=False}">

最终效果如下:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK