Jump to content

Recommended Posts

Posted

In several of my projects, I have continued to use (and improve upon) the wonderful _WinAPI_DwmEnableBlurBehindWindow10 script by @scintilla4evr which paved the way for blur options on modern versions of Windows.

I have completely opened the script up with more options and wanted to share the results back with the community for anyone else to enjoy. I have renamed the function to _WinAPI_DwmEnableBlurBehindWindow11 since it is geared more toward Windows 11 options. It may or may not work with Windows 10. Some of the AccentFlags may need to be adjusted with Windows 10. And I've made it possible to change those values when calling the function.

Maybe somebody will make some sort of continuously changing, random color changing blur increasing and decreasing levels of color transparency with it. 😄

I have included inside the function the contents of a function called _percentageOfHex() that was shared to me by @argumentum that takes a percentage 0-100 and returns the Hexadecimal color code for transparency which gets put together with the main color passed to form a COLORREF that can control color transparency levels. I combined that function into _WinAPI_DwmEnableBlurBehindWindow11 to keep things as simple as possible.

Script with Examples:

Spoiler
#include <GUIConstantsEx.au3>
#include <WinAPIGdi.au3>

DllCall("User32.dll", "bool", "SetProcessDpiAwarenessContext" , "HWND", "DPI_AWARENESS_CONTEXT" -4)

Global $DWMWA_COLOR_NONE = 0xFFFFFFFE

Global $WCA_ACCENT_POLICY = 19
Global $ACCENT_DISABLED = 0
Global $ACCENT_ENABLE_GRADIENT = 1
Global $ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
Global $ACCENT_ENABLE_BLURBEHIND = 3
Global $ACCENT_ENABLE_ACRYLICBLURBEHIND = 4
Global $ACCENT_ENABLE_HOSTBACKDROP = 5
Global $ACCENT_INVALID_STATE = 6

Example()

Func Example()
        ; Create a GUI with various controls.
        Local $hGUI = GUICreate("Example", 600, 400)
        GUISetBkColor(0x000000)
        Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25)

        ; set dark mode for titlebar
        _WinAPI_DwmSetWindowAttribute__($hGUI, 20, 1)

        ; remove window borders
        _WinAPI_DwmSetWindowAttribute__($hGUI, 34, _WinAPI_SwitchColor_mod($DWMWA_COLOR_NONE))
        
        ; set initial DwmEnableBlurBehindWindow
        Local $hRgn = _WinAPI_CreateEllipticRgn(_WinAPI_CreateRectEx(0, 0, 0, 0))
        _WinAPI_DwmEnableBlurBehindWindow($hGUI, 1, 0, $hRgn)
        If $hRgn Then
            _WinAPI_DeleteObject($hRgn)
        EndIf

        _WinAPI_DwmEnableBlurBehindWindow11($hGUI, $ACCENT_ENABLE_ACRYLICBLURBEHIND, 0x0078D4, 60)
        WinSetTitle($hGUI, "", "ACCENT_ENABLE_ACRYLICBLURBEHIND - 60%")

        ; extending the frame allows blur on the titlebar as well
        _WinAPI_DwmExtendFrameIntoClientArea($hGUI, _WinAPI_CreateMargins(-1, -1, -1, -1))

        ; Display the GUI.
        GUISetState(@SW_SHOW, $hGUI)

        Sleep(5000)
        _WinAPI_DwmEnableBlurBehindWindow11($hGUI, $ACCENT_ENABLE_TRANSPARENTGRADIENT, 0x0078D4, 80)
        WinSetTitle($hGUI, "", "ACCENT_ENABLE_TRANSPARENTGRADIENT - 80%")

        Sleep(5000)
        _WinAPI_DwmEnableBlurBehindWindow11($hGUI, $ACCENT_ENABLE_BLURBEHIND)
        WinSetTitle($hGUI, "", "ACCENT_ENABLE_BLURBEHIND")

        Sleep(5000)
        ; disable modern blur method
        _WinAPI_DwmEnableBlurBehindWindow11($hGUI, $ACCENT_DISABLED)
        ; disable original blur method
        _WinAPI_DwmEnableBlurBehindWindow($hGUI, 0, 0)
        ; clear extended frame
        _WinAPI_DwmExtendFrameIntoClientArea($hGUI, _WinAPI_CreateMargins(0, 0, 0, 0))
        WinSetTitle($hGUI, "", "ACCENT_DISABLED")

        ; Loop until the user exits.
        While 1
                Switch GUIGetMsg()
                        Case $GUI_EVENT_CLOSE, $idOK
                                ExitLoop

                EndSwitch
        WEnd

        ; Delete the previous GUI and all controls.
        GUIDelete($hGUI)
EndFunc   ;==>Example

Func _WinAPI_DwmSetWindowAttribute__($hwnd, $attribute = 34, $value = 0x00FF00, $valLen = 4)
    Local $aCall = DllCall('dwmapi.dll', 'long', 'DwmSetWindowAttribute', 'hwnd', $hWnd, 'dword', $attribute, 'dword*', $value, 'dword', $valLen)
    If @error Then Return SetError(@error, @extended, 0)
    If $aCall[0] Then Return SetError(10, $aCall[0], 0)
    Return 1
EndFunc   ;==>_WinAPI_DwmSetWindowAttribute__

Func _WinAPI_SwitchColor_mod($iColor)
    If $iColor = $DWMWA_COLOR_NONE Then Return $iColor
    If $iColor = -1 Then Return $iColor
    Return BitOR(BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0x0000FF), -16), BitShift(BitAND($iColor, 0xFF0000), 16))
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _WinAPI_DwmEnableBlurBehindWindow11
; Description ...: Enables Aero-like blurred background in Windows 11.
; Syntax ........: _WinAPI_DwmEnableBlurBehindWindow11($hWnd[, $AccentState, $iBlurColor, $iBlurPercent, $AccentFlags, $AnimationId])
; Parameters ....: $hWnd                - Window handle.
;                  $AccentState         - [optional] Enable or disable the blur effect.
;                  $iBlurColor          - [optional] Sets GradientColor
;                  $iBlurPercent        - [optional] Sets blending color transparency percentage
;                  $AccentFlags         - [optional] Sets AccentFlags value
;                  $AnimationId         - [optional] Sets AnimationId value
; Return values .: 1 on success, 0 otherwise. Call _WinAPI_GetLastError on failure for more information.
; Author ........: scintilla4evr
; Modified ......: WildByDesign - added $AccentState, $iBlurColor, $iBlurPercent, $AccentFlags and $AnimationId
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
;    ACCENT_DISABLED = 0
;    ACCENT_ENABLE_GRADIENT = 1
;    ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
;    ACCENT_ENABLE_BLURBEHIND = 3
;    ACCENT_ENABLE_ACRYLICBLURBEHIND = 4
;    ACCENT_ENABLE_HOSTBACKDROP = 5
;    ACCENT_INVALID_STATE = 6
; ===============================================================================================================================
Func _WinAPI_DwmEnableBlurBehindWindow11($hWnd, $AccentState = $ACCENT_ENABLE_ACRYLICBLURBEHIND, $iBlurColor = "", $iBlurPercent = 50, $AccentFlags = 0, $AnimationId = 0)
    Local $tAccentPolicy = DllStructCreate("int AccentState; int AccentFlags; int GradientColor; int AnimationId")
    Local $tAttrData = DllStructCreate("dword Attribute; ptr DataBuffer; ulong Size")
    $tAccentPolicy.AccentState = $AccentState
    If $AccentState = $ACCENT_ENABLE_TRANSPARENTGRADIENT And $AccentFlags = 0 Then $AccentFlags = 2
    $tAccentPolicy.AccentFlags = $AccentFlags
    Local $iVal = Int($iBlurPercent > 99 ? 100 : ($iBlurPercent < 1 ? 0 : $iBlurPercent)) ; no more than 100% or less than 0%
    Local $sTransparencyHex = Hex(Ceiling(($iVal * 100) * (2.55 * 100) / (100 * 100)), 2)
    If $iBlurColor <> "" Then $tAccentPolicy.GradientColor = '0x' & $sTransparencyHex & Hex(_WinAPI_SwitchColor($iBlurColor), 6)
    $tAccentPolicy.AnimationId = $AnimationId
    $tAttrData.Attribute = $WCA_ACCENT_POLICY
    $tAttrData.DataBuffer = DllStructGetPtr($tAccentPolicy)
    $tAttrData.Size = DllStructGetSize($tAccentPolicy)

    Local $aResult = DllCall("user32.dll", "bool", "SetWindowCompositionAttribute", "hwnd", $hWnd, "ptr", DllStructGetPtr($tAttrData))
    If @error Then Return SetError(@error, @extended, 0)

    Return $aResult[0]
EndFunc

 

 

Posted (edited)
Spoiler
#include <GUIConstantsEx.au3>
#include <WinAPIGdi.au3>

DllCall("User32.dll", "bool", "SetProcessDpiAwarenessContext", "HWND", "DPI_AWARENESS_CONTEXT" - 4)

Global $DWMWA_COLOR_NONE = 0xFFFFFFFE ;;; https://www.autoitscript.com/forum/topic/213090-_winapi_dwmenableblurbehindwindow-in-windows-11/

Global $WCA_ACCENT_POLICY = 19
Global $ACCENT_DISABLED = 0
Global $ACCENT_ENABLE_GRADIENT = 1
Global $ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
Global $ACCENT_ENABLE_BLURBEHIND = 3
Global $ACCENT_ENABLE_ACRYLICBLURBEHIND = 4
Global $ACCENT_ENABLE_HOSTBACKDROP = 5
Global $ACCENT_INVALID_STATE = 6

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $iInit = 0, $iLeft = 10, $iTop = 10, $hGUI = GUICreate("Example: ACCENT DISABLED", 600, 400)
    GUISetBkColor(0x000000)
    Local $idRadio1 = GUICtrlCreateButton("ACCENT ENABLE &ACRYLICBLURBEHIND - 60%", $iLeft, CtrlAddNextPos($iTop))
    Local $idRadio2 = GUICtrlCreateButton("ACCENT ENABLE &TRANSPARENTGRADIENT - 80%", $iLeft, CtrlAddNextPos($iTop))
    Local $idRadio3 = GUICtrlCreateButton("ACCENT ENABLE &BLURBEHIND", $iLeft, CtrlAddNextPos($iTop))
    Local $idRadio4 = GUICtrlCreateButton("ACCENT &DISABLED", $iLeft, CtrlAddNextPos($iTop))
;~  GUICtrlSetState($idRadio4, $GUI_CHECKED) ; ..had to change to buttons. My main PC is heavily modified and Radio worked just fine,
    Local $idOK = GUICtrlCreateButton("&OK", 310, 370, 85, 25) ;  but not in a "regular" PC  =/
    GUICtrlSetState($idOK, $GUI_FOCUS)

    ; set dark mode for titlebar
    _WinAPI_DwmSetWindowAttribute__($hGUI, 20, 1)

    ; remove window borders
    _WinAPI_DwmSetWindowAttribute__($hGUI, 34, _WinAPI_SwitchColor_mod($DWMWA_COLOR_NONE))

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)
    WinSetOnTop($hGUI, "", 1)
;~  Send("{ALT}") ; ..to show the accelerator keys

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idOK
                ExitLoop

            Case $idRadio1
                If Not $iInit Then InitTheGuiBlurBehind($iInit, $hGUI)
                _WinAPI_DwmEnableBlurBehindWindow11($hGUI, $ACCENT_ENABLE_ACRYLICBLURBEHIND, 0x0078D4, 60)
                WinSetTitleAndRefresh($hGUI, "", "Example: ACCENT ENABLE ACRYLICBLURBEHIND - 60%")

            Case $idRadio2
                If Not $iInit Then InitTheGuiBlurBehind($iInit, $hGUI)
                _WinAPI_DwmEnableBlurBehindWindow11($hGUI, $ACCENT_ENABLE_TRANSPARENTGRADIENT, 0x0078D4, 80)
                WinSetTitleAndRefresh($hGUI, "", "Example: ACCENT ENABLE TRANSPARENTGRADIENT - 80%")

            Case $idRadio3
                If Not $iInit Then InitTheGuiBlurBehind($iInit, $hGUI)
                _WinAPI_DwmEnableBlurBehindWindow11($hGUI, $ACCENT_ENABLE_BLURBEHIND)
                WinSetTitleAndRefresh($hGUI, "", "Example: ACCENT ENABLE BLURBEHIND")

            Case $idRadio4
                If $iInit Then
                    ; disable modern blur method
                    _WinAPI_DwmEnableBlurBehindWindow11($hGUI, $ACCENT_DISABLED)
                    ; disable original blur method
                    _WinAPI_DwmEnableBlurBehindWindow($hGUI, 0, 0)
                    ; clear extended frame
                    _WinAPI_DwmExtendFrameIntoClientArea($hGUI, _WinAPI_CreateMargins(0, 0, 0, 0))
                    $iInit = 0
                EndIf
                WinSetTitleAndRefresh($hGUI, "", "Example: ACCENT DISABLED")

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func WinSetTitleAndRefresh($sTitle, $sText, $sNewTitle, $iCtrl = 0)
    WinSetTitle($sTitle, $sText, $sNewTitle)
    WinActivate("[CLASS:Shell_TrayWnd;]") ; enable or disable this based on Win11 Build number,
    WinActivate($sTitle) ;                     or something. Some times is needed, not always.
    If $iCtrl Then GUICtrlSetState($iCtrl, $GUI_FOCUS)
EndFunc   ;==>WinSetTitleAndRefresh

Func InitTheGuiBlurBehind(ByRef $iInit, $hGUI)
    ; set initial DwmEnableBlurBehindWindow
    Local $hRgn = _WinAPI_CreateEllipticRgn(_WinAPI_CreateRectEx(0, 0, 0, 0))
    _WinAPI_DwmEnableBlurBehindWindow($hGUI, 1, 0, $hRgn)
    If $hRgn Then
        _WinAPI_DeleteObject($hRgn)
    EndIf

    ; extending the frame allows blur on the titlebar as well
    _WinAPI_DwmExtendFrameIntoClientArea($hGUI, _WinAPI_CreateMargins(-1, -1, -1, -1))

    $iInit = 1
EndFunc   ;==>InitTheGuiBlurBehind

Func CtrlAddNextPos(ByRef $iPos, $iAdd = 30)
    Local $iSaved = $iPos
    $iPos += $iAdd
    Return $iSaved
EndFunc   ;==>CtrlAddNextPos

Func _WinAPI_DwmSetWindowAttribute__($hwnd, $attribute = 34, $value = 0x00FF00, $valLen = 4)
    Local $aCall = DllCall('dwmapi.dll', 'long', 'DwmSetWindowAttribute', 'hwnd', $hwnd, 'dword', $attribute, 'dword*', $value, 'dword', $valLen)
    If @error Then Return SetError(@error, @extended, 0)
    If $aCall[0] Then Return SetError(10, $aCall[0], 0)
    Return 1
EndFunc   ;==>_WinAPI_DwmSetWindowAttribute__

Func _WinAPI_SwitchColor_mod($iColor)
    If $iColor = $DWMWA_COLOR_NONE Then Return $iColor
    If $iColor = -1 Then Return $iColor
    Return BitOR(BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0x0000FF), -16), BitShift(BitAND($iColor, 0xFF0000), 16))
EndFunc   ;==>_WinAPI_SwitchColor_mod

; #FUNCTION# ====================================================================================================================
; Name ..........: _WinAPI_DwmEnableBlurBehindWindow11
; Description ...: Enables Aero-like blurred background in Windows 11.
; Syntax ........: _WinAPI_DwmEnableBlurBehindWindow11($hWnd[, $AccentState, $iBlurColor, $iBlurPercent, $AccentFlags, $AnimationId])
; Parameters ....: $hWnd                - Window handle.
;                  $AccentState         - [optional] Enable or disable the blur effect.
;                  $iBlurColor          - [optional] Sets GradientColor
;                  $iBlurPercent        - [optional] Sets blending color transparency percentage
;                  $AccentFlags         - [optional] Sets AccentFlags value
;                  $AnimationId         - [optional] Sets AnimationId value
; Return values .: 1 on success, 0 otherwise. Call _WinAPI_GetLastError on failure for more information.
; Author ........: scintilla4evr
; Modified ......: WildByDesign - added $AccentState, $iBlurColor, $iBlurPercent, $AccentFlags and $AnimationId
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
;    ACCENT_DISABLED = 0
;    ACCENT_ENABLE_GRADIENT = 1
;    ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
;    ACCENT_ENABLE_BLURBEHIND = 3
;    ACCENT_ENABLE_ACRYLICBLURBEHIND = 4
;    ACCENT_ENABLE_HOSTBACKDROP = 5
;    ACCENT_INVALID_STATE = 6
; ===============================================================================================================================
Func _WinAPI_DwmEnableBlurBehindWindow11($hwnd, $AccentState = $ACCENT_ENABLE_ACRYLICBLURBEHIND, $iBlurColor = "", $iBlurPercent = 50, $AccentFlags = 0, $AnimationId = 0)
    Local $tAccentPolicy = DllStructCreate("int AccentState; int AccentFlags; int GradientColor; int AnimationId")
    Local $tAttrData = DllStructCreate("dword Attribute; ptr DataBuffer; ulong Size")
    $tAccentPolicy.AccentState = $AccentState
    If $AccentState = $ACCENT_ENABLE_TRANSPARENTGRADIENT And $AccentFlags = 0 Then $AccentFlags = 2
    $tAccentPolicy.AccentFlags = $AccentFlags
    Local $iVal = Int($iBlurPercent > 99 ? 100 : ($iBlurPercent < 1 ? 0 : $iBlurPercent)) ; no more than 100% or less than 0%
    Local $sTransparencyHex = Hex(Ceiling(($iVal * 100) * (2.55 * 100) / (100 * 100)), 2)
    If $iBlurColor <> "" Then $tAccentPolicy.GradientColor = '0x' & $sTransparencyHex & Hex(_WinAPI_SwitchColor($iBlurColor), 6)
    $tAccentPolicy.AnimationId = $AnimationId
    $tAttrData.Attribute = $WCA_ACCENT_POLICY
    $tAttrData.DataBuffer = DllStructGetPtr($tAccentPolicy)
    $tAttrData.Size = DllStructGetSize($tAccentPolicy)

    Local $aResult = DllCall("user32.dll", "bool", "SetWindowCompositionAttribute", "hwnd", $hwnd, "ptr", DllStructGetPtr($tAttrData))
    If @error Then Return SetError(@error, @extended, 0)

    Return $aResult[0]
EndFunc   ;==>_WinAPI_DwmEnableBlurBehindWindow11

...the 5 seconds sampling was, insufficient :lol: 

Liked it. Thanks for sharing :) 

Edited by argumentum
better

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (edited)
2 hours ago, argumentum said:

Liked it. Thanks for sharing :)

You're welcome. And thank you for the sharing your example as well with the buttons. Your example makes more sense for being able to play around with the example. I've never been very good with examples.

I was working on another example earlier today using a slider to automatically change the color transparency in real-time and it's working well. So I think what I will do is combine my slider example with your example and should make for a good example in the end.

Also, I was thinking of adding an input box (likely a color picker too) to change color. And with that, maybe a "Random Color" button as well. :)

Edited by WildByDesign

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...