如何在没有边框的Windows窗体上获得Aero Glass?

Kus*_*hal 6 vb.net dwm winforms aero-glass

我正在尝试使用DWM API在VB.NET 2010应用程序中查看Aero Glass的表单,但是正如函数调用所示,它将Frame的外观扩展到客户区,如果表单没有边框,则不会发生任何事情和形式将变得无形.那么,我可以在没有任何边框的情况下获得Aero玻璃吗?

Cod*_*ray 13

正如您所说,DwmExtendFrameIntoClientArea从字面上将窗口框架的透明玻璃效果扩展到其客户区域,这意味着如果您的窗体FormBorderStyle设置为"无",您的窗口将实际上不可见.

相反,您需要使用DwmEnableBlurBehindWindowAPI,它可以在窗口上启用玻璃模糊效果,而不需要它具有框架/边框.它需要两个参数.第一个(hWnd)是您希望应用模糊效果的表单的句柄.第二个(pBlurBehind)是通过引用传递的结构,其中包含效果的数据或参数.

因此,您还必须定义DWM_BLURBEHIND结构,该结构本身包含四个成员.first(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
Run Code Online (Sandbox Code Playgroud)

然后,这是一个如何在特定表单上调用此函数的简单示例:

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
Run Code Online (Sandbox Code Playgroud)

相反,如果您只想将模糊效果应用于表单的特定子区域,则需要为该hRgnBlur成员提供有效区域,并将该DWM_BB_BLURREGION标志添加到该dwFlags成员.

您可以使用该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
Run Code Online (Sandbox Code Playgroud)

注意,即使指定一个特定的子区域来应用模糊效果,我仍然将整个表单的背景颜色设置为黑色?这将导致我们指定的区域呈现玻璃般的模糊效果,并且表单的其余部分显示为透明.当然,您可以将表单背景颜色的其余部分设置为您想要的任何颜色(尽管确保填充您想要显示为黑色的玻璃矩形,如前所述),但它将显示为部分透明,没有玻璃般的模糊效果.MSDN解释了为什么会这样:

将模糊效果应用于窗口的子区域时,窗口的Alpha通道用于非模糊区域.这可能导致窗户的非模糊区域出现意外的透明度.因此,将模糊效果应用于子区域时要小心.

就我而言,这使得将这种效果仅应用于窗体窗口的一个子区域相对毫无价值.在我看来它唯一可能有意义的是,如果你想将任意非矩形形状渲染为玻璃状,其余形状保持透明.