1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QTextEdit, QTextBrowser, QHBoxLayout, QVBoxLayout
from PyQt5 import QtCore, QtWidgets, QtWidgets
import win32gui,win32con,win32api
from win32gui import *
from dlx import *
class SpyLabel(QtWidgets.QLabel):
def __init__(self, parent = None):
QtWidgets.QLabel.__init__(self, parent)
self.parent = parent
self.spying = False
self.rectanglePen = win32gui.CreatePen(win32con.PS_SOLID, 3, win32api.RGB(255, 0, 0))
self.prevWindow = None
self.setCursor(QtCore.Qt.SizeAllCursor)
def mouseMoveEvent(self, event):
if self.spying:
curX, curY = win32gui.GetCursorPos()
hwnd = win32gui.WindowFromPoint((curX, curY))
#if self.checkWindowValidity(hwnd):
if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
if self.prevWindow:
self.refreshWindow(self.prevWindow)
self.prevWindow = hwnd
self.highlightWindow(hwnd)
self.displayWindowInformation(hwnd)
def mouseReleaseEvent(self, event):
if self.spying:
if self.prevWindow:
self.refreshWindow(self.prevWindow)
win32gui.ReleaseCapture()
self.spying = False
def mousePressEvent(self,event):
if event.button() == QtCore.Qt.LeftButton:
self.spying = True
def highlightWindow(self, hwnd):
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
windowDc = win32gui.GetWindowDC(hwnd)
if windowDc:
prevPen = win32gui.SelectObject(windowDc, self.rectanglePen)
prevBrush = win32gui.SelectObject(windowDc, win32gui.GetStockObject(win32con.HOLLOW_BRUSH))
win32gui.Rectangle(windowDc, 0, 0, right - left, bottom - top)
win32gui.SelectObject(windowDc, prevPen)
win32gui.SelectObject(windowDc, prevBrush)
win32gui.ReleaseDC(hwnd, windowDc)
def refreshWindow(self, hwnd):
win32gui.InvalidateRect(hwnd, None, True)
win32gui.UpdateWindow(hwnd)
win32gui.RedrawWindow(hwnd,
None,
None,
win32con.RDW_FRAME|
win32con.RDW_INVALIDATE|
win32con.RDW_UPDATENOW|
win32con.RDW_ALLCHILDREN)
def displayWindowInformation(self, hwnd):
className = win32gui.GetClassName(hwnd)
buf_size = 1 + win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0)
buffer = win32gui.PyMakeBuffer(buf_size)
win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buffer)
windowText = buffer[:buf_size]
try:
windowText = unicode(windowText, 'gbk')
except:
pass
windowText=win32gui.GetWindowText(hwnd)
message = ['Handle:\t' + str(hwnd),
'Class Name:\t' + className,
'Window Text:\t' + windowText,
]
#self.output('\r\n'.join(message))
print(message)
self.parent.text_browser.setText('\r\n'.join(message)) # 2
class pyspy(QWidget):
def __init__(self):
super(pyspy, self).__init__()
self.browser_label = SpyLabel(self)
self.text_browser = QTextBrowser(self)
self.browser_v_layout = QVBoxLayout()
self.all_h_layout = QHBoxLayout()
self.layout_init()
def layout_init(self):
self.browser_v_layout.addWidget(self.browser_label)
self.browser_v_layout.addWidget(self.text_browser)
self.all_h_layout.addLayout(self.browser_v_layout)
self.setLayout(self.all_h_layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = pyspy()
window.setGeometry(1200, 600, 300, 300)
window.show()
sys.exit(app.exec_()) |