Author Topic: Transparency for all windows at the same time!  (Read 3423 times)

0 Members and 1 Guest are viewing this topic.

Offline loudar

  • Newbie
  • Posts: 73
  • improve it bit by bit.
    • View Profile
Transparency for all windows at the same time!
« on: August 07, 2020, 08:11:35 am »
I've been using WindowTop for a while now and I don't like that it applies to only one window at a time. It thought: There must be a way I can apply transparency to all windows at the same time, right?

I'm a sucker for pretty backgrounds but also rarely see them when I work. Some slight transparency solves that :D

Here's a program where every window you focused since the start of it will have the transparency applied, you can modulate it with "+" and "-".
You can also choose with the enter key which window to activate/deactivate the transparency on.
The arrow on the right side shows the currently focused window.

Code: QB64: [Select]
  1. CONST SWP_NOSIZE = &H0001 'ignores cx and cy size parameters
  2. CONST SWP_NOMOVE = &H0002 'ignores x and y position parameters
  3. CONST SWP_NOZORDER = &H0004 'keeps z order and ignores hWndInsertAfter parameter
  4. CONST SWP_NOREDRAW = &H0008 'does not redraw window changes
  5. CONST SWP_NOACTIVATE = &H0010 'does not activate window
  6. CONST SWP_FRAMECHANGED = &H0020
  7. CONST SWP_SHOWWINDOW = &H0040
  8. CONST SWP_HIDEWINDOW = &H0080
  9. CONST SWP_NOCOPYBITS = &H0100
  10. CONST SWP_NOOWNERZORDER = &H0200
  11. CONST SWP_NOSENDCHANGING = &H0400
  12. CONST SWP_DRAWFRAME = SWP_FRAMECHANGED
  13. CONST SWP_NOREPOSITION = SWP_NOOWNERZORDER
  14. CONST SWP_DEFERERASE = &H2000
  15. CONST SWP_ASYNCWINDOWPOS = &H4000
  16. CONST HWND_TOP = 0 'window at top of z order no focus
  17. CONST HWND_BOTTOM = 1 'window at bottom of z order no focus
  18. CONST HWND_TOPMOST = -1 'window above all others no focus unless active
  19. CONST HWND_NOTOPMOST = -2 'window below active no focus
  20.     'sets a created window region
  21.     FUNCTION SetWindowRgn& (BYVAL hwnd&, BYVAL hrgn&, BYVAL bredraw%)
  22.     FUNCTION SetLayeredWindowAttributes& (BYVAL hwnd AS LONG, BYVAL crKey AS LONG, BYVAL bAlpha AS _UNSIGNED _BYTE, BYVAL dwFlags AS LONG)
  23.     FUNCTION GetWindowLong& ALIAS "GetWindowLongA" (BYVAL hwnd AS LONG, BYVAL nIndex AS LONG)
  24.     FUNCTION SetWindowLong& ALIAS "SetWindowLongA" (BYVAL hwnd AS LONG, BYVAL nIndex AS LONG, BYVAL dwNewLong AS LONG)
  25.     FUNCTION FindWindowA%& (BYVAL lpClassName%&, BYVAL lpWindowName%&)
  26.     FUNCTION GetForegroundWindow&
  27.     'FUNCTION GetWindowTextLengthA (BYVAL hwnd&)
  28.     'FUNCTION GetWindowTextA (BYVAL hwnd&, BYVAL lpString%&, BYVAL nMaxCount)
  29.     'creates a rectangular region
  30.     FUNCTION CreateRectRgn& (BYVAL x1&, BYVAL y1&, BYVAL x2&, BYVAL y2&)
  31.     'creates an elliptical region
  32.     FUNCTION CreateEllipticRgn& (BYVAL x1&, BYVAL y1&, BYVAL x2&, BYVAL y2&)
  33.     'creates a rectangular region with rounded corners
  34.     FUNCTION CreateRoundRectRgn& (BYVAL x1&, BYVAL y1&, BYVAL x2&, BYVAL y2&, BYVAL x3&, BYVAL y3&)
  35.     FUNCTION GetLastError~& ()
  36.  
  37. maxwindows = 200
  38. DIM SHARED re&(maxwindows) 'you'll never have more than 200 windows open, eh?
  39. DIM SHARED applied(maxwindows)
  40. DIM SHARED maxr
  41.  
  42. SCREEN _NEWIMAGE(600, 200, 32)
  43. COLOR _RGBA(0, 0, 0, 255), _RGBA(255, 255, 255, 255)
  44.  
  45. 'notification "WinIsTrans", "is now active."
  46.  
  47. t = 255
  48. selected = 1
  49.     Taste$ = INKEY$
  50.     IF Taste$ = "+" THEN
  51.         IF t < 255 THEN
  52.             t = t + 1
  53.         END IF
  54.     END IF
  55.     IF Taste$ = "-" THEN
  56.         IF t > 20 THEN
  57.             t = t - 1
  58.         END IF
  59.     END IF
  60.     IF Taste$ = CHR$(0) + CHR$(80) THEN
  61.         IF selected < maxr THEN
  62.             selected = selected + 1
  63.         ELSE
  64.             selected = 1
  65.         END IF
  66.     END IF
  67.     IF Taste$ = CHR$(0) + CHR$(72) THEN
  68.         IF selected > 1 THEN
  69.             selected = selected - 1
  70.         ELSE
  71.             selected = maxr
  72.         END IF
  73.     END IF
  74.     IF Taste$ = CHR$(13) THEN
  75.         IF applied(selected) = 0 THEN
  76.             applied(selected) = 1
  77.         ELSE
  78.             applied(selected) = 0
  79.         END IF
  80.     END IF
  81.  
  82.     hwnd& = GetForegroundWindow&
  83.  
  84.     'check for invalid
  85.     IF maxr > 0 THEN
  86.         DO
  87.             repeat = 0
  88.             r = 0: DO: r = r + 1
  89.                 IF re&(r) = 0 AND r < maxr THEN
  90.                     repeat = 1
  91.                     DO
  92.                         re&(r) = re&(r + 1)
  93.                         r = r + 1
  94.                     LOOP UNTIL r >= maxr - 1
  95.                     r = 0
  96.                     maxr = maxr - 1
  97.                 ELSEIF re&(r) = 0 AND r = maxr THEN
  98.                     repeat = 0
  99.                     maxr = maxr - 1
  100.                     r = 0
  101.                 END IF
  102.             LOOP UNTIL r >= maxr
  103.         LOOP WHILE repeat = 1
  104.     END IF
  105.  
  106.     'check for existence
  107.     found = 0
  108.     IF maxr > 0 THEN
  109.         r = 0: DO: r = r + 1
  110.             IF re&(r) = hwnd& THEN
  111.                 found = 1
  112.             END IF
  113.         LOOP UNTIL r = maxr
  114.     END IF
  115.     IF found = 0 THEN
  116.         maxr = maxr + 1
  117.         resize
  118.         re&(maxr) = hwnd&
  119.     END IF
  120.  
  121.     'apply transparency
  122.     IF maxr > 0 THEN
  123.         r = 0: DO: r = r + 1
  124.             IF re&(r) <> _WINDOWHANDLE AND applied(r) = 1 THEN
  125.                 SetWindowOpacity re&(r), t
  126.             ELSE
  127.                 SetWindowOpacity re&(r), 255
  128.             END IF
  129.         LOOP UNTIL r = maxr
  130.     END IF
  131.  
  132.     CLS
  133.     LOCATE 1, 1
  134.     COLOR _RGBA(0, 0, 0, 255)
  135.     PRINT "Transparency: "; t
  136.     r = 0: DO: r = r + 1
  137.         LOCATE r + 1, 1
  138.         IF selected = r THEN
  139.             PRINT "> ";
  140.         END IF
  141.         IF applied(r) = 1 THEN
  142.             COLOR _RGBA(61, 194, 67, 255)
  143.         ELSE
  144.             COLOR _RGBA(0, 0, 0, 255)
  145.         END IF
  146.         PRINT LTRIM$(STR$(re&(r)));
  147.         IF re&(r) = GetForegroundWindow& THEN
  148.             PRINT " <"
  149.         ELSE
  150.             PRINT ""
  151.         END IF
  152.         'l = GetWindowTextLengthA(re&(r))
  153.         'PRINT l, GetWindowTextA(re&(r), 0, l)
  154.     LOOP UNTIL r = maxr
  155.     _DISPLAY
  156.     _LIMIT 60
  157. LOOP UNTIL Taste$ = CHR$(27)
  158. IF maxr > 0 THEN
  159.     r = 0: DO: r = r + 1
  160.         SetWindowOpacity re&(r), 255
  161.     LOOP UNTIL r = maxr
  162.  
  163. SUB notification (title$, text$)
  164.     com$ = com$ + "[reflection.assembly]::loadwithpartialname(" + CHR$(34) + "System.Windows.Forms" + CHR$(34) + ") " + CHR$(13)
  165.     com$ = com$ + "[reflection.assembly]::loadwithpartialname(" + CHR$(34) + "System.Drawing" + CHR$(34) + ")" + CHR$(13)
  166.     com$ = com$ + "$notify = new-object system.windows.forms.notifyicon" + CHR$(13)
  167.     com$ = com$ + "$notify.icon = [System.Drawing.SystemIcons]::WinLogo" + CHR$(13)
  168.     com$ = com$ + "$notify.visible = $true" + CHR$(13)
  169.     com$ = com$ + "$notify.showballoontip(10," + CHR$(34) + title$ + CHR$(34) + "," + CHR$(34) + text$ + CHR$(34) + ",[system.windows.forms.tooltipicon]::None)"
  170.     SHELL com$
  171.  
  172. SUB resize
  173.     fh = _FONTHEIGHT
  174.     SCREEN _NEWIMAGE(600, 200 + (fh * (maxr)), 32)
  175.     COLOR _RGBA(0, 0, 0, 255), _RGBA(255, 255, 255, 255)
  176.     CLS
  177.  
  178. SUB SetWindowOpacity (hWnd&, Level)
  179.     DIM Msg AS LONG
  180.     CONST G = -20
  181.     CONST LWA_ALPHA = &H2
  182.     CONST WS_EX_LAYERED = &H80000
  183.     Msg = GetWindowLong(hWnd, G)
  184.     Msg = Msg OR WS_EX_LAYERED
  185.     Crap = SetWindowLong(hWnd&, G, Msg)
  186.     Crap = SetLayeredWindowAttributes(hWnd&, 0, Level, LWA_ALPHA)
  187.     IF Crap = 0 THEN hWnd& = 0
« Last Edit: August 07, 2020, 11:38:15 am by loudar »
Check out what I do besides coding: http://loudar.myportfolio.com/

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Transparency for all windows at the same time!
« Reply #1 on: August 07, 2020, 01:05:12 pm »
Nice one! I had fun running this! :)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials