Jump to content

Toggle semi-transparency of window under the mouse


PhilHibbs
 Share

Recommended Posts

Just a bit of fun really - this script runs in the background and hooks the ` key to a function that switches the transparency of the window that is under the mouse. [Esc] aborts the script, or you can use the notification area icon to exit.

#include <WinAPI.au3>

Global $Struct = DllStructCreate($tagPoint)

Opt('MustDeclareVars', 1)
HotKeySet("`","ToggleTrans")
HotKeySet("{Esc}","Escape")

While True
    Sleep(10)
WEnd

Func Pos()
    DllStructSetData($Struct, "x", MouseGetPos(0))
    DllStructSetData($Struct, "y", MouseGetPos(1))
EndFunc   ;==>Pos

Func Escape()
    Exit
EndFunc

Func ToggleTrans()
    Local $hWnd, $hLast, $trans
    Pos()
    $hWnd = _WinAPI_WindowFromPoint($Struct)
    do
        $hLast = $hWnd
        $hWnd = _WinAPI_GetParent($hLast)
    Until $hWnd = 0
    $hWnd = $hLast
    $trans = WinGetTrans($hWnd)
    If $trans < 255 And $trans >= 0 Then
        WinSetTrans($hWnd, "", 255) ; Make fully opaque
    Else
        WinSetTrans($hWnd, "", 170) ; Make window semi-transparent (luminosity scale is logarithmic)
    EndIf
EndFunc

Func WinGetTrans($sTitle, $sText = "")
    Local $hWnd = WinGetHandle($sTitle, $sText)
    If Not $hWnd Then Return -1
    Local $val = DllStructCreate("int")
    Local $aRet = DllCall("user32.dll", "int", "GetLayeredWindowAttributes", "hwnd", $hWnd, "ulong_ptr", 0, "int_ptr", DllStructGetPtr($val), "ulong_ptr", 0)
    If @error Or Not $aRet[0] Then Return -1
    Return DllStructGetData($val, 1)
EndFunc

Shame it doesn't work on Command Prompt windows. Also the transparency is not inherited by child windows such as menus or dialogs.

Edited by PhilHibbs
Link to comment
Share on other sites

Just a bit of fun really - this script runs in the background and hooks the ` key to a function that switches the transparency of the window that is under the mouse. [Esc] aborts the script, or you can use the notification area icon to exit.

It doesn't work for me. I can't see any GUI/window.

Few years ago I tested (I think) this subject here => http://www.autoitscript.com/forum/index.php?showtopic=27605

Below updated/working script:

#include <GUIConstants.au3>
#include <EditConstants.au3>
Const $guiW = 400
Const $guiH = 200
$solid = 1
$guiTitle = "Transparency & cursor hovering"
$myGUI = GUICreate($guiTitle, $guiW, $guiH)
$label = GUICtrlCreateLabel("How to avoid flickering when mouse is over the GUI?", 80, 20, 300, 17)
If @OSTYPE = "WIN32_WINDOWS" Then $xponly = GUICtrlCreateLabel("This works only in Win XP/2000.", 120, 40, 200, 17)
$combo = GUICtrlCreateCombo("This is resolved", 125, 60, 150, 100)
$comboItem = GUICtrlSetData(-1, "by GaryFrost.")
$edit = GUICtrlCreateEdit("Thank you, Gary ;-)", 100, 100, 200, 50, $ES_READONLY)
$button = GUICtrlCreateButton("E&xit", 175, 160, 50, 25)

GUISetState(@SW_SHOW)
WinSetOnTop($guiTitle, "", 1)
AdlibRegister("HoverTrans")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $button
    Exit
    EndSwitch
WEnd

Func HoverTrans()
    $hover = GUIGetCursorInfo($myGUI)
        ;MsgBox("", $guiTitle, "hover[0] = " & $hover[0] & @CRLF & "hover[0] = " & $hover[0], 4)
    Select
    Case $hover[0] < 0 Or $hover[1] < -30 Or $hover[0] > $guiW Or $hover[1] > $guiH
    If $solid Then
    $solid = 0
    WinSetTrans($guiTitle, "", 160)
    EndIf
    Case $solid = 0
    $solid = 1
    WinSetTrans($guiTitle, "", 255)
    EndSelect

EndFunc ;==>HoverTrans

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

It doesn't work for me. I can't see any GUI/window.

I think you haven't quite got it monter. :( There is no gui.

Read the code and notice that there is a hotkey!

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I think you haven't quite got it monter. :) There is no gui.

Read the code and notice that there is a hotkey!

You're right :(

Now, I can't to turn it off ;)

'Esc' nor '`' doesn't change the semi-transparency state :)

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

You're right :(

Now, I can't to turn it off :)

'Esc' nor '`' doesn't change the semi-transparency state :)

Is the AutoIt icon still there in the notification area? If not, then you have probably closed the script which is why ` no longer does anything. If you have a semi-transparent window and you have closed the script, you need to run it again in order to toggle the transparency back off again. I have noticed that sometimes key bindings can stop working in AutoIt scripts, I think this usually happens if I have two scripts running that both try to bind the same key so that shouldn't be it - unless you ran it twice of course, I should stick that singleton pattern in.
Link to comment
Share on other sites

Is the AutoIt icon still there in the notification area? If not, then you have probably closed the script which is why ` no longer does anything.

Probably I entered ` and then Esc, so the script closed, but semi-transparent state remained active.

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

Did you think it would remember which windows were transparant and undo it on exit?

I think it would be better :(

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

I think it would be better :(

Here you go then. This version does not set the transparency to a fixed amount, it picks a random number on a pseudo-normal distribution (0-255 3 times averaged, roughly a bell curve). It's easy to change back to a fixed value though.

#include <WinAPI.au3>
#include <Misc.au3>
#include <Array.au3>

Global $Struct = DllStructCreate($tagPoint)
Global $aWindows = ""

Opt('MustDeclareVars', 1)
Opt("WinTitleMatchMode", 2) ; Match substring
HotKeySet("`","ToggleTrans")

Local $i

While Not(_IsPressed("1B"))
    Sleep(10)
WEnd
For $i = 0 To UBound( $aWindows ) - 1
    WinSetTrans($aWindows[$i], "", 255) ; Make fully opaque
Next

Func Pos()
    DllStructSetData($Struct, "x", MouseGetPos(0))
    DllStructSetData($Struct, "y", MouseGetPos(1))
EndFunc   ;==>Pos

Func ToggleTrans()
    Local $hWnd, $hLast, $trans, $pos
    Pos()
    $hWnd = _WinAPI_WindowFromPoint($Struct)
    do
        $hLast = $hWnd
        $hWnd = _WinAPI_GetParent($hLast)
    Until $hWnd = 0
    $hWnd = $hLast
    $trans = WinGetTrans($hWnd)
    If $trans < 255 And $trans >= 0 Then
        WinSetTrans($hWnd, "", 255) ; Make fully opaque
        $pos = _ArraySearch( $aWindows, $hWnd )
        If $pos >= 0 Then
            _ArrayDelete( $aWindows, $pos )
        EndIf
    Else
        WinSetTrans($hWnd, "", (Random(0,255,1)+Random(0,255,1)+Random(0,255,1))/3) ; Make window semi-transparent
        _ArrayAdd1( $aWindows, $hWnd )
    EndIf
EndFunc

Func WinGetTrans($sTitle, $sText = "")
    Local $hWnd = WinGetHandle($sTitle, $sText)
    If Not $hWnd Then Return -1
    Local $val = DllStructCreate("int")
    Local $aRet = DllCall("user32.dll", "int", "GetLayeredWindowAttributes", "hwnd", $hWnd, "ulong_ptr", 0, "int_ptr", DllStructGetPtr($val), "ulong_ptr", 0)
    If @error Or Not $aRet[0] Then Return -1
    Return DllStructGetData($val, 1)
EndFunc

Func _ArrayAdd1( ByRef $avArray, $vValue )
    Local $iUBound = UBound($avArray)
    If Not(IsArray($avArray)) Or UBound($avArray, 0) <> 1 Then
        Dim $TempArray[1]
        $avArray = $TempArray
        $iUBound = 0
    Else
        ReDim $avArray[$iUBound + 1]
    EndIf
    $avArray[$iUBound] = $vValue
    Return $iUBound
EndFunc
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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