如何在Windows 10中使表单具有模糊效果 [英] How to make form have blur effect in windows 10

查看:192
本文介绍了如何在Windows 10中使表单具有模糊效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码从添加Aero Glass模糊到你的Windows 10应用程序

我已将此代码转换为vb.net但不适用于我。这是完整的代码

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

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





我的尝试:



我可以将WPF表单与Windows表单合并吗?

如果不能,请分享您对WindowCompositionAttribute的了解


感谢您提前,Sammy



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

推荐答案

引用:




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



替换 New WindowInteropHelper(Me )使用新表单,您已经更改了代码的含义。模糊效果现在将应用于从未显示的新表单,而不是当前表单。



删除该行,并传递当前表单的处理 SetWindowCompositionAttribute 方法:


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:

SetWindowCompositionAttribute(Me.Handle, data)


这篇关于如何在Windows 10中使表单具有模糊效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆