From 44e3ff5ec05ac36bcfd774cdf5c4cd66a90b21c6 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: Mon, 15 Jul 2024 06:53:49 +0800 Subject: [PATCH] wv2 --- .../LunaTranslator/gui/textbrowser.py | 3 ++ .../LunaTranslator/gui/translatorUI.py | 2 ++ .../LunaTranslator/gui/usefulwidget.py | 9 +++-- LunaTranslator/LunaTranslator/myutils/hwnd.py | 5 ++- .../LunaTranslator/rendertext/webview.py | 34 +++++++++++++++++-- LunaTranslator/LunaTranslator/windows.py | 18 ++++++++-- 6 files changed, 61 insertions(+), 10 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gui/textbrowser.py b/LunaTranslator/LunaTranslator/gui/textbrowser.py index 9305b38c..ed248de2 100644 --- a/LunaTranslator/LunaTranslator/gui/textbrowser.py +++ b/LunaTranslator/LunaTranslator/gui/textbrowser.py @@ -3,6 +3,7 @@ from myutils.config import globalconfig, _TR import importlib from webviewpy import webview_exception from gui.usefulwidget import getQMessageBox +from traceback import print_exc class Textbrowser(QLabel): @@ -34,6 +35,8 @@ class Textbrowser(QLabel): _TR("错误"), "can't find QWebEngine!", ) + else: + print_exc() globalconfig["rendertext_using"] = "textbrowser" tb = importlib.import_module(f"rendertext.textbrowser").TextBrowser self.textbrowser = tb(self) diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index 8819d3ce..6c87045d 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -44,6 +44,7 @@ class QUnFrameWindow(resizableframeless): muteprocessignal = pyqtSignal() ocr_once_signal = pyqtSignal() resizesignal = pyqtSignal(QSize) + move_signal=pyqtSignal(QPoint) def hookfollowsignalsolve(self, code, other): if self._move_drag: @@ -565,6 +566,7 @@ class QUnFrameWindow(resizableframeless): self.muteprocessignal.connect(self.muteprocessfuntion) self.toolbarhidedelaysignal.connect(self.toolbarhidedelay) self.resizesignal.connect(self.resize) + self.move_signal.connect(self.move) def __init__(self): diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index e09e541e..ead83c84 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -962,6 +962,11 @@ class WebivewWidget(abstractwebview): webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_BROWSER_CONTROLLER ) + def get_hwnd(self): + return self.webview.get_native_handle( + webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_UI_WIDGET + ) + def __init__(self, parent=None, debug=True) -> None: super().__init__(parent) declare_library_path( @@ -1013,9 +1018,7 @@ class WebivewWidget(abstractwebview): def resizeEvent(self, a0: QResizeEvent) -> None: if self.webview: - hwnd = self.webview.get_native_handle( - webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_UI_WIDGET - ) + hwnd = self.get_hwnd() size = a0.size() * self.devicePixelRatioF() windows.MoveWindow(hwnd, 0, 0, size.width(), size.height(), True) diff --git a/LunaTranslator/LunaTranslator/myutils/hwnd.py b/LunaTranslator/LunaTranslator/myutils/hwnd.py index 600a0a99..19c27555 100644 --- a/LunaTranslator/LunaTranslator/myutils/hwnd.py +++ b/LunaTranslator/LunaTranslator/myutils/hwnd.py @@ -213,9 +213,8 @@ def mouseselectwindow(callback): def _loop(): while True: - keystate = windows.GetKeyState( - windows.VK_LBUTTON - ) # 必须使用GetKeyState, GetAsyncKeyState或SetWindowHookEx都无法检测到高权限应用上的点击事件。 + keystate = windows.GetKeyState(windows.VK_LBUTTON) + # 必须使用GetKeyState, GetAsyncKeyState或SetWindowHookEx都无法检测到高权限应用上的点击事件。 if keystate < 0: break time.sleep(0.01) diff --git a/LunaTranslator/LunaTranslator/rendertext/webview.py b/LunaTranslator/LunaTranslator/rendertext/webview.py index b2d7ec0b..ec871fd4 100644 --- a/LunaTranslator/LunaTranslator/rendertext/webview.py +++ b/LunaTranslator/LunaTranslator/rendertext/webview.py @@ -1,9 +1,9 @@ from qtsymbols import * from rendertext.somefunctions import dataget -import gobject, uuid, json, os, functools +import gobject, uuid, json, os, functools, windows, time from urllib.parse import quote from myutils.config import globalconfig, static_data -from myutils.wrapper import tryprint +from myutils.wrapper import tryprint, threader from gui.usefulwidget import WebivewWidget, QWebWrap testsavejs = False @@ -48,6 +48,15 @@ class TextBrowser(QWidget, dataget): # webview2当会执行alert之类的弹窗js时,若qt窗口不可视,会卡住 self.webivewwidget = WebivewWidget(self) + # webview2无法接收qt事件。 + webviewhwnd = self.webivewwidget.get_hwnd() + self.wndproc = windows.WNDPROCTYPE( + functools.partial( + self.extrahandle, + windows.GetWindowLongPtr(webviewhwnd, windows.GWLP_WNDPROC), + ) + ) + windows.SetWindowLongPtr(webviewhwnd, windows.GWLP_WNDPROC, self.wndproc) self.masklabel_left = QLabel(self) self.masklabel_left.setMouseTracking(True) # self.masklabel_left.setStyleSheet('background-color:red') @@ -72,6 +81,27 @@ class TextBrowser(QWidget, dataget): self.isfirst = True self._qweb_query_word() + @threader + def trackingthread(self): + pos = gobject.baseobject.translation_ui.pos() + cus = QCursor.pos() + while True: + keystate = windows.GetKeyState(windows.VK_LBUTTON) + if keystate >= 0: + break + gobject.baseobject.translation_ui.move_signal.emit( + pos + QCursor.pos() - cus + ) + time.sleep(0.01) + + def extrahandle(self, orig, hwnd, msg, wp, lp): + if wp == windows.WM_LBUTTONDOWN: + # 因为有父窗口,所以msg是WM_PARENTNOTIFY,wp才是WM_LBUTTONDOWN + # 而且SetCapture后会立即被父窗口把capture夺走,无法后面的释放&移动,所以只能开个线程来弄 + if self.masklabel.isVisible(): + self.trackingthread() + return windows.WNDPROCTYPE(orig)(hwnd, msg, wp, lp) + @tryprint def showEvent(self, e): if not self.isfirst: diff --git a/LunaTranslator/LunaTranslator/windows.py b/LunaTranslator/LunaTranslator/windows.py index 410941ba..423ece0e 100644 --- a/LunaTranslator/LunaTranslator/windows.py +++ b/LunaTranslator/LunaTranslator/windows.py @@ -21,7 +21,6 @@ from ctypes import ( cast, ) import os -from traceback import print_exc from ctypes.wintypes import ( RECT, POINT, @@ -30,10 +29,12 @@ from ctypes.wintypes import ( WORD, DWORD, PHKEY, - BYTE, HKEY, LPDWORD, LPBYTE, + WPARAM, + LPARAM, + INT, LPCWSTR, HANDLE, UINT, @@ -1022,3 +1023,16 @@ def MapViewOfFile(hfmap, acc, high, low, size): IsZoomed = _user32.IsZoomed IsZoomed.argtypes = (HWND,) IsZoomed.restype = BOOL + +WNDPROCTYPE = WINFUNCTYPE(INT, HWND, INT, WPARAM, LPARAM) + +GWLP_WNDPROC = -4 +SetWindowLongPtr = _user32.SetWindowLongPtrW +SetWindowLongPtr.argtypes = HWND, INT, c_void_p +SetWindowLongPtr.restype = c_void_p +GetWindowLongPtr = _user32.GetWindowLongPtrW +GetWindowLongPtr.argtypes = HWND, INT +GetWindowLongPtr.restype = c_void_p +WM_LBUTTONDOWN = 0x0201 +WM_LBUTTONUP = 0x0202 +WM_MOUSEMOVE = 0x0200