问题描述
我试图在带有DWM API的VB.NET 2010应用程序中的表单中显示Aero Glass,但是正如函数调用所建议的那样,它将Frame的外观扩展到客户区域,如果表单没有边框,则不会有任何效果发生,形式将变得无形.所以,我能以没有任何边界的形式获得Aero玻璃吗?... ??
I'm trying to have Aero Glass look in my forms in VB.NET 2010 app with DWM API, but as function call suggests, it extends look of Frame to the client area, and if form has no border, nothing will happen and form will become invisible. So, can I get Aero glass in a form without any border.... ??
推荐答案
正如您所说的,DwmExtendFrameIntoClientArea
确实将窗口框架的透明玻璃效果扩展到其客户区,这意味着如果窗体的FormBorderStyle
设置为无",则您的窗口实际上将不可见.
As you've said, DwmExtendFrameIntoClientArea
literally extends the transparent glass effect of the window's frame into its client area, which means that if your form's FormBorderStyle
is set to "None", your window will effectively be invisible.
相反,您需要使用 DwmEnableBlurBehindWindow
API ,该API可以启用不需要窗口具有边框/边框的玻璃上的玻璃模糊效果..它具有两个参数.第一个(hWnd
)是希望将模糊效果应用到的表单的句柄.第二个(pBlurBehind
)是通过引用传递的结构,其中包含该效果的数据或参数.
Instead, you need to use the DwmEnableBlurBehindWindow
API, which enables the glassy blur effect on a window without requiring it to have a frame/border. It takes two parameters. The first (hWnd
) is the handle to the form that you wish to apply the blur behind effect to. The second (pBlurBehind
) is a structure passed by reference that contains data or parameters for the effect.
因此,您还必须定义 DWM_BLURBEHIND
结构,该结构本身包含四个成员.第一个(dwFlags
)是常量值的按位组合指示已设置此结构的哪些成员.第二个(fEnable
)指示要启用还是禁用模糊效果.第三个(hRgnBlur
)允许您在客户端区域中指定将要应用模糊效果的特定区域;将此设置为Nothing
表示整个客户区将具有模糊效果.第四个(fTransitionOnMaximized
)允许您指定是否应转换表单的颜色以匹配最大化的窗口.
Therefore, you also have to define the DWM_BLURBEHIND
structure, which itself contains four members. The first (dwFlags
) is a bitwise combination of constant values that indicate which members of this structure have been set. The second (fEnable
) indicates whether you want to enable or disable the blur effect. The third (hRgnBlur
) allows you to specify a particular region within the client area that the blur effect will be applied to; setting this to Nothing
indicates that the entire client area will have the blur effect. The fourth (fTransitionOnMaximized
) allows you to specify whether or not the form's colorization should transition to match the maximized windows.
要使用此功能,以下是必须包含在代码中的最终API声明:
Here are the final API declarations that you have to include in your code in order to use this function:
<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
然后是一个简单的示例,说明如何以特定形式调用此函数:
And then here's a simple example of how you would call this function on a particular form:
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
成员提供有效区域,并将DWM_BB_BLURREGION
标志添加到dwFlags
成员.
If instead, you only want to apply the blur behind effect to a particular subregion of the form, you will need to supply a valid region for the hRgnBlur
member, and add the DWM_BB_BLURREGION
flag to the dwFlags
member.
您可以使用 Region.GetHrgn
方法来获取要指定为hRgnBlur
成员的区域的句柄.例如,除了上面的代码,您可以使用以下代码:
You can use the Region.GetHrgn
method to get a handle to the region you want to specify as the hRgnBlur
member. For example, instead of the above code, you can use the following:
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
请注意,即使在指定要应用模糊效果的特定子区域时,如何仍将整个表单的背景色设置为黑色?这将使我们指定的区域呈现出玻璃状的模糊效果,而表单的其余部分则显得透明.当然,您可以将表单的其余背景颜色设置为所需的任何颜色(尽管像以前一样,请确保将要显示为玻璃的矩形填充为黑色),但是它将显示为部分透明,只是没有玻璃状的模糊效果. MSDN 解释了为什么会这样:
Notice how, even when specifying a particular subregion to apply the blur-behind effect to, I still set the entire form's background color to black? This will cause the region we specified to render with a glassy blur-behind effect, and the rest of the form to appear transparent. Of course, you can set the rest of the form's background color to any color that you want (although make sure to fill the rectangle that you want to appear as glass with the color black, as before), but it will appear as partially transparent, just without the glassy blur-behind effect. MSDN explains why this is the case:
就我而言,这使得将这种效果仅应用于表单窗口的子区域相对毫无价值.在我看来,这似乎唯一有意义的一次是,如果您想将任意非矩形形状呈现为玻璃状,而其余的形状保持透明.
As far as I'm concerned, that makes applying this effect to only a subregion of the form's window relatively worthless. The only time it seems to me like it might make sense is if you want to render an arbitrary non-rectangular shape as glassy, with the rest of the form remaining transparent.
这篇关于如何在无边框的Windows窗体上获取Aero Glass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!