Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Transparency tool


  • Please log in to reply
16 replies to this topic
Eleria
  • Members
  • 98 posts
  • Last active: Jul 05 2007 02:51 AM
  • Joined: 09 May 2006
I searched through the forum to see if there was a tool to make a window under the cursor transparent, but i only found long scripts and i wondered if there is a simpler way.
Here is my tool.
It handles transparency through multiple variables so each window can have his own setting. Also displays the transparency level through a tooltip.
Oh and Ctrl+Shift+Wheel up/down does the trick :wink:
#SingleInstance, Force
#NoEnv

OnExit Exit
Menu, Tray, Tip, Transparence Powa!
Return

^+WheelDown::
^+WheelUp::
MouseGetPos, cx, cy, Win_Id
WinGetClass, Class, ahk_id %Win_Id%
If Class = Progman
      Return
If Needle%Win_Id% = 
{
	WinGet, Trans, Transparent, ahk_id %Win_Id%
	IfEqual, Trans,, SetEnv, Trans, 255
	List = %List%%Win_Id%,%Trans% 
	Needle%Win_Id% = %Trans%
}
IfEqual, A_ThisHotkey, ^+Wheelup, EnvAdd, Needle%Win_Id%, 15
Else Needle%Win_Id% -= 15
IfGreater, Needle%Win_Id%, 255, SetEnv, Needle%Win_Id%, 255
IfLess, Needle%Win_Id%, 30, SetEnv, Needle%Win_Id%, 30
Winset, Transparent,% Needle%Win_Id%, ahk_id %Win_Id%
Gosub, Set_ToolTip
Return

Set_ToolTip:
Tooltip,% "Transparency level :" Needle%Win_Id%, % cx+20,% cy+20
SetTimer, RemoveToolTip, 1000
Return

RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
Return

Exit:
Loop, Parse, List, `,
If (A_Index & 1)           ; Win_ID's are in odd positions
         Id = %A_LoopField%
      Else
         Winset Transparent, %A_LoopField%, ahk_id %Id% 
ExitApp

Posted Image

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Nice work! I think it is useful to restore the original transparency levels, when the script exits, or at least, ask the user if he wants that. (We need to maintain a list with the window ID's, and the original transparency levels.) Also, there are some little simplifications possible:
OnExit EXIT

^+WheelDown::                    ; Ctrl-Shift-Wheel changes transparency under mouse

^+WheelUp::

   Sleep 50                      ; Slow down wheel events

   MouseGetPos cx, cy, Win_Id

   WinGetClass Class, ahk_id %Win_Id%

   If Class in Progman,Shell_TrayWnd

      Return                     ; Do nothing with special windows

   IfEqual N%Win_Id%,, {

      WinGet T, Transparent, ahk_id %Win_Id%

      IfEqual T,, SetEnv T,255   ; No transparency = Opaque

      List = %List%%Win_Id%,%T%, ; Win_ID, TransLevel, appended to List

      N%Win_Id% = %T%            ; Initial transparency of window

   }

   IfEqual A_ThisHotKey,^+WheelUp, EnvAdd N%Win_Id%,20

   Else N%Win_Id% -= 20          ; Transparency changes by 20

   IfGreater N%Win_Id%,255, SetEnv N%Win_Id%,255

   IfLess    N%Win_Id%,40,  SetEnv N%Win_Id%,40

   WinSet Transparent, % N%Win_Id%, ahk_id %Win_Id%

   TrayTip,,% "Transparency: " N%Win_Id%

Return



EXIT:

  Loop Parse, List, `,

      If (A_Index & 1)           ; Win_ID's are in odd positions

         Id = %A_LoopField%

      Else

         Winset Transparent, %A_LoopField%, ahk_id %Id%

ExitApp


Eleria
  • Members
  • 98 posts
  • Last active: Jul 05 2007 02:51 AM
  • Joined: 09 May 2006
Wow there i see someone who has experience with ahk :O
Your code is very compact. On greater projects it must be very hard to maintain your codes :wink:
Anyway,
May i ask you why using environment variables ?
Isn't it quite unsafe to use them ?
If not then what is the benefit :?:

Oh and, thank you for your reply :wink:
Posted Image

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
No environment variables are used, those EnvAdd, SetEnv commands are just awkward (historic) names for assignments and "+=". They are needed if we use them in the same line as an IF statement.

I don't think a 20 line, well commented, compact subroutine is harder to maintain than an equivalent 60 line routine, which breaks down the code to sub-subroutines, which are called only once. You could always add blank lines and full line comments instead of function calls. On the other hand, you can supply test vectors for those sub-subs separately, store them in different files and include them in the main script. It is hard to find the balance. In a hierarchy of functions, 20 levels deep, it is difficult to find anything you may need. And, of course, you need different tools and methodology for a large team than for a single programmer.

Eleria
  • Members
  • 98 posts
  • Last active: Jul 05 2007 02:51 AM
  • Joined: 09 May 2006
I learnt alot thanks to you :oops:
And cut my code from a big part using some of your tricks 8)

But i get hard time understanding your exit loop.
Does both If and Else get executed each loop iteration ?
And also i got a little question : what the hell does the (A_Index & 1) thingy mean ? I understand it as follows :
(first iteration) : 1&1 so true
(second iteration) : 1&2 so false......
(third iteration) : 1&3 ....etc
Posted Image

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

i get hard time understanding your exit loop... what the hell does the (A_Index & 1) thingy mean ?

"&" is the bitwise AND operator, therefore (A_Index & 1) extracts the least significant bit of the loop index. It has the values 1, 0, 1, 0...
Accordingly, the first time the "Id = %A_LoopField%" instruction sets Id to the window ID read from List, the second iteration (the ELSE branch) sets the transparency of this window to the new A_LoopField level, and this repeats with the third/fourth entries in the List, then with the fifth/sixth entries, etc.

Eleria
  • Members
  • 98 posts
  • Last active: Jul 05 2007 02:51 AM
  • Joined: 09 May 2006
(I did not think the A_Index was converted to binary when using the & operator)
But now, i understand :p
Thank you again for your patience.

Edit: Updated code
Posted Image

Seabiscuit
  • Members
  • 109 posts
  • Last active: May 08 2008 06:31 PM
  • Joined: 07 Jan 2007
:( none of above codes works on ex: AutoHotkey.chm window, or Firefox window. Too bad :?

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

:( none of above codes works on ex: AutoHotkey.chm window, or Firefox window. Too bad :?

They certainly do... unless you're on Windows 98 (or similar.) Layered windows (i.e. window transparency) only works on Windows 2000 and up.

Seabiscuit
  • Members
  • 109 posts
  • Last active: May 08 2008 06:31 PM
  • Joined: 07 Jan 2007
WinXP SP2, ex on FF one way is reducing transparency, the other way cycle throu tabs.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
Are you sure you have no other scripts running? Those scripts don't send any keystrokes... Press ^WheelUp/Down (whichever doesn't work), and check the key history, to see if it recognizes it.

fatal
  • Guests
  • Last active:
  • Joined: --
hey i need somone to modify this so i can use shift cntrl left click to increase transparency and shift cntl right click to decrease

fatal
  • Guests
  • Last active:
  • Joined: --
or snap to full visibility for shift cntrl right click

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
#SingleInstance, Force
#NoEnv

OnExit Exit
Menu, Tray, Tip, Transparence Powa!
Return

^+LButton::
^+RButton::
MouseGetPos, cx, cy, Win_Id
WinGetClass, Class, ahk_id %Win_Id%
If Class = Progman
      Return
If Needle%Win_Id% =
{
   WinGet, Trans, Transparent, ahk_id %Win_Id%
   IfEqual, Trans,, SetEnv, Trans, 255
   List = %List%%Win_Id%,%Trans%
   Needle%Win_Id% = %Trans%
}
IfEqual, A_ThisHotkey, ^+RButton, EnvAdd, Needle%Win_Id%, 15
Else Needle%Win_Id% -= 15
IfGreater, Needle%Win_Id%, 255, SetEnv, Needle%Win_Id%, 255
IfLess, Needle%Win_Id%, 30, SetEnv, Needle%Win_Id%, 30
Winset, Transparent,% Needle%Win_Id%, ahk_id %Win_Id%
Gosub, Set_ToolTip
Return

Set_ToolTip:
Tooltip,% "Transparency level :" Needle%Win_Id%, % cx+20,% cy+20
SetTimer, RemoveToolTip, 1000
Return

RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
Return

Exit:
Loop, Parse, List, `,
If (A_Index & 1)           ; Win_ID's are in odd positions
         Id = %A_LoopField%
      Else
         Winset Transparent, %A_LoopField%, ahk_id %Id%
ExitApp
At $60 an hour you owe me $0.25

  • Guests
  • Last active:
  • Joined: --
after i posted it i figured it out and did it my self but i changed it to up and down insteadof left click and right click