-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyspy.py
129 lines (104 loc) · 4.4 KB
/
pyspy.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
#coding:utf-8
#
# Copyright 2009 CoderZh.com.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = 'CoderZh'
import win32gui
import win32con
import win32api
from ui_pyspy import Ui_SpyDialog
from PyQt4 import QtCore, QtGui
class SpyLabel(QtGui.QLabel):
def __init__(self, parent = None):
QtGui.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 output(self, message):
self.parent.output(message)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.spying = True
def mouseMoveEvent(self, event):
if self.spying:
curX, curY = win32gui.GetCursorPos()
hwnd = win32gui.WindowFromPoint((curX, curY))
if self.checkWindowValidity(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 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 checkWindowValidity(self, hwnd):
if not hwnd:
return False
if not win32gui.IsWindow(hwnd):
return False
if self.prevWindow == hwnd:
return False
if self.parent == hwnd:
return False
return True
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
message = ['Handle:\t' + str(hwnd),
'Class Name:\t' + className,
'Window Text:\t' + windowText]
self.output('\r\n'.join(message))
class SpyDialog(QtGui.QDialog, Ui_SpyDialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
self.spyLabel = SpyLabel(self)
self.spyLabel.setGeometry(QtCore.QRect(170, 20, 41, 41))
self.spyLabel.setPixmap(QtGui.QPixmap(":/res/finderf.bmp"))
self.spyLabel.setObjectName("spyLabel")
def output(self, message):
self.textEditInformation.setText(message)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
dlg = SpyDialog()
dlg.show()
sys.exit(app.exec_())