Page 1 of 1

[Function] Windows 10 Acrylic / Glass effect

Posted: 14 Apr 2019, 12:09
by robodesign
Hello, earthlings !

I made a new function that applies to any window the Windows 10 fancy acrylic / glassy effect. It is based on AHK_TaskBar_SetAttr by jNizM.

Code: Select all

#Persistent

CreateGui()
Gui, thisGuia: Show
Return

CreateGui() {

    thisFntSize := 25
    bgrColor := "000022"
    txtColor := "ffeedd"
    Gui, thisGuia: -DPIScale +Owner +hwndhGui
    Gui, thisGuia: Margin, % thisFntSize*2, % thisFntSize*2
    Gui, thisGuia: Color, c%bgrColor%
    Gui, thisGuia: Font, s%thisFntSize% Q5, Arial
    Gui, thisGuia: Add, Text, c%txtColor% , This is a demo. Enjoy.
    WinSet, AlwaysOnTop, On, ahk_id %hGui%
    SetAcrylicGlassEffect(bgrColor, 125, hGui)
}

ConvertToBGRfromRGB(RGB) { ; Get numeric BGR value from numeric RGB value or HTML color name
  ; HEX values
  BGR := SubStr(RGB, -1, 2) SubStr(RGB, 1, 4) 
  Return BGR 
}

SetAcrylicGlassEffect(thisColor, thisAlpha, hWindow) {
  ; based on https://github.com/jNizM/AHK_TaskBar_SetAttr/blob/master/scr/TaskBar_SetAttr.ahk
  ; by jNizM
    initialAlpha := thisAlpha
    If (thisAlpha<16)
       thisAlpha := 16
    Else If (thisAlpha>245)
       thisAlpha := 245


    thisColor := ConvertToBGRfromRGB(thisColor)
    thisAlpha := Format("{1:#x}", thisAlpha)
    gradient_color := thisAlpha . thisColor

    Static init, accent_state := 4, ver := DllCall("GetVersion") & 0xff < 10
    Static pad := A_PtrSize = 8 ? 4 : 0, WCA_ACCENT_POLICY := 19
    accent_size := VarSetCapacity(ACCENT_POLICY, 16, 0)
    NumPut(accent_state, ACCENT_POLICY, 0, "int")

    If (RegExMatch(gradient_color, "0x[[:xdigit:]]{8}"))
       NumPut(gradient_color, ACCENT_POLICY, 8, "int")

    VarSetCapacity(WINCOMPATTRDATA, 4 + pad + A_PtrSize + 4 + pad, 0)
    && NumPut(WCA_ACCENT_POLICY, WINCOMPATTRDATA, 0, "int")
    && NumPut(&ACCENT_POLICY, WINCOMPATTRDATA, 4 + pad, "ptr")
    && NumPut(accent_size, WINCOMPATTRDATA, 4 + pad + A_PtrSize, "uint")
    If !(DllCall("user32\SetWindowCompositionAttribute", "ptr", hWindow, "ptr", &WINCOMPATTRDATA))
       Return 0 
    thisOpacity := (initialAlpha<16) ? 60 + initialAlpha*9 : 250
    WinSet, Transparent, %thisOpacity%, ahk_id %hWindow%
    Return 1
}

thisGuiaGuiClose:
ExitApp
Return 

thisGuiaGuiEscape:
ExitApp
Return 
I created this function initially for KeyPress OSD, but then, I thought to share it with you as well.

P.S. This works *only* on Windows 10 .

Best regards, Marius.

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 15 Apr 2019, 12:51
by DataLife
This is very nice.

How do you change the name of the GUI?

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 15 Apr 2019, 16:14
by robodesign
The function is applied to a window / GUI handle.

The GUI name is thisGuia, but that's not relevant. The GUI is created with a HWND identifier attached to it - hGUI...

The function is called with the following parameters : the color (the color in HEX of the tint), alpha (opacity from 0 to 255) and the HWND identifier of the window / GUI you want the effect applied to.

I hope these explanations are clear enough.

Best regards, Marius.

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 16 Apr 2019, 10:39
by jNizM
:thumbup:

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 02 May 2019, 11:32
by elModo7
Sleek design. :clap:

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 14 Jun 2020, 23:56
by tqphan
When I try to move the acrylic window, there's a lot of lag. Any idea why?

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 15 Jun 2020, 02:36
by robodesign
I noticed this problem as well recently.... I don't know why,.... I suppose Microsoft broke something ^_^

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 18 Jun 2020, 15:39
by SirSocks
This is great! But I also experience lag on click and drag of the gui.
EDIT: Here's something similar..... https://www.autohotkey.com/boards/viewtopic.php?t=18823

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 18 Jun 2020, 16:25
by robodesign
SirSocks wrote:
18 Jun 2020, 15:39
This is great! But I also experience lag on click and drag of the gui.
EDIT: Here's something similar..... https://www.autohotkey.com/boards/viewtopic.php?t=18823
Does this cause lags as well?

Thank you.

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 18 Jun 2020, 17:06
by SirSocks
@robodesign no lag on that one for me.

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 27 Feb 2021, 21:33
by lexikos
I had some issues with the example:
  • Without Font option Q3 (or TransColor), the text was invisible.
  • AlwaysOnTop and Transparent were not applied because the GUI is hidden and DetectHiddenWindows is Off.
Using WinSet TransColor normally makes the keyed colour fully transparent, but in combination with this, it instead causes the keyed colour to be replaced with acrylic. I've noticed one very useful side-effect of TransColor is that it causes controls of any other colour to be fully opaque. This is good because normally controls rendered with GDI don't work very well, because parts of the controls have zero alpha. The end result is that one can use normal controls on a GUI with an acrylic gradient background. The only caveat I've noticed so far is that the acrylic part becomes "invisible" to the mouse, and can't be clicked.

This could be used in combination with the WS_EX_TRANSPARENT ExStyle (+E0x20) to make a GUI which is completely unclickable but displays nicely and can still be operated with the keyboard if it has focus. Alternatively, clever use of a second GUI with a transparency of 1 could make the background appear to be clickable again.

My modifications are marked with "Lexikos":

Code: Select all

#Persistent

CreateGui()
Gui, thisGuia: Show
Return

CreateGui() {

    thisFntSize := 25
    bgrColor := "000022"
    txtColor := "ffeedd"
    Gui, thisGuia: -DPIScale +Owner +hwndhGui
    Gui, thisGuia: Margin, % thisFntSize*2, % thisFntSize*2
    Gui, thisGuia: Color, c%bgrColor%
    Gui, thisGuia: Font, s%thisFntSize% Q5, Arial
    Gui, thisGuia: Add, Text, c%txtColor% , This is a demo. Enjoy.
    ; Lexikos: Add an Edit to demonstrate TransColor.
    Gui, thisGuia: Add, Edit,, This is a demo. Enjoy.
    ; Lexikos: Enable DHW so WinSet will actually work.
    dhw := A_DetectHiddenWindows
    DetectHiddenWindows On  ; </Lexikos>
    WinSet, AlwaysOnTop, On, ahk_id %hGui%
    SetAcrylicGlassEffect(bgrColor, 125, hGui)
    DetectHiddenWindows % dhw  ; Lexikos
}

ConvertToBGRfromRGB(RGB) { ; Get numeric BGR value from numeric RGB value or HTML color name
  ; HEX values
  BGR := SubStr(RGB, -1, 2) SubStr(RGB, 1, 4) 
  Return BGR 
}

SetAcrylicGlassEffect(thisColor, thisAlpha, hWindow) {
  ; based on https://github.com/jNizM/AHK_TaskBar_SetAttr/blob/master/scr/TaskBar_SetAttr.ahk
  ; by jNizM
    initialAlpha := thisAlpha
    If (thisAlpha<16)
       thisAlpha := 16
    Else If (thisAlpha>245)
       thisAlpha := 245


    ; Lexikos: Keep original value of thisAlpha for use below.
    gradient_color := Format("{1:#x}{}", thisAlpha, ConvertToBGRfromRGB(thisColor))

    Static init, accent_state := 4, ver := DllCall("GetVersion") & 0xff < 10
    Static pad := A_PtrSize = 8 ? 4 : 0, WCA_ACCENT_POLICY := 19
    accent_size := VarSetCapacity(ACCENT_POLICY, 16, 0)
    NumPut(accent_state, ACCENT_POLICY, 0, "int")

    If (RegExMatch(gradient_color, "0x[[:xdigit:]]{8}"))
       NumPut(gradient_color, ACCENT_POLICY, 8, "int")

    VarSetCapacity(WINCOMPATTRDATA, 4 + pad + A_PtrSize + 4 + pad, 0)
    && NumPut(WCA_ACCENT_POLICY, WINCOMPATTRDATA, 0, "int")
    && NumPut(&ACCENT_POLICY, WINCOMPATTRDATA, 4 + pad, "ptr")
    && NumPut(accent_size, WINCOMPATTRDATA, 4 + pad + A_PtrSize, "uint")
    If !(DllCall("user32\SetWindowCompositionAttribute", "ptr", hWindow, "ptr", &WINCOMPATTRDATA))
       Return 0 
    thisOpacity := (initialAlpha<16) ? 60 + initialAlpha*9 : 250
    ; Lexikos: Use TransColor instead of Transparent.
    WinSet, TransColor, %thisColor% %thisOpacity%, ahk_id %hWindow%
    Return 1
}

thisGuiaGuiClose:
ExitApp
Return 

thisGuiaGuiEscape:
ExitApp
Return
Tested on Windows 10.0.19041.

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 20 Jun 2023, 00:40
by o000u
@robodesign
@lexikos
WOW!!I have to say, this is really cool! It makes the window more beautiful and modern than ever before. Thank you very much for sharing. You guys are the best!!

In addition, my question is:How to fillet the acrylic window? Even with borders and shadows?

Re: [Function] Windows 10 Acrylic / Glass effect

Posted: 20 Jun 2023, 07:08
by o000u
o000u wrote:
20 Jun 2023, 00:40
@robodesign
@lexikos
WOW!!I have to say, this is really cool! It makes the window more beautiful and modern than ever before. Thank you very much for sharing. You guys are the best!!

In addition, my question is:How to fillet the acrylic window? Even with borders and shadows?
Sorry, I forgot to explain.I mean:Is there any way to set the acrylic window (- Caption) to a rounded rectangle?

@robodesign
@lexikos

With best wishes!