diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index ba8eaf1..82392ec 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -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/TestFontRoboto.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/mainui.py=utf-8 encoding//\u65E0\u8FB9\u6846\u81EA\u5B9A\u4E49\u6807\u9898\u680F\u7A97\u53E3/FramelessWindow.py=utf-8 diff --git a/README.md b/README.md index c4603c2..37a34f1 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ - [1.28.3 pydext](C和C++扩展/pydext/) - [1.29 数据库查询显示表格](数据库查询显示表格/) - [1.30 左侧选项卡](左侧选项卡/) + - [1.40 探测窗口和放大截图](探测窗口和放大截图/) ### [2.QGraphicsView练习](QGraphicsView练习/) diff --git a/探测窗口和放大截图/README.md b/探测窗口和放大截图/README.md new file mode 100644 index 0000000..098445d --- /dev/null +++ b/探测窗口和放大截图/README.md @@ -0,0 +1,7 @@ +# 简单探测窗口和放大截图 + +利用win32gui模块获取鼠标所在位置的窗口大小(未去掉边框)和rgb颜色, 主要思路是利用一个全屏的全透明鼠标穿透的窗口(目的在于绘制矩形框和截图) + +截图 + +![1](ScreenShot/1.gif) \ No newline at end of file diff --git a/探测窗口和放大截图/ScreenShot/1.gif b/探测窗口和放大截图/ScreenShot/1.gif new file mode 100644 index 0000000..3886c1a Binary files /dev/null and b/探测窗口和放大截图/ScreenShot/1.gif differ diff --git a/探测窗口和放大截图/简单探测窗口和放大截图.py b/探测窗口和放大截图/简单探测窗口和放大截图.py new file mode 100644 index 0000000..4419b6b --- /dev/null +++ b/探测窗口和放大截图/简单探测窗口和放大截图.py @@ -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_())