我正在尝试使用DWM API在VB.NET 2010应用程序中实现Aero Glass效果,但是如同函数调用所示,它扩展了框架的外观到客户区域,如果窗体没有边框,那么什么也不会发生,窗体将变得不可见。那么,我可以在没有任何边框的窗体中得到Aero Glass效果吗?
我正在尝试使用DWM API在VB.NET 2010应用程序中实现Aero Glass效果,但是如同函数调用所示,它扩展了框架的外观到客户区域,如果窗体没有边框,那么什么也不会发生,窗体将变得不可见。那么,我可以在没有任何边框的窗体中得到Aero Glass效果吗?
DwmExtendFrameIntoClientArea
字面上将窗口边框的透明玻璃效果扩展到其客户区域,这意味着如果您的窗体FormBorderStyle
设置为"None",则您的窗口将有效地变为不可见。
相反,您需要使用DwmEnableBlurBehindWindow
API,该API可以在无需窗口框架/边框的情况下在窗口上启用玻璃模糊效果。它接受两个参数。第一个参数(hWnd
)是您希望应用模糊效果的窗体的句柄。第二个参数(pBlurBehind
)是通过引用传递的结构,其中包含效果的数据或参数。DWM_BLURBEHIND
结构,它本身包含四个成员。第一个(dwFlags
)是位组合的常量值,指示已设置此结构的哪些成员。第二个(fEnable
)指示您是否要启用或禁用模糊效果。第三个(hRgnBlur
)允许您指定客户区域内要应用模糊效果的特定区域;将其设置为Nothing
表示整个客户区域都将具有模糊效果。第四个(fTransitionOnMaximized
)允许您指定窗体的着色是否应过渡以匹配最大化的窗口。
以下是您必须在代码中包含的最终API声明,以便使用此函数:
<StructLayout(LayoutKind.Sequential)> _
Private Structure DWM_BLURBEHIND
Public dwFlags As Integer
Public fEnable As Boolean
Public hRgnBlur As IntPtr
Public fTransitionOnMaximized As Boolean
End Structure
Private Const DWM_BB_ENABLE As Integer = &H1
Private Const DWM_BB_BLURREGION As Integer = &H2
Private Const DWM_BB_TRANSITIONONMAXIMIZED As Integer = &H4
<DllImport("dwmapi.dll", PreserveSig:=False)> _
Private Shared Sub DwmEnableBlurBehindWindow(ByVal hWnd As IntPtr, ByRef pBlurBehind As DWM_BLURBEHIND)
End Sub
这里有一个简单的示例,说明如何在特定表单上调用此函数:
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
''#Set the form's border style to None
Me.FormBorderStyle = FormBorderStyle.None
''#Whatever region that you fill with black will become the glassy region
''# (in this example, the entire form becomes transparent)
Me.BackColor = Color.Black
''#Create and populate the blur-behind structure
Dim bb As DWM_BLURBEHIND
bb.dwFlags = DWM_BB_ENABLE
bb.fEnable = True
bb.hRgnBlur = Nothing
''#Enable the blur-behind effect
DwmEnableBlurBehindWindow(Me.Handle, bb)
End Sub
hRgnBlur
成员提供有效的区域,并在dwFlags
成员中添加DWM_BB_BLURREGION
标志。您可以使用Region.GetHrgn
方法获取要指定为hRgnBlur
成员的区域句柄。例如,您可以使用以下代码:Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
''#Set the form's border style to None
Me.FormBorderStyle = FormBorderStyle.None
''#Fill the entire form with black to make it appear transparent
Me.BackColor = Color.Black
''#Create a region corresponding to the area of the form you want to render as glass
Using g As Graphics = Me.CreateGraphics
Dim glassRect As New Rectangle(0, 0, 100, 150)
Using rgn As New Region(glassRect)
''#Create and populate the blur-behind structure
Dim bb As DWM_BLURBEHIND
bb.dwFlags = DWM_BB_ENABLE Or DWM_BB_BLURREGION
bb.fEnable = True
bb.hRgnBlur = rgn.GetHrgn(g)
''#Enable blur-behind effect
DwmEnableBlurBehindWindow(Me.Handle, bb)
End Using
End Using
End Sub