当前位置: 首页 > news >正文

调整应用窗口透明度

朋友问我有没有软件透明得,一开始没理解,他给我发一个,我一看原来时调整窗口透明度得,想着python应该也可以实现,就写了一个。

效果图如下:

源码如下:
 

import sys
import ctypes
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QSlider, QHBoxLayout, QFrame
from PySide2.QtCore import Qt, QTimer
import win32gui
import win32condef is_valid_window(hwnd, title):"""判断窗口是否为有效的主窗口,只显示在任务栏的窗口"""if not title:return False# 获取窗口类名class_name = win32gui.GetClassName(hwnd)if "Cortana" in title or "SearchUI" in class_name:return False# 检查窗口是否可见,并且不处于最小化状态if not win32gui.IsWindowVisible(hwnd) or win32gui.IsIconic(hwnd):return False# 检查窗口的样式,确保它是应用程序窗口而非工具窗口等style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE)if not (style & win32con.WS_EX_APPWINDOW):return Falsereturn Trueclass WindowBlock(QFrame):"""表示单个窗口信息的块"""def __init__(self, hwnd, title, parent=None):super().__init__(parent)self.hwnd = hwndself.title = title[:8]  # 最多显示8个字符self.init_ui()def init_ui(self):# 设置边框self.setFrameStyle(QFrame.Box | QFrame.Raised)self.setLineWidth(1)  # 边框线宽# 布局:横向布局,标题在左,滑块在右layout = QHBoxLayout()# 设置统一的固定大小self.setFixedSize(50, 150)  # 宽度100,高度150,根据你的需求调整# 调整内部布局的间距和边距,让整体更紧凑layout.setContentsMargins(5, 5, 5, 5)  # 控制窗口边距,越小越紧凑layout.setSpacing(3)  # 控制内部元素之间的间距# 竖直显示窗口标题self.title_label = QLabel(self.format_vertical_text(self.title))self.title_label.setAlignment(Qt.AlignCenter)layout.addWidget(self.title_label)# 透明度滑块,竖向布局self.slider = QSlider(Qt.Vertical)  # 改为竖向拖动self.slider.setMinimum(0)self.slider.setMaximum(255)self.slider.setValue(255)self.slider.valueChanged.connect(self.change_transparency)layout.addWidget(self.slider)self.setLayout(layout)def format_vertical_text(self, text):"""将文本格式化为竖直排列"""return '\n'.join(list(text))def change_transparency(self, value):"""根据滑块值调整窗口透明度"""style = win32gui.GetWindowLong(self.hwnd, win32con.GWL_EXSTYLE)win32gui.SetWindowLong(self.hwnd, win32con.GWL_EXSTYLE, style | win32con.WS_EX_LAYERED)win32gui.SetLayeredWindowAttributes(self.hwnd, 0, value, win32con.LWA_ALPHA)class WindowTransparencyApp(QWidget):"""主窗口类,展示所有可调整透明度的窗口"""def __init__(self):super().__init__()self.setWindowTitle("窗口透明度调整器")self.setGeometry(300, 300, 800, 200)  # 横向布局,调宽度# 总体布局self.layout = QHBoxLayout()  # 改为横向布局self.layout.setContentsMargins(5, 5, 5, 5)  # 控制主窗口的边距self.layout.setSpacing(5)  # 控制每个窗口块之间的间距self.setLayout(self.layout)# 设置定时器每秒更新窗口列表self.timer = QTimer(self)  # 使用 QTimerself.timer.timeout.connect(self.update_window_list)self.timer.start(1000)  # 每秒刷新一次窗口列表# 初始化窗口列表self.windows = []def update_window_list(self):"""每秒更新当前打开的窗口列表,保持最新状态。"""current_hwnds = [hwnd for hwnd, _ in self.windows]new_windows = []def enum_window_callback(hwnd, _):title = win32gui.GetWindowText(hwnd)if is_valid_window(hwnd, title):if hwnd not in current_hwnds:  # 仅添加新窗口new_windows.append((hwnd, title))# 枚举所有窗口win32gui.EnumWindows(enum_window_callback, None)# 如果有新窗口,更新显示if new_windows:for hwnd, title in new_windows:self.windows.append((hwnd, title))window_block = WindowBlock(hwnd, title)self.layout.addWidget(window_block)# 检查是否有窗口关闭for hwnd, title in self.windows[:]:if not win32gui.IsWindow(hwnd):self.windows.remove((hwnd, title))# 删除对应的窗口块for i in range(self.layout.count()):widget = self.layout.itemAt(i).widget()if isinstance(widget, WindowBlock) and widget.hwnd == hwnd:widget.deleteLater()if __name__ == "__main__":app = QApplication(sys.argv)window = WindowTransparencyApp()window.show()window.adjustSize()sys.exit(app.exec_())

成品下载:Release 1.0 · duanxcseven/hide (github.com)

http://www.lryc.cn/news/460798.html

相关文章:

  • 启智畅想集装箱号码智能识别原理,OCR识别应用
  • React基础知识
  • Java基础:面向对象编程3
  • 实验kubernetes的CPU绑定策略
  • Zsh 安装与配置
  • Redis可视化工具Redis Desktop Manager(附安装包)
  • sql server删除过期备份文件脚本
  • 【Docker系列】Docker查看镜像架构
  • Python案例 | 测试网络的下载速度上传速度和 ping 延迟
  • 一键找回,2024四大固态硬盘数据恢复工具推荐!
  • 数据结构~AVL树
  • ffmpeg面向对象——rtsp拉流探索(1)
  • 【启明智显分享】ZX7981PM WIFI6 5G-CPE:2.5G WAN口,2.4G/5G双频段自动调速
  • openresty“热部署“lua
  • 基于SpringBoot+Vue+MySQL的企业招聘管理系统
  • vue3之defineComponent
  • springboot+vue家政服务管理平台
  • python pip安装requirements.txt依赖与国内镜像
  • 解决Qt的QWidget设计师编辑UI后和软件运行显示不一致
  • 交易所开发:构建安全、高效、可靠的数字资产交易平台
  • 【Next.js 入门教程系列】09-优化技巧
  • Windows 11 开发详解:工具与高级用法
  • leetcode 292.Nim游戏
  • 《最优化方法》
  • fork中的死锁问题
  • Java面试题———SpringBoot篇
  • 模块化沙箱:构建零信任架构的关键技术
  • 10.14学习日志
  • “我们为什么缺少科学精神”演讲内容拆解
  • openpyxl 3.0.7 中文教程