截图画矩形
This commit is contained in:
parent
c2e68b81da
commit
9f90499259
4 changed files with 112 additions and 1 deletions
|
@ -23,4 +23,6 @@
|
|||
|
||||
### 10.<a href="自定义QWidget的QSS样式">自定义QWidget的QSS样式</a><br />
|
||||
|
||||
### 11.<a href="浏览器获取Cookie">浏览器获取Cookie</a><br />
|
||||
### 11.<a href="浏览器获取Cookie">浏览器获取Cookie</a><br />
|
||||
|
||||
### 12.<a href="截图画矩形">截图画矩形</a><br />
|
104
截图画矩形/DrawRectangle.py
Normal file
104
截图画矩形/DrawRectangle.py
Normal file
|
@ -0,0 +1,104 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
'''
|
||||
Created on 2017年12月10日
|
||||
@author: Irony."[讽刺]
|
||||
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
|
||||
@email: 892768447@qq.com
|
||||
@file: DrawRectangle
|
||||
@description:
|
||||
'''
|
||||
import sys
|
||||
|
||||
from PyQt5.QtCore import Qt, QRect, QRectF
|
||||
from PyQt5.QtGui import QPainter, QColor, QPen, QPainterPath
|
||||
from PyQt5.QtWidgets import QApplication, QDialog
|
||||
|
||||
|
||||
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
|
||||
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
|
||||
__Version__ = "Version 1.0"
|
||||
|
||||
|
||||
class DrawRectangle(QDialog):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DrawRectangle, self).__init__(*args, **kwargs)
|
||||
self.pressed = False
|
||||
self.moved = False
|
||||
self.rectArea = QRect() # 矩形框
|
||||
self.rectPen = QPen(QColor(22, 168, 250))
|
||||
self.rectPen.setWidth(1)
|
||||
self.setMouseTracking(True)
|
||||
self.setWindowFlags(Qt.FramelessWindowHint)
|
||||
self.setAttribute(Qt.WA_TranslucentBackground, True)
|
||||
self.showMaximized()
|
||||
# 通过绑定自定义右键菜单来监听右键点击,实现重新画区域或者退出,或者弹出菜单
|
||||
self.setContextMenuPolicy(Qt.CustomContextMenu)
|
||||
self.customContextMenuRequested.connect(self.onRightClicked)
|
||||
|
||||
def onRightClicked(self, _):
|
||||
if not self.rectArea.isNull():
|
||||
self.rectArea.setWidth(0)
|
||||
self.rectArea.setHeight(0) # clear
|
||||
return self.update()
|
||||
self.close()
|
||||
|
||||
def mousePressEvent(self, event):
|
||||
if event.buttons() == Qt.LeftButton:
|
||||
self.pressed = True
|
||||
# if not self.rectArea.isNull():
|
||||
# self.moved = self.rectArea.contains(
|
||||
# event.pos()) # 设置画好的窗口可整体移动
|
||||
# return
|
||||
self.rectArea.setTopLeft(event.pos()) # 设置左上角位置
|
||||
|
||||
def mouseReleaseEvent(self, event):
|
||||
self.pressed = False
|
||||
self.moved = False
|
||||
super(DrawRectangle, self).mouseReleaseEvent(event)
|
||||
|
||||
def mouseMoveEvent(self, event):
|
||||
pos = event.pos()
|
||||
# if self.moved and not self.rectArea.isNull(): # 表示移动整体画好的矩形框
|
||||
# # 重新设置矩形框的位置
|
||||
# self.rectArea.setX(pos.x())
|
||||
# self.rectArea.setY(pos.y())
|
||||
# return self.update()
|
||||
if not self.pressed:
|
||||
if not self.rectArea.isNull():
|
||||
if self.rectArea.contains(pos):
|
||||
# 设置鼠标形状为十字箭头
|
||||
return self.setCursor(Qt.SizeAllCursor)
|
||||
# 设置鼠标为普通形状
|
||||
return self.setCursor(Qt.ArrowCursor)
|
||||
self.rectArea.setBottomRight(pos) # 设置右下角位置
|
||||
self.update()
|
||||
|
||||
def paintEvent(self, event):
|
||||
painter = QPainter(self)
|
||||
if self.rectArea.isNull():
|
||||
# 窗口背景全部半透明颜色
|
||||
return painter.fillRect(self.rect(), QColor(0, 0, 0, 50))
|
||||
|
||||
# 画外围背景,去掉中间绘制的矩形框
|
||||
path = QPainterPath()
|
||||
path.setFillRule(Qt.OddEvenFill)
|
||||
path.addRect(QRectF(self.rect()))
|
||||
path.addRect(QRectF(self.rectArea))
|
||||
painter.fillPath(path, QColor(0, 0, 0, 50))
|
||||
# 画矩形
|
||||
painter.save()
|
||||
painter.setPen(self.rectPen)
|
||||
painter.drawRect(self.rectArea) # 画矩形框
|
||||
# 背景用白色透明遮住,防止鼠标穿透到桌面
|
||||
painter.fillRect(self.rectArea, QColor(255, 255, 255, 1))
|
||||
painter.restore()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
w = DrawRectangle()
|
||||
w.show()
|
||||
sys.exit(app.exec_())
|
5
截图画矩形/README.md
Normal file
5
截图画矩形/README.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
# 仿截图软件画矩形框,暂时只实现了矩形框的绘制,框的移动和大小调整还未实现<br />
|
||||
右键退出矩形框或退出程序
|
||||
|
||||
# 截图
|
||||
<img src="ScreenShot/1.gif" />
|
BIN
截图画矩形/ScreenShot/1.gif
Normal file
BIN
截图画矩形/ScreenShot/1.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 MiB |
Loading…
Reference in a new issue