From 1926586d0f4560cc9c4cc1b02e5578de4ab0e04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Sun, 26 May 2024 20:53:10 +0800 Subject: [PATCH] multi screen --- .../LunaTranslator/gui/rangeselect.py | 53 +++++++++++++++---- .../LunaTranslator/myutils/ocrutil.py | 19 +++++-- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gui/rangeselect.py b/LunaTranslator/LunaTranslator/gui/rangeselect.py index cb70659c..8b356e35 100644 --- a/LunaTranslator/LunaTranslator/gui/rangeselect.py +++ b/LunaTranslator/LunaTranslator/gui/rangeselect.py @@ -1,4 +1,12 @@ -from PyQt5.QtWidgets import QMenu, QApplication, QMainWindow, QLabel, QAction, QDialog +from PyQt5.QtWidgets import ( + QMenu, + QApplication, + QMainWindow, + QLabel, + QAction, + QDialog, + QDesktopWidget, +) from PyQt5.QtGui import QPainter, QPen, QColor from PyQt5.QtCore import Qt, QPoint, QRect, QEvent from myutils.config import _TR @@ -69,6 +77,19 @@ class rangeadjust(Mainw): ), ] + def setGeometry(self, x, y, w, h): + if QDesktopWidget().screenCount() > 1: + windows.MoveWindow(int(self.winId()), x, y, w, h, True) + else: + super().setGeometry(x, y, w, h) + + def geometry(self): + if QDesktopWidget().screenCount() > 1: + rect = windows.GetWindowRect(int(self.winId())) + return QRect(rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1]) + else: + return super().geometry() + def moveEvent(self, e): if self._rect: self._rect = self.rectoffset(self.geometry()) @@ -125,6 +146,8 @@ class rangeselct(QMainWindow): self.setMouseTracking(True) self.start_point = QPoint() self.end_point = QPoint() + self.__start = None + self.__end = None self.startauto = False self.clickrelease = False self.rectlabel.setStyleSheet( @@ -172,27 +195,37 @@ class rangeselct(QMainWindow): self.start_point = event.pos() self.end_point = self.start_point self.is_drawing = True + self.__start = self.__end = windows.GetCursorPos() def mouseMoveEvent(self, event): if self.startauto and self.is_drawing == False: self.is_drawing = True self.end_point = self.start_point = event.pos() + self.__start = self.__end = windows.GetCursorPos() self.startauto = False if self.is_drawing: self.end_point = event.pos() + self.__end = windows.GetCursorPos() self.update() def getRange(self): - start_point = self.mapToGlobal(self.start_point) - end_point = self.mapToGlobal(self.end_point) - x1, y1, x2, y2 = ( - start_point.x(), - start_point.y(), - end_point.x(), - end_point.y(), - ) - + if QDesktopWidget().screenCount() > 1: + x1, y1, x2, y2 = ( + self.__start.x, + self.__start.y, + self.__end.x, + self.__end.y, + ) + else: + start_point = self.mapToGlobal(self.start_point) + end_point = self.mapToGlobal(self.end_point) + x1, y1, x2, y2 = ( + start_point.x(), + start_point.y(), + end_point.x(), + end_point.y(), + ) x1, x2 = min(x1, x2), max(x1, x2) y1, y2 = min(y1, y2), max(y1, y2) diff --git a/LunaTranslator/LunaTranslator/myutils/ocrutil.py b/LunaTranslator/LunaTranslator/myutils/ocrutil.py index 85ae6c48..9afa7c1d 100644 --- a/LunaTranslator/LunaTranslator/myutils/ocrutil.py +++ b/LunaTranslator/LunaTranslator/myutils/ocrutil.py @@ -1,7 +1,7 @@ import windows import os, importlib from myutils.config import globalconfig, _TR -from PyQt5.QtWidgets import QApplication +from PyQt5.QtWidgets import QApplication, QDesktopWidget from PyQt5.QtGui import QImage from PyQt5.QtCore import QByteArray, QBuffer from myutils.commonbase import ArgsEmptyExc @@ -83,9 +83,20 @@ def imageCut(hwnd, x1, y1, x2, y2, viscompare=True, rawimage=False) -> QImage: except: print_exc() else: - pix = screen.grabWindow( - QApplication.desktop().winId(), x1, y1, x2 - x1, y2 - y1 - ) + if QDesktopWidget().screenCount() > 1: + desktop = QApplication.primaryScreen().virtualGeometry() + pix = screen.grabWindow( + QApplication.desktop().winId(), + desktop.x(), + desktop.y(), + desktop.width(), + desktop.height(), + ) + pix = pix.copy(x1, y1, x2 - x1, y2 - y1) + else: + pix = screen.grabWindow( + QApplication.desktop().winId(), x1, y1, x2 - x1, y2 - y1 + ) image = pix.toImage() if rawimage: