您的位置:首页 > 其它

WPF中打造半透明窗口效果 - [WPF开发]

2011-08-24 15:23 344 查看
自Windows Vista起,Windows的桌面效果就增加了对Aero透明玻璃效果的支持,系统默认的话只是对标题栏或者菜单栏进行半透明处理,如果想实现整个窗口都Aero化的话,得引用一个系统DLL来实现。首先看效果图:



这个效果是通过DWM(Destop Window Manager)中的一个API来实现的,关键的代码如下:

1 private void ExtendAeroGlass(Window window)

2 {

3 try

4 {

5 // 为WPF程序获取窗口句柄

6 IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle;

7 HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);

8 mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;

9

// 设置Margins

MARGINS margins = new MARGINS();

// 扩展Aero Glass

margins.cxLeftWidth = -1;

margins.cxRightWidth = -1;

margins.cyTopHeight = -1;

margins.cyBottomHeight = -1;

int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);

if (hr < 0)

{

MessageBox.Show("DwmExtendFrameIntoClientArea Failed");

}

}

catch (DllNotFoundException)

{

Application.Current.MainWindow.Background = Brushes.White;

}

}

[StructLayout(LayoutKind.Sequential)]

public struct MARGINS

{

public int cxLeftWidth;

public int cxRightWidth;

public int cyTopHeight;

public int cyBottomHeight;

};

[DllImport("DwmApi.dll")]

public static extern int DwmExtendFrameIntoClientArea(

IntPtr hwnd,

ref MARGINS pMarInset);

从代码中得知,我们需要引用一个DwmApi.dll文件,然后定义一个函数去实现拓展Aero区域,从而实现整个窗口的Aero化。

参考资料:

1、关于WPF窗口的知识:http://www.cnblogs.com/libenqing/archive/2011/04/07/2007817.html
2、原文出处:http://www.cnblogs.com/gnielee/archive/2010/10/04/windows7-extend-aero-glass.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: