ctypes: How to get the unscaled position of the mouse cursor? Disable Dpi scaling

ctypes: How to get the unscaled position of the mouse cursor? Disable Dpi scaling

from ctypes import windll, Structure, c_long, byref

SetProcessDpiAwarenessContext = windll.user32.SetProcessDpiAwarenessContext
SetProcessDpiAwarenessContext.restype = wintypes.BOOL
SetProcessDpiAwarenessContext.argtypes = [
    wintypes.HANDLE,
]

SetProcessDpiAwarenessContext(wintypes.HANDLE(-1))

pywinauto/pywinauto/windows/win32functions.py / https://github.com/pywinauto/pywinauto/blob/547e4e40b9043115fb008973d367bce7253e8e7e/pywinauto/windows/win32functions.py

Example:

from PyQt6.QtCore import *
from PyQt6.QtCore import pyqtSlot as Slot
from PyQt6.QtCore import pyqtSignal as Signal
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
from PyQt6.QtWebEngineWidgets import *
from PyQt6.QtWebEngineCore import *

from ctypes import windll
from ctypes import wintypes
import sys
import threading
import math
import datetime
# import WindowsKeyHook

import ctypes,win32api



from ctypes import windll, Structure, c_long, byref

SetProcessDpiAwarenessContext = windll.user32.SetProcessDpiAwarenessContext
SetProcessDpiAwarenessContext.restype = wintypes.BOOL
SetProcessDpiAwarenessContext.argtypes = [
    wintypes.HANDLE,
]

SetProcessDpiAwarenessContext(wintypes.HANDLE(-1))

class POINT(Structure):
    _fields_ = [("x", c_long), ("y", c_long)]

def MousePosition_ctypes():
    pos = POINT()
    windll.user32.GetCursorPos(byref(pos))
    return (pos.x,pos.y)

class WebEngineView(QWebEngineView):  #

    def __init__(self, parent=None):
        super().__init__(parent)
        self.webpage = QWebEnginePage()
        # self.webpage.javaScriptConsoleMessage = lambda level, message, lineNumber, sourceID: print(message)  # if level > QWebEnginePage.WarningMessageLevel else None  # 关闭js console msg,不起作用→self.javaScriptConsoleMessage = None;https://doc.qt.io/qt-5/qwebenginepage.html#JavaScriptConsoleMessageLevel-enum
        self.setPage(self.webpage)
        self.webpage.load(QUrl('https://dict.eudic.net/liju/en/good#TingLiju'))
        # desktop = QGuiApplication.primaryScreen()
        # print(desktop.devicePixelRatio())

        self.mouseMoveCheckTimer = QTimer()
        self.mouseMoveCheckTimer.timeout.connect(self.checkMousePos)  # showTranslation
        self.mouseMoveCheckTimer.start(1000)  # 不能在3方mouse thread里启动,所以这样了,但是也好,方便后面鼠标悬停计时取词



    def checkMousePos(self):  # 其实就是想获得text got pos
        print(QCursor.pos(),MousePosition_ctypes(),win32api.GetCursorPos())
        

if __name__ == "__main__":

    app = QApplication(sys.argv)
    webEngineView = WebEngineView()
    webEngineView.show()
    sys.exit(app.exec())

Comments