Click here to Skip to main content
15,796,734 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code obtained from Adding the "Aero Glass" blur to your Windows 10 apps.
I've converted this code to vb.net but not working for me. This is full code
VB
Imports System.Runtime.InteropServices
Imports System.Windows.Interop
Friend Enum AccentState
    ACCENT_DISABLED = 0
    ACCENT_ENABLE_GRADIENT = 1
    ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
    ACCENT_ENABLE_BLURBEHIND = 3
    ACCENT_INVALID_STATE = 4
End Enum

<StructLayout(LayoutKind.Sequential)>
Friend Structure AccentPolicy
    Public AccentState As AccentState
    Public AccentFlags As Integer
    Public GradientColor As Integer
    Public AnimationId As Integer
End Structure

<StructLayout(LayoutKind.Sequential)>
Friend Structure WindowCompositionAttributeData
    Public Attribute As WindowCompositionAttribute
    Public Data As IntPtr
    Public SizeOfData As Integer
End Structure

Friend Enum WindowCompositionAttribute
    ' ...
    WCA_ACCENT_POLICY = 19
    ' ...
End Enum
Public Class BlurBehindTest
    <DllImport("user32.dll")>
    Friend Shared Function SetWindowCompositionAttribute(ByVal hwnd As IntPtr, ByRef data As WindowCompositionAttributeData) As Integer
    End Function
    Friend Sub EnableBlur()
        Dim windowHelper = New Form 'WindowInteropHelper(Me) < This code only working on WPF app, so i've changed to Form

        Dim accent = New AccentPolicy()
        accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND

        Dim accentStructSize = Marshal.SizeOf(accent)

        Dim accentPtr = Marshal.AllocHGlobal(accentStructSize)
        Marshal.StructureToPtr(accent, accentPtr, False)

        Dim data = New WindowCompositionAttributeData()
        data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY
        data.SizeOfData = accentStructSize
        data.Data = accentPtr

        SetWindowCompositionAttribute(windowHelper.Handle, data)

        Marshal.FreeHGlobal(accentPtr)
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        EnableBlur()
    End Sub
End Class


What I have tried:

Can I merge WPF form with Windows form?
If can't, Please share your knowledge about WindowCompositionAttribute

Thanks for advance, Sammy
Posted
Updated 13-Jun-18 16:23pm
v3

1 solution

Quote:

VB.NET
Dim windowHelper = New Form 'WindowInteropHelper(Me) < This code only working on WPF app, so i've changed to Form

By replacing New WindowInteropHelper(Me) with New Form, you've changed the meaning of the code. The blur effect will now be applied to that new form, which is never shown, rather than the current form.

Remove that line, and pass the current form's Handle to the SetWindowCompositionAttribute method:
VB.NET
SetWindowCompositionAttribute(Me.Handle, data)
 
Share this answer
 
Comments
Sammy VSP 8-May-16 11:20am    
You right, but my form opaque not stable. Are you have tested your code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900