1 使用QT API获取鼠标位置
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import socket
import time
import sys
from threading import Thread,Lock
LCD_W = 240
LCD_H = 240
LCD_R = 0
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("192.168.3.80",1347))
x = 0
y = 0
pressed = False
class MySignals(QObject):
mouse_update = pyqtSignal([int,int],[int,int,int])
mouse_single = MySignals()
def task():
bankLock = Lock()
def update_mouse(posx, posy):
bankLock.acquire()
global x, y
x = posx
y = posy
bankLock.release()
def update_mouse_pressed(posx, posy, state):
bankLock.acquire()
global x, y, pressed
x = posx
y = posy
pressed = state
bankLock.release()
mouse_single.mouse_update[int,int].connect(update_mouse)
mouse_single.mouse_update[int,int,int].connect(update_mouse_pressed)
def threadFunc():
global x, y
client_isConnected = True
while True:
try:
if ( x >= 0 and x <= LCD_W ) and ( y >= 0 and y <= LCD_H ):
send_data = ("{\"point\":[%d,%d],\"state\":\"%d\"}\n"%(x,y,pressed)).encode("utf-8")
print(send_data)
client.send(send_data)
except:
client_isConnected = False
if client_isConnected == False:
try:
client.close()
client.connect(("192.168.3.80",1347))
client_isConnected = True
except:
client_isConnected = False
time.sleep(0.02)
thread = Thread(target = threadFunc, daemon=True)
thread.start()
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.resize(LCD_W, LCD_H)
self.setFixedSize(QSize(LCD_W, LCD_H))
self.setWindowOpacity(0.6)
self.setWindowTitle('Touch')
self.setWindowIcon(QIcon('./images/boy.png'))
self.pix = QPixmap()
self.lastPoint = QPoint()
self.endPoint = QPoint()
self.pix = QPixmap(LCD_W, LCD_H)
self.pix.fill(Qt.white)
self.mouse_x = 0
self.mouse_y = 0
self.mouse_pressed = 0
self.label_mouse_x = QLabel(self)
self.label_mouse_x.setGeometry(190, 5, 80, 30)
self.label_mouse_x.setText('x:0')
self.label_mouse_x.setMouseTracking(True)
self.label_mouse_y = QLabel(self)
self.label_mouse_y.setText('y:0')
self.label_mouse_y.setGeometry(190, 40, 80, 30)
self.label_mouse_y.setMouseTracking(True)
def paintEvent(self, event):
paint = QPainter(self.pix)
paint.setPen(QPen(QColor(0, 160, 230), 1))
paint.drawLine(LCD_W//4*0, 0, LCD_W//4*0, LCD_H)
paint.drawLine(LCD_W//4*1, 0, LCD_W//4*1, LCD_H)
paint.drawLine(LCD_W//4*2, 0, LCD_W//4*2, LCD_H)
paint.drawLine(LCD_W//4*3, 0, LCD_W//4*3, LCD_H)
paint.drawLine(LCD_W//4*4 - 1, 0, LCD_W//4*4 - 1, LCD_H)
paint.drawLine(0, LCD_H//4*0, LCD_W, LCD_H//4*0)
paint.drawLine(0, LCD_H//4*1, LCD_W, LCD_H//4*1)
paint.drawLine(0, LCD_H//4*2, LCD_W, LCD_H//4*2)
paint.drawLine(0, LCD_H//4*3, LCD_W, LCD_H//4*3)
paint.drawLine(0, LCD_H//4*4 - 1, LCD_W, LCD_H//4*4 - 1)
if(self.mouse_pressed == True):
if(self.lastPoint == self.endPoint):
paint.setPen(QPen(QColor(255, 0, 0), 8))
paint.drawPoint(self.endPoint)
else:
paint.setPen(QPen(QColor(255, 0, 0), 1))
paint.drawLine(self.lastPoint, self.endPoint)
else:
pass
self.lastPoint = self.endPoint
painter = QPainter(self)
painter.drawPixmap(0, 0, self.pix)
def mousePressEvent(self, event):
s = event.windowPos()
self.setMouseTracking(True)
self.mouse_x = int(s.x())
self.mouse_y = int(s.y())
if ( self.mouse_x >= 0 and self.mouse_x <= LCD_W ) and ( self.mouse_y >= 0 and self.mouse_y <= LCD_H ):
self.label_mouse_x.setText('X:' + str(int(self.mouse_x)))
self.label_mouse_y.setText('Y:' + str(int(self.mouse_y)))
self._ActiveButton = event.button()
if self._ActiveButton == Qt.LeftButton:
self.pix.fill(Qt.white)
mouse_single.mouse_update[int,int,int].emit(self.mouse_x, self.mouse_y, 1)
self.endPoint = event.pos()
self.lastPoint = self.endPoint
self.mouse_pressed = True
self.update()
elif self._ActiveButton == Qt.MiddleButton:
self.pix.fill(Qt.white)
self.endPoint = event.pos()
self.lastPoint = self.endPoint
self.mouse_pressed = False
self.update()
def mouseMoveEvent(self, event):
s = event.windowPos()
self.setMouseTracking(True)
self.label_mouse_x.setText('X:' + str(int(s.x())))
self.label_mouse_y.setText('Y:' + str(int(s.y())))
self.mouse_x = int(s.x())
self.mouse_y = int(s.y())
mouse_single.mouse_update[int,int].emit(self.mouse_x, self.mouse_y)
if event.buttons() and Qt.LeftButton:
self.endPoint = event.pos()
mouse_single.mouse_update[int,int,int].emit(self.mouse_x, self.mouse_y, 1)
self.update()
else:
mouse_single.mouse_update[int,int,int].emit(self.mouse_x, self.mouse_y, 0)
self.endPoint = self.lastPoint
self.update()
def mouseReleaseEvent(self, event):
s = event.windowPos()
self.setMouseTracking(True)
self.label_mouse_x.setText('X:' + str(int(s.x())))
self.label_mouse_y.setText('Y:' + str(int(s.y())))
self.mouse_x = int(s.x())
self.mouse_y = int(s.y())
self._ActiveButton = event.button()
if self._ActiveButton == Qt.LeftButton:
self.mouse_pressed = False
mouse_single.mouse_update[int,int,int].emit(self.mouse_x, self.mouse_y, 0)
self.pix.fill(Qt.white)
self.endPoint = self.lastPoint
self.update()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
win.show()
task()
sys.exit(app.exec_())
2 使用Win API获取鼠标位置
from PyQt5.QtCore import QThread, pyqtSignal, QObject
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtGui import QPainter, QPixmap, QColor, QPen
from PyQt5.QtCore import Qt, QPoint, QSize
import socket
import time
import sys
import qdarkstyle
import win32gui
import win32api
LCD_W = 240
LCD_H = 240
LCD_R = 0
def findTitle(window_title):
'''
查找指定标题窗口句柄
@param window_title: 标题名
@return: 窗口句柄
'''
hWndList = []
win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)
for hwnd in hWndList:
title = win32gui.GetWindowText(hwnd)
if (title == window_title):
print("标题:", title, "句柄:", hwnd)
break
return hwnd
class BackendThread(QObject):
update_date = pyqtSignal(str)
def run(self):
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("192.168.3.80",1347))
window_title = u'ESP32-Touch'
hwnd = findTitle(window_title)
print(hwnd)
x,y,w,h = win32gui.GetWindowRect(hwnd)
print(x,y,w,h)
while True:
p = win32api.GetCursorPos()
x,y,w,h = win32gui.GetWindowRect(hwnd)
pos_x = p[0] - x - 5
pos_y = p[1] - y - 30
if ( pos_x >= 0 and pos_x <= LCD_W ) and ( pos_y >= 0 and pos_y <= LCD_H ):
print(pos_x,pos_y)
client.send((str(pos_x)+","+str(pos_y)+"\n").encode("utf-8"))
time.sleep(0.02)
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle('ESP32-Touch')
self.setWindowIcon(QIcon('./images/tiantiansifangmao.png'))
self.resize(LCD_W, LCD_H)
self.setFixedSize(QSize(LCD_W, LCD_H))
self.setWindowOpacity(0.3)
self.pix = QPixmap()
self.lastPoint = QPoint()
self.endPoint = QPoint()
self.pix = QPixmap(LCD_W, LCD_H)
self.pix.fill(Qt.white)
self.initUI()
def initUI(self):
self.backend = BackendThread()
self.backend.update_date.connect(self.handleDisplay)
self.thread = QThread()
self.backend.moveToThread(self.thread)
self.thread.started.connect(self.backend.run)
self.thread.start()
def paintEvent(self, event):
paint = QPainter(self.pix)
paint.setPen(QPen(QColor(0, 160, 230), 1))
paint.drawLine(LCD_W//4*1, 0, LCD_W//4*1, LCD_H)
paint.drawLine(LCD_W//4*2, 0, LCD_W//4*2, LCD_H)
paint.drawLine(LCD_W//4*3, 0, LCD_W//4*3, LCD_H)
paint.drawLine(0, LCD_H//4*1, LCD_W, LCD_H//4*1)
paint.drawLine(0, LCD_H//4*2, LCD_W, LCD_H//4*2)
paint.drawLine(0, LCD_H//4*3, LCD_W, LCD_H//4*3)
paint.setPen(QPen(QColor(255, 0, 0), 2))
paint.drawLine(self.lastPoint, self.endPoint)
self.lastPoint = self.endPoint
painter = QPainter(self)
painter.drawPixmap(0, 0, self.pix)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.lastPoint = event.pos()
def mouseMoveEvent(self, event):
if event.buttons() and Qt.LeftButton:
self.endPoint = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.endPoint = event.pos()
self.pix.fill(Qt.white)
self.update()
def handleDisplay(self, data):
self.input.setText(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
|