《将玻璃框扩展到 wpf 应用程序》由会员分享,可在线阅读,更多相关《将玻璃框扩展到 wpf 应用程序(4页珍藏版)》请在金锄头文库上搜索。
1、本主题演示如何将 Windows Vista 玻璃框扩展至 Windows Presentation Foundation (WPF) 应用程序的工作区。说明:说明:此示例仅在运行已启用玻璃效果的桌面窗口管理器 (DWM) 的 Windows Vista 计算机上才会起作用。Windows Vista Home Basic 版本不支持透明玻璃效果。通常利用透明玻璃效果呈现的区域在其他版本的 Windows Vista 上呈现为不透明。示例 下面的示例演示一个已扩展到 Internet Explorer 7 地址栏的玻璃框。InternetInternet ExplorerExplorer,扩展
2、的玻璃框位于地址栏后。,扩展的玻璃框位于地址栏后。若要在 WPF 应用程序上扩展玻璃框,需要访问非托管的 API。下面的代码示例为扩展玻璃框工作区所需的两个 API 执行平台调用 (pinvoke)。每个 API 都是在名为 NonClientRegionAPINonClientRegionAPI 的类中声明的。C# 复制代码 StructLayout(LayoutKind.Sequential)public struct MARGINSpublic int cxLeftWidth; / width of left border that retains its sizepublic int
3、cxRightWidth; / width of right border that retains its sizepublic int cyTopHeight; / height of top border that retains its sizepublic int cyBottomHeight; / height of bottom border that retains its size;DllImport(“DwmApi.dll“)public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd,ref MARGI
4、NS pMarInset);DwmExtendFrameIntoClientArea 是用于将框扩展到工作区中的 DWM 函数。它有两个参数,一个窗口句柄,一个 MARGINS 结构。MARGINS 用于告知 DWM 框扩展到工作区中时延伸的程度。若要使用 DwmExtendFrameIntoClientArea 函数,必须获取一个窗口句柄。在 WPF 中,可以通过 HwndSource 的 Handle 属性获取窗口句柄。在下面的示例中,框扩展至窗口的 Loaded 事件的工作区。C# 复制代码 void OnLoaded(object sender, RoutedEventArgs e)t
5、ry/ Obtain the window handle for WPF applicationIntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);/ Get System DpiSystem.Drawing.Graphics desktop = Sys
6、tem.Drawing.Graphics.FromHwnd(mainWindowPtr);float DesktopDpiX = desktop.DpiX;float DesktopDpiY = desktop.DpiY;/ Set MarginsNonClientRegionAPI.MARGINS margins = new NonClientRegionAPI.MARGINS();/ Extend glass frame into client area/ Note that the default desktop Dpi is 96dpi. The margins are/ adjust
7、ed for the system Dpi.margins.cxLeftWidth = Convert.ToInt32(5 * (DesktopDpiX / 96);margins.cxRightWidth = Convert.ToInt32(5 * (DesktopDpiX / 96);margins.cyTopHeight = Convert.ToInt32(int)topBar.ActualHeight + 5) * (DesktopDpiX / 96);margins.cyBottomHeight = Convert.ToInt32(5 * (DesktopDpiX / 96);int hr = NonClientRegionAPI.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);/if (hr PathSearch下图说明扩展至 WPF 应用程序的玻璃框。玻璃框扩展至一个玻璃框扩展至一个 WPF 应用程序。应用程序。