[VB.NET] Hiệu ứng Acrylic, Mica, Tabbed Blur Effect trên Winform
Giao diện demo ứng dụng:Ở màn hình này, các bạn có thể chọn giao diện Theme Dark/Light ở nút button.
Video demo ứng dụng Blur Effect VB.NET:
Đầu tiên các bạn tạo 1 class Pinvoke.vb:
Imports System.Runtime.InteropServices Public Class PInvoke Public Class ParameterTypes <Flags> Public Enum DWMWINDOWATTRIBUTE DWMWA_USE_IMMERSIVE_DARK_MODE = 20 DWMWA_SYSTEMBACKDROP_TYPE = 38 End Enum <StructLayout(LayoutKind.Sequential)> Public Structure MARGINS Public cxLeftWidth As Integer Public cxRightWidth As Integer Public cyTopHeight As Integer Public cyBottomHeight As Integer End Structure End Class Public Class Methods <DllImport("DwmApi.dll")> Public Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, ByRef pMarInset As ParameterTypes.MARGINS) As Integer End Function <DllImport("dwmapi.dll")> Public Shared Function DwmSetWindowAttribute(ByVal hwnd As IntPtr, ByVal dwAttribute As ParameterTypes.DWMWINDOWATTRIBUTE, ByRef pvAttribute As Integer, ByVal cbAttribute As Integer) As Integer End Function Public Shared Function ExtendFrame(ByVal hwnd As IntPtr, ByVal margins As ParameterTypes.MARGINS) As Integer Return DwmExtendFrameIntoClientArea(hwnd, margins) End Function Public Shared Function SetWindowAttribute(ByVal hwnd As IntPtr, ByVal attribute As ParameterTypes.DWMWINDOWATTRIBUTE, ByVal parameter As Integer) As Integer Return DwmSetWindowAttribute(hwnd, attribute, parameter, Marshal.SizeOf(Of Integer)()) End Function End Class End Class
Và tiếp theo là source code cho Form1.vb.
Imports System.Net Imports System.Windows.Interop Imports AcrylicBlurWindows11.PInvoke.ParameterTypes Imports AcrylicBlurWindows11.PInvoke.Methods Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim bounds As MARGINS = New MARGINS Dim hwnd As IntPtr = Handle With bounds .cxLeftWidth = 0 .cxRightWidth = 0 .cyTopHeight = Me.Height - 105 'Screen.PrimaryScreen.Bounds.Height - 60 .cyBottomHeight = 0 End With Dim result As Integer = DwmExtendFrameIntoClientArea(hwnd, bounds) Me.BackColor = Color.Black Dim Panel As New Panel With Panel .Size = New Size(Me.Width, Me.Height - 60) .Location = New Point(0, 0) .Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom .BackColor = Color.FromKnownColor(KnownColor.Control) End With 'Panel.Dock = DockStyle.Fill ' Me.Controls.Add(Panel) End Sub Private Sub rdoArcylic_CheckedChanged(sender As Object, e As EventArgs) Handles rdoArcylic.CheckedChanged If rdoArcylic.Checked Then SetWindowAttribute(Me.Handle, DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, 3) End If End Sub Private Sub rdoMica_CheckedChanged(sender As Object, e As EventArgs) Handles rdoMica.CheckedChanged If rdoMica.Checked Then SetWindowAttribute(Me.Handle, DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, 2) End If End Sub Private Sub rdoTabbed_CheckedChanged(sender As Object, e As EventArgs) Handles rdoTabbed.CheckedChanged If rdoTabbed.Checked Then SetWindowAttribute(Me.Handle, DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, 4) End If End Sub Private Sub rdoNone_CheckedChanged(sender As Object, e As EventArgs) Handles rdoNone.CheckedChanged If rdoNone.Checked Then SetWindowAttribute(Me.Handle, DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, 1) End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Button1.Tag.ToString() = "0" Then SetWindowAttribute( Me.Handle, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, 1) Button1.Tag = "1" Button1.Text = "Light" Else SetWindowAttribute( Me.Handle, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, 0) Button1.Tag = "0" Button1.Text = "Dark" End If End Sub End Class
Qua những bước trên các bạn cũng biết cách thực hiển rồi chứ. Mọi người áp dụng vào để làm cho mình một chương trình phát nhạc hoàn chỉnh nhé.
Chúc mọi người thành công với thủ thuật trên.