探测窗口和放大截图

This commit is contained in:
Irony 2018-06-08 23:13:35 +08:00
parent e2ff919129
commit ceaca3a67b
5 changed files with 129 additions and 0 deletions

View file

@ -49,6 +49,7 @@ encoding//\u5B57\u4F53\u6D4B\u8BD5/FontAwesome.py=utf-8
encoding//\u5B57\u4F53\u6D4B\u8BD5/TestFontAwesome.py=utf-8 encoding//\u5B57\u4F53\u6D4B\u8BD5/TestFontAwesome.py=utf-8
encoding//\u5B57\u4F53\u6D4B\u8BD5/TestFontRoboto.py=utf-8 encoding//\u5B57\u4F53\u6D4B\u8BD5/TestFontRoboto.py=utf-8
encoding//\u5DE6\u4FA7\u9009\u9879\u5361/LeftTabWidget.py=utf-8 encoding//\u5DE6\u4FA7\u9009\u9879\u5361/LeftTabWidget.py=utf-8
encoding//\u63A2\u6D4B\u7A97\u53E3\u548C\u653E\u5927\u622A\u56FE/\u7B80\u5355\u63A2\u6D4B\u7A97\u53E3\u548C\u653E\u5927\u622A\u56FE.py=utf-8
encoding//\u6570\u636E\u5E93\u67E5\u8BE2\u663E\u793A\u8868\u683C/main.py=utf-8 encoding//\u6570\u636E\u5E93\u67E5\u8BE2\u663E\u793A\u8868\u683C/main.py=utf-8
encoding//\u6570\u636E\u5E93\u67E5\u8BE2\u663E\u793A\u8868\u683C/mainui.py=utf-8 encoding//\u6570\u636E\u5E93\u67E5\u8BE2\u663E\u793A\u8868\u683C/mainui.py=utf-8
encoding//\u65E0\u8FB9\u6846\u81EA\u5B9A\u4E49\u6807\u9898\u680F\u7A97\u53E3/FramelessWindow.py=utf-8 encoding//\u65E0\u8FB9\u6846\u81EA\u5B9A\u4E49\u6807\u9898\u680F\u7A97\u53E3/FramelessWindow.py=utf-8

View file

@ -41,6 +41,7 @@
- [1.28.3 pydext](C和C++扩展/pydext/) - [1.28.3 pydext](C和C++扩展/pydext/)
- [1.29 数据库查询显示表格](数据库查询显示表格/) - [1.29 数据库查询显示表格](数据库查询显示表格/)
- [1.30 左侧选项卡](左侧选项卡/) - [1.30 左侧选项卡](左侧选项卡/)
- [1.40 探测窗口和放大截图](探测窗口和放大截图/)
### [2.QGraphicsView练习](QGraphicsView练习/) ### [2.QGraphicsView练习](QGraphicsView练习/)

View file

@ -0,0 +1,7 @@
# 简单探测窗口和放大截图
利用win32gui模块获取鼠标所在位置的窗口大小(未去掉边框)和rgb颜色, 主要思路是利用一个全屏的全透明鼠标穿透的窗口(目的在于绘制矩形框和截图)
截图
![1](ScreenShot/1.gif)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

View file

@ -0,0 +1,120 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QPainter, QPen, QCursor, QColor
from PyQt5.QtWidgets import QLabel, QWidget, QApplication
import win32gui
# Created on 2018年6月8日
# author: Irony
# site: https://github.com/892768447
# email: 892768447@qq.com
# file: 简单探测窗口和放大截图
# description:
__Author__ = """By: Irony
QQ: 892768447
Email: 892768447@qq.com"""
__Copyright__ = 'Copyright (c) 2018 Irony'
__Version__ = 1.0
class FrameWidget(QWidget):
# 一个全屏的透明窗口
def __init__(self, *args, **kwargs):
super(FrameWidget, self).__init__(*args, **kwargs)
self.setWindowFlags(Qt.Tool | Qt.FramelessWindowHint |
Qt.WindowStaysOnTopHint)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.showFullScreen() # 全屏
self._rect = QRect() # 被探测的窗口的矩形位置
def setRect(self, x, y, w, h):
# 更新矩形框
self._rect.setX(x)
self._rect.setY(y)
self._rect.setWidth(w - x)
self._rect.setHeight(h - y)
self.update()
def paintEvent(self, event):
super(FrameWidget, self).paintEvent(event)
if self._rect.isValid(): # 画边框
painter = QPainter(self)
painter.setPen(QPen(Qt.red, 4))
painter.drawRect(self._rect)
class Label(QLabel):
def __init__(self, *args, **kwargs):
super(Label, self).__init__(*args, **kwargs)
self.ismd = False # 是不是否按下
self.setAlignment(Qt.AlignCenter)
self.setText('鼠标按住不放拖动到外面')
self.resize(240, 240)
self.frameWidget = FrameWidget() # 边框
def closeEvent(self, event):
self.frameWidget.close()
super(Label, self).closeEvent(event)
def mousePressEvent(self, event):
super(Label, self).mousePressEvent(event)
self.ismd = True # 按下
# 设置鼠标样式为十字
self.setCursor(Qt.CrossCursor)
def mouseReleaseEvent(self, event):
super(Label, self).mouseReleaseEvent(event)
self.ismd = False
self.frameWidget.setRect(0, 0, 0, 0)
# 设置鼠标样式为普通
self.setCursor(Qt.ArrowCursor)
self.clear()
self.setText('鼠标按住不放拖动到外面')
def mouseMoveEvent(self, event):
super(Label, self).mouseMoveEvent(event)
# 得到鼠标在屏幕中的位置
pos = self.mapToGlobal(event.pos())
hwnd = win32gui.WindowFromPoint((pos.x(), pos.y()))
self.frameWidget.setRect(*win32gui.GetWindowRect(hwnd))
# 截图
screen = QApplication.primaryScreen()
if screen is not None:
image = screen.grabWindow(0,
pos.x() - 60, pos.y() - 60, 120, 120)
if not image.isNull():
self.setPixmap(image.scaled(240, 240))
def paintEvent(self, event):
super(Label, self).paintEvent(event)
# 中正间画十字
painter = QPainter(self)
painter.setPen(Qt.red)
x = int(self.width() / 2)
y = int(self.height() / 2)
painter.drawLine(x, 0, x, self.height())
painter.drawLine(0, y, self.width(), y)
if self.ismd:
# 画坐标点
pos = QCursor.pos()
ret = win32gui.GetPixel(win32gui.GetWindowDC(
win32gui.GetDesktopWindow()), pos.x(), pos.y())
r, g, b = ret & 0xff, (ret >> 8) & 0xff, (ret >> 16) & 0xff
print(r, g, b)
painter.setPen(Qt.white)
painter.drawText(self.rect(), Qt.AlignLeft |
Qt.AlignBottom, '({}, {})\nRGB: ({}, {}, {})\n{}'.format(
pos.x(), pos.y(), r, g, b, QColor(r, g, b).name()))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(True)
w = Label()
w.show()
sys.exit(app.exec_())