欢迎来到天天文库
浏览记录
ID:24474122
大小:229.50 KB
页数:4页
时间:2018-11-14
《[计算机]将玻璃框扩展到wpf应用程序》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、本主题演示如何将WindowsVista玻璃框扩展至WindowsPresentationFoundation(WPF)应用程序的工作区。说明:此示例仅在运行已启用玻璃效果的桌面窗口管理器(DWM)的WindowsVista计算机上才会起作用。WindowsVistaHomeBasic版本不支持透明玻璃效果。通常利用透明玻璃效果呈现的区域在其他版本的WindowsVista上呈现为不透明。 示例下面的示例演示一个已扩展到InternetExplorer7地址栏的玻璃框。InternetExplore
2、r,扩展的玻璃框位于地址栏后。若要在WPF应用程序上扩展玻璃框,需要访问非托管的API。下面的代码示例为扩展玻璃框工作区所需的两个API执行平台调用(pinvoke)。每个API都是在名为NonClientRegionAPI的类中声明的。C#复制代码[StructLayout(LayoutKind.Sequential)]publicstructMARGINS{publicintcxLeftWidth;//widthofleftborderthatretainsitssizepublicintcxR
3、ightWidth;//widthofrightborderthatretainsitssizepublicintcyTopHeight;//heightoftopborderthatretainsitssizepublicintcyBottomHeight;//heightofbottomborderthatretainsitssize};[DllImport("DwmApi.dll")]publicstaticexternintDwmExtendFrameIntoClientArea(IntPt
4、rhwnd,refMARGINSpMarInset);DwmExtendFrameIntoClientArea是用于将框扩展到工作区中的DWM函数。它有两个参数,一个窗口句柄,一个MARGINS结构。MARGINS用于告知DWM框扩展到工作区中时延伸的程度。若要使用DwmExtendFrameIntoClientArea函数,必须获取一个窗口句柄。在WPF中,可以通过HwndSource的Handle属性获取窗口句柄。在下面的示例中,框扩展至窗口的Loaded事件的工作区。C#复制代码voidOnL
5、oaded(objectsender,RoutedEventArgse){try{//ObtainthewindowhandleforWPFapplicationIntPtrmainWindowPtr=newWindowInteropHelper(this).Handle;HwndSourcemainWindowSrc=HwndSource.FromHwnd(mainWindowPtr);mainWindowSrc.CompositionTarget.BackgroundColor=Color.Fr
6、omArgb(0,0,0,0);//GetSystemDpiSystem.Drawing.Graphicsdesktop=System.Drawing.Graphics.FromHwnd(mainWindowPtr);floatDesktopDpiX=desktop.DpiX;floatDesktopDpiY=desktop.DpiY;//SetMarginsNonClientRegionAPI.MARGINSmargins=newNonClientRegionAPI.MARGINS();//Ext
7、endglassframeintoclientarea//NotethatthedefaultdesktopDpiis96dpi.Themarginsare//adjustedforthesystemDpi.margins.cxLeftWidth=Convert.ToInt32(5*(DesktopDpiX/96));margins.cxRightWidth=Convert.ToInt32(5*(DesktopDpiX/96));margins.cyTopHeight=Convert.ToInt32
8、(((int)topBar.ActualHeight+5)*(DesktopDpiX/96));margins.cyBottomHeight=Convert.ToInt32(5*(DesktopDpiX/96));inthr=NonClientRegionAPI.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle,refmargins);//if(hr<0){//DwmExtendFrameIntoClientAreaF
此文档下载收益归作者所有