[Function] Windows 10 Acrylic / Glass effect

Post your working scripts, libraries and tools for AHK v1.1 and older
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

[Function] Windows 10 Acrylic / Glass effect

14 Apr 2019, 12:09

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.
Last edited by robodesign on 03 May 2019, 04:37, edited 2 times in total.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: [Function] Windows 10 Acrylic / Glass effect

15 Apr 2019, 12:51

This is very nice.

How do you change the name of the GUI?
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Function] Windows 10 Acrylic / Glass effect

15 Apr 2019, 16:14

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.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: [Function] Windows 10 Acrylic / Glass effect

16 Apr 2019, 10:39

:thumbup:
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
tqphan
Posts: 26
Joined: 04 Mar 2019, 14:44

Re: [Function] Windows 10 Acrylic / Glass effect

14 Jun 2020, 23:56

When I try to move the acrylic window, there's a lot of lag. Any idea why?
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Function] Windows 10 Acrylic / Glass effect

15 Jun 2020, 02:36

I noticed this problem as well recently.... I don't know why,.... I suppose Microsoft broke something ^_^
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: [Function] Windows 10 Acrylic / Glass effect

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
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Function] Windows 10 Acrylic / Glass effect

18 Jun 2020, 16:25

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.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: [Function] Windows 10 Acrylic / Glass effect

18 Jun 2020, 17:06

@robodesign no lag on that one for me.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Function] Windows 10 Acrylic / Glass effect

27 Feb 2021, 21:33

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.
o000u
Posts: 5
Joined: 09 May 2023, 20:35

Re: [Function] Windows 10 Acrylic / Glass effect

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?
o000u
Posts: 5
Joined: 09 May 2023, 20:35

Re: [Function] Windows 10 Acrylic / Glass effect

20 Jun 2023, 07:08

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!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 233 guests