gpt4 book ai didi

python - PyQt5中有没有办法让鼠标事件完全忽略窗口?

转载 作者:行者123 更新时间:2023-12-02 19:05:05 25 4
gpt4 key购买 nike

我尝试过使用setAttribute(Qt.Qt.WA_TransparentForMouseEvents),但鼠标也无法穿透Qtwindow。

我想让鼠标事件穿透Qtwindow,就像我在位于Windows10桌面的Qtwindow上单击鼠标右键,然后它会触发win10上下文菜单。

最佳答案

透明窗口适合您的需求吗?

from PyQt5 import QtCore, QtWidgets, QtGui


class Overlay(QtWidgets.QWidget):

def __init__(self):
super().__init__()
self.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
layout = QtWidgets.QHBoxLayout(self)
label = QtWidgets.QLabel('Transparent and propagating')
label.setFont(QtGui.QFont('Arial', 26))
label.setStyleSheet("background-color : white")
layout.addWidget(label)
self.show()


if __name__ == '__main__':
app = QtWidgets.QApplication([])
form = Overlay()
app.exec_()

我试图找到一种将点击直接传输到桌面的方法。 closest related question给了我一些想法,但最终我无法让它工作,点击永远不会到达桌面。也许你仍然可以从中得到一些想法:

from PyQt5 import QtWidgets, QtGui
import win32api, win32con
from ctypes import windll


class Overlay(QtWidgets.QWidget):

def __init__(self):
super().__init__()
layout = QtWidgets.QHBoxLayout(self)
label = QtWidgets.QLabel('Click to Desktop')
label.setFont(QtGui.QFont('Arial', 26))
label.setStyleSheet("background-color : white")
layout.addWidget(label)
# make window partially transparent to see where you are clicking
self.setWindowOpacity(0.5)
# get handle to desktop as described in linked question
hProgman = windll.User32.FindWindowW("Progman", 0)
hFolder = windll.User32.FindWindowExW(hProgman, 0, "SHELLDLL_DefView", 0)
self.desktop = windll.User32.FindWindowExW(hFolder, 0, "SysListView32", 0)
self.show()

def mousePressEvent(self, event):
# catch mouse event to route it to desktop
x = event.globalPos().x()
y = event.globalPos().y()
lParam = win32api.MAKELONG(x, y)
# left click on desktop (left button down + up, => should be replaced by event.button() pseudo switch case once working)
windll.User32.SendInput(self.desktop, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
windll.User32.SendInput(self.desktop, win32con.WM_LBUTTONUP, 0, lParam)
# display position for debugging (position gets displayed, but nothing gets clicked)
print(f'clicked on desktop at position {x} and {y}')


if __name__ == '__main__':
app = QtWidgets.QApplication([])
form = Overlay()
app.exec_()

关于python - PyQt5中有没有办法让鼠标事件完全忽略窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65167075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com