// .cpp #include "paintscreenshot.h" #include <QApplication> #include <QDesktopWidget> #include <QMouseEvent> #include <QWidget> #include "screenshot.h" PaintScreenshot::PaintScreenshot(QWidget *parent) : QWidget(parent) , m_bIsMousePress(false) { initWindow(); loadBackgroundPixmap(); } PaintScreenshot::~PaintScreenshot() { } void PaintScreenshot::initWindow() { this->setMouseTracking(true); this->setWindowFlags(Qt::FramelessWindowHint); setWindowState(Qt::WindowActive | Qt::WindowFullScreen); } void PaintScreenshot::loadBackgroundPixmap() { m_loadPixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); //抓取当前屏幕的图片; m_nScreenwidth = m_loadPixmap.width(); m_nScreenheight = m_loadPixmap.height(); } void PaintScreenshot::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_bIsMousePress = true; m_beginPoint = event->pos(); } return QWidget::mousePressEvent(event); } void PaintScreenshot::mouseMoveEvent(QMouseEvent* event) { if (m_bIsMousePress) { m_endPoint = event->pos(); update(); } return QWidget::mouseMoveEvent(event); } void PaintScreenshot::mouseReleaseEvent(QMouseEvent *event) { m_endPoint = event->pos(); m_bIsMousePress = false; if (nullptr != m_pScreenshotWindow) { m_pScreenshotWindow->SendImageSaveAndShow(m_capturePixmap); } close(); return QWidget::mouseReleaseEvent(event); } void PaintScreenshot::paintEvent(QPaintEvent *event) { m_painter.begin(this); //进行重绘; QColor mShadowColor = QColor(0, 0, 0, 100); //阴影颜色设置; m_painter.setPen(QPen(Qt::blue, 1, Qt::SolidLine, Qt::FlatCap)); //设置画笔; m_painter.drawPixmap(0, 0, m_loadPixmap); //将背景图片画到窗体上; m_painter.fillRect(m_loadPixmap.rect(), mShadowColor); //画影罩效果; if (m_bIsMousePress) { QRect mSelectedRect = getRect(m_beginPoint, m_endPoint); m_capturePixmap = m_loadPixmap.copy(mSelectedRect); m_painter.drawPixmap(mSelectedRect.topLeft(), m_capturePixmap); m_painter.drawRect(mSelectedRect); } m_painter.end(); //重绘结束; return QWidget::paintEvent(event); } void PaintScreenshot::keyPressEvent(QKeyEvent *event) { //// Esc 键退出截图; //if (event->key() == Qt::Key_Escape) //{ // close(); //} // Eeter键完成截图; //if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) //{ // emit signalCompleteCature(m_capturePixmap); // close(); //} return QWidget::keyPressEvent(event); } QRect PaintScreenshot::getRect(const QPoint &beginPoint, const QPoint &endPoint) { int x, y, nWidth, nHeight; nWidth = qAbs(beginPoint.x() - endPoint.x()); nHeight = qAbs(beginPoint.y() - endPoint.y()); x = beginPoint.x() < endPoint.x() ? beginPoint.x() : endPoint.x(); y = beginPoint.y() < endPoint.y() ? beginPoint.y() : endPoint.y(); QRect mSelectedRect = QRect(x, y, nWidth, nHeight); // 避免宽或高为零时拷贝截图有误; // 可以看QQ截图,当选取截图宽或高为零时默认为2; if (mSelectedRect.width() == 0) { mSelectedRect.setWidth(1); } if (mSelectedRect.height() == 0) { mSelectedRect.setHeight(1); } return mSelectedRect; } void PaintScreenshot::SendScreenshotWindowPoint(Screenshot * mScreenshotWindowPoint) { if (nullptr != mScreenshotWindowPoint) { m_pScreenshotWindow = mScreenshotWindowPoint; } } // .h #ifndef PAINTSCREENSHOT_H #define PAINTSCREENSHOT_H #include <QWidget> #include <QScreen> #include <QPainter> class Screenshot; class PaintScreenshot : public QWidget { public: PaintScreenshot(QWidget *parent = 0); ~PaintScreenshot(); void SendScreenshotWindowPoint(Screenshot * ); private: void initWindow(); void loadBackgroundPixmap(); QRect getRect(const QPoint &mBeginPoint, const QPoint &mEndPoint); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent* event); void mouseReleaseEvent(QMouseEvent *event); void keyPressEvent(QKeyEvent *event); void paintEvent(QPaintEvent *event); private: bool m_bIsMousePress; QPixmap m_loadPixmap, m_capturePixmap; int m_nScreenwidth; int m_nScreenheight; QPoint m_beginPoint, m_endPoint; QPainter m_painter; Screenshot * m_pScreenshotWindow;// 接收截图窗口的指针地址 }; #endif // PAINTSCREENSHOT_H