Using Python Win32 extensions, how do you make an arbitrary window on Windows XP transparent?
Asked
Active
Viewed 5,114 times
3 Answers
16
SetLayeredWindowsAttributes example:
import win32gui
import win32con
import winxpgui
import win32api
import subprocess
import time
subprocess.Popen("notepad.exe", shell=True)
time.sleep(1)
hwnd = win32gui.FindWindow(None, "New file - metapad") ## The caption of my empty notepad (MetaPad)
win32gui.SetWindowLong (hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong (hwnd, win32con.GWL_EXSTYLE ) | win32con.WS_EX_LAYERED )
winxpgui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0,0,0), 180, win32con.LWA_ALPHA)

Stevoisiak
- 23,794
- 27
- 122
- 225

PabloG
- 25,761
- 10
- 46
- 59
-
You can eliminate the `winxpgui` import by using `win32gui.SetLayeredWindowAttributes()` – Stevoisiak Aug 14 '18 at 14:56
3
You can use the SetLayeredWindowAttributes WIN32 API function for creating transparent windows:
BOOL WINAPI SetLayeredWindowAttributes(
__in HWND hwnd,
__in COLORREF crKey,
__in BYTE bAlpha,
__in DWORD dwFlags
);
Here is a code sample that you can use for wrapping WIN32 API functions for setting transparency.

ArBR
- 4,032
- 2
- 23
- 29
0
import win32gui
import win32con
import winxpgui
import win32api
import subprocess
import time
subprocess.Popen("notepad.exe", shell=True)
time.sleep(1)
hwnd = win32gui.FindWindow(None, "New file - metapad") ## The caption of my empty notepad (MetaPad)
win32gui.SetWindowLong (hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong (hwnd, win32con.GWL_EXSTYLE ) | win32con.WS_EX_LAYERED )
winxpgui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0,0,0), 180, win32con.LWA_ALPHA)
This does not work for Windows 10.

Adrian Mole
- 49,934
- 160
- 51
- 83

arma
- 1
- 1
-
Yes it does. I just tested it. In fact it even works on Windows 11 – Mihail-Cosmin Munteanu Oct 16 '21 at 20:01