#coding=utf-8
# Alan(Chaofan Zhang)
# gagazcfan@gmail.com
# http://weibo.com/alanslab
# https://gitcafe.com/activities
# https://github.com/zcfan
importsys, math, win32gui, win32apifrom PyQt4 importQtGui, QtCorefrom PyQt4.QtCore importQtclassTrans(QtGui.QWidget):#status code
STATUS_STARTPOINT =0
STATUS_ENDPOINT= 1STATUS_ESC= 2BALL_SIZE= 25
def __init__(self):
super(Trans, self).__init__()
self.startpoint=()
self.endpoint=()#self.targetpoint =()
self.status =0
self.initUI()definitUI(self):#make a unvisable fullscreen window
self.setWindowOpacity(0.01)
self.setWindowFlags(Qt.FramelessWindowHint)
desktop=QtGui.QApplication.desktop()
rect=desktop.availableGeometry()
self.setGeometry(rect)#set the style of mouse cursor
mcursor = QtGui.QCursor(QtGui.QPixmap("./cursor.png"))
self.setCursor(mcursor)#not using anymore## def paintEvent(self, event):## paint = QtGui.QPainter()## paint.begin(self)#### if len(self.startpoint) == 2:## self.printStartpoint(paint)## if len(self.endpoint) == 2:## self.printEndpoint(paint)## self.printLine(paint)## #self.printTargetpoint(paint)#### paint.end()#### def printStartpoint(self, paint):## paint.setPen(Qt.red)## paint.drawPoint(self.startpoint[0], self.startpoint[1])#### def printEndpoint(self, paint):## paint.setPen(Qt.red)## paint.drawPoint(self.endpoint[0], self.endpoint[1])#### def printLine(self, paint):## pen = QtGui.QPen(Qt.green)## paint.setPen(pen)## paint.drawLine(self.startpoint[0], self.startpoint[1],## self.endpoint[0], self.endpoint[1])
#def printTargetpoint(self, paint):
defmousePressEvent(self, event):if event.button() ==Qt.LeftButton:if self.status ==self.STATUS_STARTPOINT:
self.status=self.STATUS_ENDPOINT
self.endpoint=()
self.startpoint=(event.x(), event.y())
self.update()elif self.status ==self.STATUS_ENDPOINT:
self.status=self.STATUS_ESC
self.endpoint=(event.x(), event.y())
lenX= self.endpoint[0]-self.startpoint[0]
lenY= self.endpoint[1]-self.startpoint[1]
tmpX= int(round(self.BALL_SIZE * (lenX /math.sqrt(
math.pow(lenX,2) + math.pow(lenY, 2)))))
tmpY= int(round(self.BALL_SIZE * (lenY /math.sqrt(
math.pow(lenX,2) + math.pow(lenY, 2)))))#move mouse cursor to the right position
win32api.SetCursorPos((win32gui.GetCursorPos()[0] +tmpX,
win32gui.GetCursorPos()[1] +tmpY))
self.targetpoint= (self.endpoint[0] +tmpX,
self.endpoint[1] +tmpY)
self.update()elif self.status ==self.STATUS_ESC:
self.close()if __name__ == '__main__':
app=QtGui.QApplication(sys.argv)
trans=Trans()
trans.show()
sys.exit(app.exec_())