|
| 1 | +Imports System |
| 2 | +Imports System.Runtime.InteropServices |
| 3 | + |
| 4 | +Namespace HashTool |
| 5 | + |
| 6 | + Friend Module DwmHelper |
| 7 | + |
| 8 | + ' --- Win11 DWM attributes --- |
| 9 | + Private Const DWMWA_WINDOW_CORNER_PREFERENCE As Integer = 33 |
| 10 | + Private Const DWMWA_BORDER_COLOR As Integer = 34 |
| 11 | + Private Const DWMWA_SYSTEMBACKDROP_TYPE As Integer = 38 |
| 12 | + |
| 13 | + ' Corner preference values |
| 14 | + Private Const DWMWCP_DEFAULT As Integer = 0 |
| 15 | + Private Const DWMWCP_DONOTROUND As Integer = 1 |
| 16 | + Private Const DWMWCP_ROUND As Integer = 2 |
| 17 | + Private Const DWMWCP_ROUNDSMALL As Integer = 3 |
| 18 | + |
| 19 | + ' Backdrop types (Win11) |
| 20 | + Private Const DWMSBT_MAINWINDOW As Integer = 2 ' Mica |
| 21 | + |
| 22 | + ' --- Win10 composition fallback --- |
| 23 | + Private Const WCA_ACCENT_POLICY As Integer = 19 |
| 24 | + Private Const ACCENT_ENABLE_BLURBEHIND As Integer = 3 |
| 25 | + Private Const ACCENT_ENABLE_ACRYLICBLURBEHIND As Integer = 4 |
| 26 | + |
| 27 | + <DllImport("dwmapi.dll")> |
| 28 | + Private Function DwmSetWindowAttribute(hwnd As IntPtr, dwAttribute As Integer, ByRef pvAttribute As Integer, cbAttribute As Integer) As Integer |
| 29 | + End Function |
| 30 | + |
| 31 | + <StructLayout(LayoutKind.Sequential)> |
| 32 | + Private Structure ACCENT_POLICY |
| 33 | + Public AccentState As Integer |
| 34 | + Public AccentFlags As Integer |
| 35 | + Public GradientColor As Integer |
| 36 | + Public AnimationId As Integer |
| 37 | + End Structure |
| 38 | + |
| 39 | + <StructLayout(LayoutKind.Sequential)> |
| 40 | + Private Structure WINDOWCOMPOSITIONATTRIBDATA |
| 41 | + Public Attribute As Integer |
| 42 | + Public Data As IntPtr |
| 43 | + Public SizeOfData As Integer |
| 44 | + End Structure |
| 45 | + |
| 46 | + <DllImport("user32.dll")> |
| 47 | + Private Function SetWindowCompositionAttribute(hwnd As IntPtr, ByRef data As WINDOWCOMPOSITIONATTRIBDATA) As Integer |
| 48 | + End Function |
| 49 | + |
| 50 | + Friend Sub TryEnableBackdrop(hwnd As IntPtr) |
| 51 | + ' ✅ 关键:先把 Win11 系统“圆角/边框”关掉,保证跨系统一致 |
| 52 | + TryDisableSystemRounding(hwnd) |
| 53 | + TrySetBorderColorTransparent(hwnd) |
| 54 | + |
| 55 | + ' Win11: Mica |
| 56 | + If TrySetSystemBackdrop(hwnd, DWMSBT_MAINWINDOW) Then Return |
| 57 | + |
| 58 | + ' Win10 fallback:先 Acrylic,再 Blur |
| 59 | + If Not TrySetAccent(hwnd, ACCENT_ENABLE_ACRYLICBLURBEHIND, &HCCFFFFFF) Then |
| 60 | + TrySetAccent(hwnd, ACCENT_ENABLE_BLURBEHIND, 0) |
| 61 | + End If |
| 62 | + End Sub |
| 63 | + |
| 64 | + Private Sub TryDisableSystemRounding(hwnd As IntPtr) |
| 65 | + Try |
| 66 | + Dim pref As Integer = DWMWCP_DONOTROUND |
| 67 | + DwmSetWindowAttribute(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, pref, Marshal.SizeOf(Of Integer)()) |
| 68 | + Catch |
| 69 | + End Try |
| 70 | + End Sub |
| 71 | + |
| 72 | + Private Sub TrySetBorderColorTransparent(hwnd As IntPtr) |
| 73 | + Try |
| 74 | + ' DWMWA_BORDER_COLOR uses COLORREF (0x00BBGGRR). 0 means "default". |
| 75 | + ' We want fully transparent border — Win11 accepts 0x00000000 as "no visible border" in practice. |
| 76 | + Dim color As Integer = 0 |
| 77 | + DwmSetWindowAttribute(hwnd, DWMWA_BORDER_COLOR, color, Marshal.SizeOf(Of Integer)()) |
| 78 | + Catch |
| 79 | + End Try |
| 80 | + End Sub |
| 81 | + |
| 82 | + Private Function TrySetSystemBackdrop(hwnd As IntPtr, backdropType As Integer) As Boolean |
| 83 | + Try |
| 84 | + Dim hr = DwmSetWindowAttribute(hwnd, DWMWA_SYSTEMBACKDROP_TYPE, backdropType, Marshal.SizeOf(Of Integer)()) |
| 85 | + Return hr = 0 |
| 86 | + Catch |
| 87 | + Return False |
| 88 | + End Try |
| 89 | + End Function |
| 90 | + |
| 91 | + Private Function TrySetAccent(hwnd As IntPtr, accentState As Integer, rgba As Integer) As Boolean |
| 92 | + Try |
| 93 | + Dim policy As New ACCENT_POLICY With { |
| 94 | + .AccentState = accentState, |
| 95 | + .AccentFlags = 2, |
| 96 | + .GradientColor = rgba, ' AABBGGRR |
| 97 | + .AnimationId = 0 |
| 98 | + } |
| 99 | + |
| 100 | + Dim size = Marshal.SizeOf(Of ACCENT_POLICY)() |
| 101 | + Dim ptr = Marshal.AllocHGlobal(size) |
| 102 | + Marshal.StructureToPtr(policy, ptr, False) |
| 103 | + |
| 104 | + Dim data As New WINDOWCOMPOSITIONATTRIBDATA With { |
| 105 | + .Attribute = WCA_ACCENT_POLICY, |
| 106 | + .Data = ptr, |
| 107 | + .SizeOfData = size |
| 108 | + } |
| 109 | + |
| 110 | + Dim ret = SetWindowCompositionAttribute(hwnd, data) |
| 111 | + Marshal.FreeHGlobal(ptr) |
| 112 | + |
| 113 | + Return ret <> 0 |
| 114 | + Catch |
| 115 | + Return False |
| 116 | + End Try |
| 117 | + End Function |
| 118 | + |
| 119 | + End Module |
| 120 | + |
| 121 | +End Namespace |
0 commit comments