使用wpf与API-SetWindowCompositionAttribute编写磨砂透明窗口
vb.net吧
全部回复
仅看楼主
level 11
1227961 楼主
2017年04月24日 04点04分 1
level 11
1227961 楼主
首先添加引用
Imports System.Runtime.InteropServices
Imports System.Windows.Interop
2017年04月24日 04点04分 2
level 11
1227961 楼主
Structure WindowsCompostionAttributeData
Public Attribute As WindowCompositionAttribute
Public Data As IntPtr
Public SizeOfData As Integer
End Structure
Enum WindowCompositionAttribute
WCA_ACCENT_POLICY = 19
End Enum
Enum AccentState
ACCENT_DISABLED = 0
ACCENT_ENABLE_GRADIENT = 1
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
ACCENT_ENABLE_BLURBEHIND = 3
ACCENT_INVALID_STATE = 4
End Enum
Structure AccentPolicy
Public AccentState As AccentState
Public AccentFlags As Integer
Public GradientColor As Integer
Public A***tionId As Integer
End Structure
Private Declare Function SetWindowCompositionAttribute Lib "user32.dll" (ByVal Hwnd As IntPtr, ByRef data As WindowsCompostionAttributeData) As Long
调用API
2017年04月24日 04点04分 3
level 11
1227961 楼主
Sub EnableBlur()
Dim windowHelper As New WindowInteropHelper(Me)
Dim accent As New AccentPolicy()
Dim accentStructSize = Marshal.SizeOf(accent)
accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND
Dim accentPtr = Marshal.AllocHGlobal(accentStructSize)
Marshal.StructureToPtr(accent, accentPtr, False)
Dim data As New WindowsCompostionAttributeData() With {
.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
.SizeOfData = accentStructSize,
.Data = accentPtr
}
SetWindowCompositionAttribute(windowHelper.Handle, data)
Marshal.FreeHGlobal(accentPtr)
End Sub
定义一个sub过程
然后在窗口的loaded事件里调用EnableBlur()
就可以啦
2017年04月24日 04点04分 4
level 11
1227961 楼主
忘了说,窗口必须是半透明的。这点我就不赘述了。
2017年04月24日 04点04分 5
level 11
1227961 楼主
另外,你得用Windows10 1607以上版本。
2017年04月24日 04点04分 6
level 11
1227961 楼主
窗口颜色,我也不多说了。
2017年04月24日 04点04分 7
level 13
不错。回头研究。
2017年04月24日 06点04分 8
level 11
就是有颜色的近视眼效果么
2017年04月24日 16点04分 9
level 13
2017年04月25日 01点04分 10
层主啊,我看到github上riverar/sample-win32-acrylicblur有用这个api做亚克力模糊,1803版本一下运行正常,但是昨天更新了系统1903,原本丝滑的窗口移动变得迟钝了,感觉有阻力一样,这是啥原因?
2019年10月27日 07点10分
好像找到原因了,github上 sourcechord/FluentWPF,他写的AcrylicBlur中有注释到//在Windows10 1903或更高版本中,如果使用ACCENT_ENABLE_ACRYLICBLURBEHIND,它将不会通过拖动窗口来跟随鼠标操作。在修改SetWindowCompositionAttribute函数的行为之前,请勿使用ACCENT_ENABLE_ACRYLICBLURBEHIND。
2019年10月27日 07点10分
level 11
1227961 楼主
看了楼上吧主的代码,我深深的感觉到我的代码太业余了。
2017年04月25日 04点04分 11
我还没怎么封装这个功能呢。前一阵子我把它封装成了附加属性,但是没发布出来。
2017年04月29日 03点04分
level 1
有深意,研究一下
2017年04月25日 10点04分 12
level 8
好帖
2017年04月27日 00点04分 13
level 11
NEON既视感[滑稽][滑稽][滑稽]
2017年05月09日 10点05分 14
我现在用的16188预览版,里面的“人脉”应用就是用的Project NEON,效果还没这个好。现在“人脉”这个应用的毛玻璃效果感觉玻璃好久没擦一样。
2017年05月09日 14点05分
@1227961 我也是16188,确实不咋好看,但是起码比RS2强点……
2017年05月10日 00点05分
@1227961 “图片”,开始菜单,“计算器”什么的都用上NEON了
2017年05月10日 00点05分
level 11
1227961 楼主
此法已失效,特此告知。
2018年08月05日 01点08分 15
1 2 尾页