python
import sys
import ctypes
import platform
from PySide6.QtCore import Qt, QPoint, QTimer
from PySide6.QtWidgets import (
QApplication,
QWidget,
QPushButton,
QVBoxLayout,
QHBoxLayout,
QDialog,
QLabel,
QSpinBox,
QDialogButtonBox,
)
from pynput.mouse import Controller as MouseController
# Windows-specific constants and functions
if platform.system() == "Windows":
GWL_EXSTYLE = -20
WS_EX_NOACTIVATE = 0x08000000
WS_EX_TOOLWINDOW = 0x00000080
def set_window_noactivate(widget):
hwnd = widget.winId().__int__()
ex_style = ctypes.windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
ex_style |= WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW
ctypes.windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, ex_style)
class ScrollButton(QPushButton):
def __init__(self, label, direction, parent=None):
super().__init__(label, parent)
self.direction = direction # 'up' 或 'down'
self.mouse = MouseController()
# 设置按钮不接受焦点
self.setFocusPolicy(Qt.NoFocus)
# 使用 QTimer 实现持续滚动
self.timer = QTimer()
self.timer.timeout.connect(self.scroll_step)
self.timer_interval = 50 # 默认每50毫秒滚动一次
def scroll_step(self):
if self.direction == 'up':
self.mouse.scroll(0, 1) # 向上滚动
elif self.direction == 'down':
self.mouse.scroll(0, -1) # 向下滚动
def mousePressEvent(self, event):
if event.button() == Qt.RightButton: # 修改为右键
self.timer.start(self.timer_interval) # 每50毫秒滚动一次,调整此值以控制滚动速度
event.accept()
else:
super().mousePressEvent(event)
def mouseReleaseEvent(self, event):
if event.button() == Qt.RightButton: # 修改为右键
self.timer.stop()
event.accept()
else:
super().mouseReleaseEvent(event)
def set_timer_interval(self, interval):
self.timer_interval = interval
if self.timer.isActive():
self.timer.start(self.timer_interval)
class SettingsDialog(QDialog):
def __init__(self, current_interval, parent=None):
super().__init__(parent)
self.setWindowTitle("设置滚动速度")
self.setModal(True)
self.setFixedSize(300, 100)
layout = QVBoxLayout()
label = QLabel("设置滚动间隔(毫秒):")
layout.addWidget(label)
self.spin_box = QSpinBox()
self.spin_box.setRange(10, 1000) # 允许设置的范围
self.spin_box.setValue(current_interval)
layout.addWidget(self.spin_box)
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
self.setLayout(layout)
def get_interval(self):
return self.spin_box.value()
class DraggableWindow(QWidget):
def __init__(self):
super().__init__()
# 设置窗口标志,使其始终在最前且无边框,并且不获取焦点
self.setWindowFlags(
Qt.WindowStaysOnTopHint |
Qt.FramelessWindowHint |
Qt.Tool
)
self.setAttribute(Qt.WA_ShowWithoutActivating) # 显示时不激活窗口
self.setAttribute(Qt.WA_TranslucentBackground, True) # 可选,设置透明背景
self.setFocusPolicy(Qt.NoFocus)
self.opacity_level = 0.8 # 初始透明度
self.setWindowOpacity(self.opacity_level)
self.init_ui()
self.dragging = False
self.drag_position = QPoint()
# 平台特定的设置
if platform.system() == "Windows":
set_window_noactivate(self)
elif platform.system() == "Linux":
# Linux上可能需要额外的设置,取决于窗口管理器
pass
elif platform.system() == "Darwin":
# macOS上可能需要额外的设置
pass
def init_ui(self):
main_layout = QVBoxLayout()
main_layout.setSpacing(10)
main_layout.setContentsMargins(10, 10, 10, 10)
# 创建上滚和下滚按钮
self.up_button = ScrollButton("⬆️ 上滚", 'up', self)
self.down_button = ScrollButton("⬇️ 下滚", 'down', self)
# 设置滚动按钮样式
scroll_button_style = """
QPushButton {
background-color: rgba(52, 152, 219, 0.8);
color: white;
border: none;
border-radius: 5px;
height: 40px;
font-size: 14px;
}
QPushButton:pressed {
background-color: rgba(41, 128, 185, 0.8);
}
QPushButton:hover {
background-color: rgba(52, 152, 219, 1);
}
"""
self.up_button.setStyleSheet(scroll_button_style)
self.down_button.setStyleSheet(scroll_button_style)
# 更新按钮的工具提示,说明使用右键进行滚动
self.up_button.setToolTip("右键按住以向上滚动")
self.down_button.setToolTip("右键按住以向下滚动")
main_layout.addWidget(self.up_button)
main_layout.addWidget(self.down_button)
# 创建水平布局用于透明度按钮、设置按钮和关闭按钮
bottom_layout = QHBoxLayout()
bottom_layout.setSpacing(5)
bottom_layout.setContentsMargins(0, 0, 0, 0)
# 创建透明度减少按钮
self.opacity_down = QPushButton("-")
self.opacity_down.setFixedSize(30, 30)
self.opacity_down.setToolTip("降低透明度")
self.opacity_down.setStyleSheet(self.get_opacity_button_style())
# 创建透明度增加按钮
self.opacity_up = QPushButton("+")
self.opacity_up.setFixedSize(30, 30)
self.opacity_up.setToolTip("增加透明度")
self.opacity_up.setStyleSheet(self.get_opacity_button_style())
# 连接透明度按钮到对应函数
self.opacity_down.clicked.connect(self.decrease_opacity)
self.opacity_up.clicked.connect(self.increase_opacity)
# 添加透明度按钮到底部布局
bottom_layout.addWidget(self.opacity_down)
bottom_layout.addWidget(self.opacity_up)
# 创建设置按钮
self.settings_button = QPushButton("设置")
self.settings_button.setFixedSize(50, 30) # 增大按钮宽度以适应文本
self.settings_button.setToolTip("设置滚动速度")
self.settings_button.setStyleSheet(self.get_settings_button_style())
self.settings_button.clicked.connect(self.open_settings)
# 创建关闭按钮
self.close_button = QPushButton("关闭")
self.close_button.setFixedSize(50, 30) # 增大按钮宽度以适应文本
self.close_button.setToolTip("关闭程序")
self.close_button.setStyleSheet(self.get_close_button_style())
self.close_button.clicked.connect(QApplication.quit)
# 添加设置和关闭按钮到底部布局
bottom_layout.addWidget(self.settings_button)
bottom_layout.addWidget(self.close_button)
# 将底部布局添加到主布局
main_layout.addLayout(bottom_layout)
self.setLayout(main_layout)
self.setFixedSize(170, 220) # 根据内容调整窗口大小
def get_opacity_button_style(self):
# 透明度按钮的样式
return """
QPushButton {
background-color: rgba(231, 76, 60, 0.8);
color: white;
border: none;
border-radius: 3px;
font-size: 16px;
}
QPushButton:pressed {
background-color: rgba(192, 57, 43, 0.8);
}
QPushButton:hover {
background-color: rgba(231, 76, 60, 1);
}
"""
def get_settings_button_style(self):
# 设置按钮的样式
return """
QPushButton {
background-color: rgba(155, 89, 182, 0.8);
color: white;
border: none;
border-radius: 3px;
font-size: 14px;
}
QPushButton:pressed {
background-color: rgba(142, 68, 173, 0.8);
}
QPushButton:hover {
background-color: rgba(155, 89, 182, 1);
}
"""
def get_close_button_style(self):
# 关闭按钮的样式
return """
QPushButton {
background-color: rgba(192, 57, 43, 0.8);
color: white;
border: none;
border-radius: 3px;
font-size: 14px;
}
QPushButton:pressed {
background-color: rgba(165, 42, 42, 0.8);
}
QPushButton:hover {
background-color: rgba(192, 57, 43, 1);
}
"""
def increase_opacity(self):
if self.opacity_level < 1.0:
self.opacity_level += 0.1
if self.opacity_level > 1.0:
self.opacity_level = 1.0
self.setWindowOpacity(self.opacity_level)
def decrease_opacity(self):
if self.opacity_level > 0.2:
self.opacity_level -= 0.1
if self.opacity_level < 0.2:
self.opacity_level = 0.2
self.setWindowOpacity(self.opacity_level)
def open_settings(self):
# 使用 up_button 的 timer_interval 作为当前间隔值
dialog = SettingsDialog(self.up_button.timer_interval, self)
if dialog.exec() == QDialog.Accepted:
new_interval = dialog.get_interval()
self.up_button.set_timer_interval(new_interval)
self.down_button.set_timer_interval(new_interval)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
# 检查点击的位置是否在按钮上
widget = self.childAt(event.position().toPoint())
if widget is None:
self.dragging = True
# 获取鼠标点击位置与窗口左上角的偏移量
if hasattr(event, 'globalPosition'):
self.drag_position = event.globalPosition().toPoint() - self.frameGeometry().topLeft()
else:
self.drag_position = event.globalPos() - self.frameGeometry().topLeft()
event.accept()
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton and self.dragging:
# 计算新的窗口位置
if hasattr(event, 'globalPosition'):
new_pos = event.globalPosition().toPoint() - self.drag_position
else:
new_pos = event.globalPos() - self.drag_position
self.move(new_pos)
event.accept()
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.dragging = False
event.accept()
super().mouseReleaseEvent(event)
# 重写 event 方法,确保窗口不被激活
def event(self, e):
if platform.system() == "Windows" and e.type() == 10: # QEvent.WindowActivate = 10
return False
return super().event(e)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = DraggableWindow()
window.show()
sys.exit(app.exec())