本文目录导读:

我来帮你写一个自动切换窗口透明度的脚本,这里提供几种不同平台的实现方案:
Windows (AutoHotkey)
; 自动切换窗口透明度脚本
; 使用 Ctrl+Alt+T 切换当前活动窗口透明度
; 定义透明度级别
transparency_levels := [255, 200, 150, 100, 50]
current_index := 1
; 热键:Ctrl+Alt+T 切换透明度
^!t::
WinGet, active_id, ID, A
current_index := Mod(current_index, transparency_levels.Length()) + 1
WinSet, Transparent, % transparency_levels[current_index], ahk_id %active_id%
ToolTip, 透明度:% transparency_levels[current_index] "/255"
SetTimer, RemoveToolTip, 2000
return
; 热键:Ctrl+Alt+R 重置为不透明
^!r::
WinGet, active_id, ID, A
WinSet, Transparent, 255, ahk_id %active_id%
current_index := 1
ToolTip, 恢复不透明
SetTimer, RemoveToolTip, 2000
return
RemoveToolTip:
ToolTip
SetTimer, RemoveToolTip, Off
return
; 鼠标滚轮调节(在窗口标题栏使用时)
#IfWinActive
WheelUp & ^::
WinGet, active_id, ID, A
WinGet, current_trans, Transparent, ahk_id %active_id%
if (current_trans = "")
current_trans := 255
new_trans := current_trans + 25
if (new_trans > 255)
new_trans := 255
WinSet, Transparent, %new_trans%, ahk_id %active_id%
return
WheelDown & ^::
WinGet, active_id, ID, A
WinGet, current_trans, Transparent, ahk_id %active_id%
if (current_trans = "")
current_trans := 255
new_trans := current_trans - 25
if (new_trans < 20)
new_trans := 20
WinSet, Transparent, %new_trans%, ahk_id %active_id%
return
Python (Windows,使用 ctypes)
import ctypes
import time
import random
class WindowTransparency:
def __init__(self):
self.GWL_EXSTYLE = -20
self.WS_EX_LAYERED = 0x80000
self.update = ctypes.windll.user32.UpdateLayeredWindow
def set_transparency(self, hwnd, alpha):
"""设置窗口透明度 (0-255)"""
if not hwnd:
return False
# 获取窗口扩展样式
user32 = ctypes.windll.user32
style = user32.GetWindowLongW(hwnd, self.GWL_EXSTYLE)
# 设置分层窗口
user32.SetWindowLongW(hwnd, self.GWL_EXSTYLE, style | self.WS_EX_LAYERED)
# 设置透明度
result = user32.SetLayeredWindowAttributes(hwnd, 0, alpha, 2)
return result
def get_active_window(self):
"""获取当前活动窗口"""
user32 = ctypes.windll.user32
return user32.GetForegroundWindow()
def auto_cycle(self, interval=2):
"""自动循环切换透明度"""
alphas = [255, 200, 150, 100, 150, 200]
index = 0
print("自动切换透明度启动,按 Ctrl+C 退出")
try:
while True:
hwnd = self.get_active_window()
if hwnd:
self.set_transparency(hwnd, alphas[index])
print(f"窗口 {hwnd} 透明度设置为: {alphas[index]}")
index = (index + 1) % len(alphas)
time.sleep(interval)
except KeyboardInterrupt:
print("\n已退出")
# 使用示例
if __name__ == "__main__":
wm = WindowTransparency()
# 手动设置透明度
# hwnd = wm.get_active_window()
# wm.set_transparency(hwnd, 150)
# 自动循环切换
wm.auto_cycle(3)
PowerShell 脚本
# 窗口透明度切换脚本
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class WinAPI {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
}
"@
$transparencyValues = @(255, 200, 150, 100, 50)
$currentIndex = 0
function Set-Transparency($alpha) {
$hwnd = [WinAPI]::GetForegroundWindow()
$GWL_EXSTYLE = -20
$WS_EX_LAYERED = 0x80000
$LWA_ALPHA = 0x2
$style = [WinAPI]::GetWindowLong($hwnd, $GWL_EXSTYLE)
[WinAPI]::SetWindowLong($hwnd, $GWL_EXSTYLE, $style -bor $WS_EX_LAYERED)
[WinAPI]::SetLayeredWindowAttributes($hwnd, 0, $alpha, $LWA_ALPHA)
}
# 循环切换透明度
while ($true) {
$alpha = $transparencyValues[$currentIndex]
Set-Transparency $alpha
Write-Host "已设置透明度: $alpha" -ForegroundColor Green
$currentIndex = ($currentIndex + 1) % $transparencyValues.Length
Start-Sleep -Seconds 3
}
Electron/Node.js (跨平台)
// 需要安装 electron 和 robotjs
const { app, BrowserWindow, globalShortcut } = require('electron');
const robot = require('robotjs');
class TransparencyManager {
constructor() {
this.opacityLevels = [1, 0.8, 0.5, 0.3, 0.1];
this.currentIndex = 0;
}
// 读取指定窗口的透明度(仅限自己创建的窗口)
setOwnWindowTransparency(window, opacity) {
window.setOpacity(opacity);
}
// 系统级窗口处理(需要第三方工具)
async setSystemWindowTransparency() {
// 在 Windows 上可以使用 PowerShell 命令
const { exec } = require('child_process');
const script = `
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
}
"@
# Get foreground window
$hwnd = [Win32]::GetForegroundWindow()
# Set opacity
[Win32]::SetLayeredWindowAttributes($hwnd, 0, 150, 2)
`;
exec(`powershell -Command "${script}"`, (error, stdout, stderr) => {
if (error) console.error('错误:', error);
else console.log('设置成功');
});
}
// 自动循环
startAutoCycle(interval = 3000) {
console.log('开始自动切换透明度...');
setInterval(() => {
this.currentIndex = (this.currentIndex + 1) % this.opacityLevels.length;
const opacity = this.opacityLevels[this.currentIndex];
console.log(`当前透明度: ${opacity}`);
// 这里调用具体的系统窗口设置方法
this.setSystemWindowTransparency();
}, interval);
}
}
// 使用示例
const manager = new TransparencyManager();
manager.startAutoCycle(5000); // 每5秒切换一次
使用建议
- 保存当前设置:修改时记住原始透明度,提供恢复功能
- 快捷键操作:添加方便的热键组合
- 异常处理:某些系统窗口可能不允许设置透明度
- 性能考虑:避免过于频繁地修改透明度
- 用户界面:可以考虑添加托盘图标和菜单
这些脚本可以根据你的具体需求调整,如果你需要特定平台或功能的实现,请告诉我具体需求!