Qt教程-屏幕截图工具

实现屏幕截图功能,可自由拖动选择区域

设置窗口无边框,背景透明

this->setWindowFlag(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setFocusPolicy(Qt::StrongFocus);

在窗口显示时,获取屏幕截图

void ScreenCaptureDlg::setVisible(bool visible)
{
	if (visible)
	{
		QScreen *pScreen = QGuiApplication::primaryScreen();
		m_pixmap = pScreen->grabWindow(0);
		
		this->setGeometry(0, 0, m_pixmap.width(), m_pixmap.height());
	}

	QDialog::setVisible(visible);
}

通过鼠标事件,实现在整个截图上自由选择区域

void ScreenCaptureDlg::mousePressEvent(QMouseEvent *event)
{
	m_pressPos = event->pos();

	m_selectRect.setTopLeft(event->pos());
	m_selectRect.setWidth(0);
	m_selectRect.setHeight(0);

	update();
}

void ScreenCaptureDlg::mouseMoveEvent(QMouseEvent *event)
{
	int width = event->pos().x() - m_pressPos.x();
	int height = event->pos().y() - m_pressPos.y();

	m_selectRect.setWidth(width);
	m_selectRect.setHeight(height);

	update();
}

在绘制事件中,先绘制完整截图,然后以红色边框绘制选择区域,然后以半透明黑色绘制选中区域以外的位置

painter.drawPixmap(rect, m_pixmap);

painter.setPen(QPen(QColor(255, 0, 0)));
painter.drawRect(m_selectRect);

painter.setBrush(QBrush(QColor(0, 0, 0, 150)));
painter.setPen(Qt::NoPen);

QPainterPath path;
path.setFillRule(Qt::OddEvenFill);
path.addRect(rect);
path.addRect(m_selectRect);
painter.drawPath(path);

截屏类代码

#include "ScreenCaptureDlg.h"

ScreenCaptureDlg::ScreenCaptureDlg(QWidget *parent)
	: QDialog(parent)
{
	this->setWindowFlag(Qt::FramelessWindowHint);
	this->setAttribute(Qt::WA_TranslucentBackground);
	this->setFocusPolicy(Qt::StrongFocus);
}

ScreenCaptureDlg::~ScreenCaptureDlg()
{
}

void ScreenCaptureDlg::setVisible(bool visible)
{
	if (visible)
	{
		QScreen *pScreen = QGuiApplication::primaryScreen();
		m_pixmap = pScreen->grabWindow(0);
		
		this->setGeometry(0, 0, m_pixmap.width(), m_pixmap.height());
	}

	QDialog::setVisible(visible);
}

QPixmap ScreenCaptureDlg::getPixmap()
{
	return m_pixmap;
}

void ScreenCaptureDlg::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);

	QRect rect;
	rect.setWidth(this->geometry().width());
	rect.setHeight(this->geometry().height());

	painter.drawPixmap(rect, m_pixmap);

	painter.setPen(QPen(QColor(255, 0, 0)));
	painter.drawRect(m_selectRect);

	painter.setBrush(QBrush(QColor(0, 0, 0, 150)));
	painter.setPen(Qt::NoPen);

	QPainterPath path;
	path.setFillRule(Qt::OddEvenFill);
	path.addRect(rect);
	path.addRect(m_selectRect);
	painter.drawPath(path);
}

void ScreenCaptureDlg::resizeEvent(QResizeEvent *event)
{

}

void ScreenCaptureDlg::mousePressEvent(QMouseEvent *event)
{
	m_pressPos = event->pos();

	m_selectRect.setTopLeft(event->pos());
	m_selectRect.setWidth(0);
	m_selectRect.setHeight(0);

	update();
}

void ScreenCaptureDlg::mouseMoveEvent(QMouseEvent *event)
{
	int width = event->pos().x() - m_pressPos.x();
	int height = event->pos().y() - m_pressPos.y();

	m_selectRect.setWidth(width);
	m_selectRect.setHeight(height);

	update();
}

void ScreenCaptureDlg::mouseReleaseEvent(QMouseEvent *event)
{
	m_selectRect = m_selectRect.normalized();
}

void ScreenCaptureDlg::keyPressEvent(QKeyEvent *event)
{

}

void ScreenCaptureDlg::keyReleaseEvent(QKeyEvent *event)
{
	if (event->key() == Qt::Key_Escape)
	{
		m_selectRect = QRect();
		repaint();
		
		this->reject();
	}

	if (event->key() == Qt::Key_Return)
	{
		m_pixmap = m_pixmap.copy(m_selectRect);
		m_selectRect = QRect();
		repaint();
		
		this->accept();
	}
}

完整工程下载:

https://download.csdn.net/download/tth0225/20351562

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值