WinFade (multiprocess)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

WinFade (multiprocess)

07 Oct 2017, 13:32

My ideas for this script aren't done, but I don't think Ill be making any changes at this time, so I thought I'd post it.

This script will fade windows that are [not maximized/minimized, under the mouse, active or system windows].
What may be unique to this fade script is that it uses sendmessage for interprocess communication to make the fade effect multithreaded/multiprocess
place both files in same directory and run winfade, it will launch 2 copies of _fade_effect, 1 for fadein, 1 for fadeout.

Image

winfade.ahk

Code: Select all

#NoEnv
;#NoTrayIcon
#SingleInstance, force
#Persistent
CoordMode, Mouse, Screen
SetWinDelay, -1
SetWorkingDir, %A_ScriptDir%
SetTitleMatchMode, 2
DetectHiddenWindows, On

OnExit("Shutdown", -1)

exclusions := {}

Startup()
Main_Loop()
return

IsExcluded(win) {
    global exclusions
    if(exclusions[win])
        return true
    if(DllCall("GetParent", UInt, win))
        return true
    WinGetTitle, wtitle, ahk_id %win%
    if(!wtitle)
        return true
    WinGet, mm, MinMax, ahk_id %win%
    if(mm <> 0)
        return true
    return false
}

UpdateWindows() {
    global AllWindows

    newWindows := {}
    DetectHiddenWindows, Off
    GroupAdd, allwins
    WinGet, WINDS, List, ahk_group allwins
    DetectHiddenWindows, On
    Loop, %WINDS% {
        cWind := WINDS%A_Index%
        if(IsExcluded(cWind))
            Continue
        newWindows[cWind] := 1
    }

    AllWindows := newWindows
}

IsPopup(win) {
    WinGet, ss, Style, ahk_id %win%
    winGet, sx, ExStyle, ahk_id %win%

    if(ss & 0x80000000 and sx & 0x00000080)
        return true
    return false
}

Startup() {
    global exclusions, _FadeIn_pid, _FadeOut_pid, fadein_id, fadeout_id, AllWindows
    bad_classes := Array("Progman", "WorkerW", "Shell_SecondaryTrayWnd", "Shell_TrayWnd", "ClassicShell.CMenuContainer", "TaskManagerWindow", "Windows.UI.Core.CoreWindow")
    for i, v in bad_classes {
        WinGet, wid, ID, ahk_class %v%
        exclusions[wid] := 1
    }
    exclusions[DllCall("GetDesktopWindow")] := 1

    Run, % ".\_fade_effect.ahk",, Hide, _FadeIn_pid
    WinWait, % "_fade_effect.ahk ahk_pid " . _FadeIn_pid
    fadein_id := WinExist("_fade_effect.ahk ahk_pid " . _FadeIn_pid)

    Run, % ".\_fade_effect.ahk",, Hide, _FadeOut_pid
    WinWait, % "_fade_effect.ahk ahk_pid " . _FadeOut_pid
    fadeout_id := WinExist("_fade_effect.ahk ahk_pid " . _FadeOut_pid)

    UpdateWindows()
}

Shutdown() {
    global _FadeOut_pid, _FadeIn_pid, AllWindows
    
    UpdateWindows()
    For win, v in AllWindows
        Sync_FadeIn(win)
    Sleep, 250

    Process, Close, %_FadeIn_pid%
    Process, Close, %_FadeOut_pid%
}



Main_Loop() {
    prior_active := 0, prior_mouseover := 0
    trigger_event := true

    Loop {
        Sleep, 50

        trigger_event := !trigger_event

        if(trigger_event) {
            ProcessWindowUnderMouse(ProcessActiveWindow())
        } else {
            UpdateWindows()
        }
    }
}

ProcessActiveWindow() {
    static prev_active := WinActive("A")
    current_active := WinActive("A")

    if(current_active != prev_active) {
        FadeIn(current_active)
        FadeOut(prev_active)

        prev_active := current_active
    }

    return current_active
}

ProcessWindowUnderMouse(exclude) {
    static prev_mwin

    MouseGetPos,,, current_mwin,,
    ; if(IsExcluded(current_mwin))
    ;     return
    if(current_mwin != prev_mwin) {
        if(!IsPopup(current_mwin)) {
            FadeIn(current_mwin)
        }
        if(prev_mwin != exclude) {
            FadeOut(prev_mwin)
        }

        prev_mwin := current_mwin
    }
}

Sync_FadeIn(win) {
    global fadein_id
    SendMessage, 0x9005, -1, win,, ahk_id %fadein_id%
}

FadeIn(win) {
    global fadein_id
    PostMessage, 0x9005, -1, win,, ahk_id %fadein_id%
}

FadeOut(win) {
    global fadeout_id, AllWindows

    if(!AllWindows[win] or IsExcluded(win))
        return

    PostMessage, 0x9005, 1, win,, ahk_id %fadeout_id%
}
_fade_effect.ahk

Code: Select all

#NoEnv
#NoTrayIcon
#SingleInstance, off
#Persistent
Thread, NoTimers
SetWinDelay, -1
SetWorkingDir %A_ScriptDir%

OnMessage(0x9005, "FadeEffect")
Return

FadeEffect(wParam, lParam, Msg) {
    window_id := WinExist("ahk_id " . lParam)
    TransitionTrans(window_id, (wParam == -1 ? 255 : 150))
    return 0x9005
}

TransitionTrans(window, needed) {
    WinGet, cTrans, Transparent, ahk_id %window%
    if(cTrans == needed)
        return
    cTrans := !cTrans ? 255 : cTrans

    steps := 5
    step_size := (needed - cTrans) / steps
    Loop, %steps% {
        cTrans += step_size
        WinSet, Transparent, %cTrans%, ahk_id %window%
        ;WinGet, cTrans, Transparent, ahk_id %window%
        Sleep, % (150 / steps)
    }
    WinSet, Redraw,, ahk_id %window%
    ;DllCall("User32\ShowWindow", UInt, window, Int, 4, Int)

    if(needed == 255) {
        WinSet, Transparent, Off, ahk_id %window%
    }
}
Last edited by KuroiLight on 07 Oct 2017, 20:23, edited 3 times in total.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WinFade (multiprocess)

07 Oct 2017, 17:36

The effect looks nice, but the clean-up didn't work properly, left me with a few semi-transparent windows. No harm though, since they were just some explorer windows I opened for testing. The start menu got transparent too :lol:

Cheers :wave:

Edit: I tried the bottom-most script.
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: WinFade (multiprocess)

07 Oct 2017, 19:16

It should cleanup properly, are you using a custom shell or start menu :?: ?
The following should exclude minimized windows and the default startmenu now, and make the transitions less flickery.
I can't figure out why windows sometimes go completely invisible, Might be the desktop actually moving up the layer stack because of the transparency, but it seems unlikely. it's caused by transparency some how getting to zero
seem to have fixed it, code below in OP updated to reflect changes, EDIT: and fixed the cleanup issue
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: WinFade (multiprocess)

15 Nov 2023, 08:34

Is there any way to pass the target transparency value to the _fade_effect.ahk along with the message 0x9005? That way you could dynamically choose the level of transparency for different 0x9005 messages posted ?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: cleancode and 69 guests