实现屏幕截图功能,可自由拖动选择区域
设置窗口无边框,背景透明
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();
}
}