qt截图

QT截图


在这里插入图片描述

#ifndef CAPTURESCREEN_H
#define CAPTURESCREEN_H


#include <QWidget>
#include <QPainter>
#include<QRect>
#include<QLabel>
#include<QClipboard>
class CaptureScreen : public QWidget
{
    Q_OBJECT

public:
    CaptureScreen(QWidget *parent = 0);
    ~CaptureScreen();
    QRect getRect(const QPoint &beginPoint, const QPoint &endPoint);

Q_SIGNALS:
    void signalCompleteCature(QPixmap catureImage);

private:
    void initWindow();                          //初始化窗口
    void loadBackgroundPixmap();                //获取整个窗口下来

    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void paintEvent(QPaintEvent *event);
    QRect changePosition(const QPoint &beginPoint, const QPoint &endPoint);

private:
    bool m_isMousePress1=false,m_isMousePress2=false; //鼠标是否被按下
    QPixmap m_loadPixmap, m_capturePixmap;      //pixmap加载和捕获
    int m_screenwidth;                          //屏幕的宽度
    int m_screenheight;                         //屏幕的高度
    QPoint m_beginPoint, m_endPoint;            //开始点和结束点的坐标
    QPainter m_painter;                         //画笔
    QPoint m_changePoint,m_begin_movePonit,m_end_movePoint;//移动,改变大小
    QClipboard *clip;
    QRect selectedRect;
    int flag=0;
    // 四个角坐标;
    QPoint topLeft;
    QPoint topRight ;
    QPoint bottomLeft ;
    QPoint bottomRight;
    // 四条边中间点坐标;
    QPoint leftCenter ;
    QPoint topCenter ;
    QPoint rightCenter ;
    QPoint bottomCenter ;

};
#endif // CAPTURESCREEN_H

#include "capturescreen.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QMouseEvent>
#include<QDebug>
/*
 * 截图:把选定的图的放在剪贴板
 * 原理:
 * 1.利用QPixMap::grabWindow抓取图片m_loadPixmap
 * 2.重绘事件,让m_loadPixmap布上阴影
 * 3.对窗口进行选取,然后得到m_capturePixmap
 * 4.复制进剪贴板
 *
*/
CaptureScreen::CaptureScreen(QWidget *parent)
    : QWidget(parent)
    , m_isMousePress1(false)
{
    initWindow();
    loadBackgroundPixmap();
}

CaptureScreen::~CaptureScreen()
{

}

void CaptureScreen::initWindow()
{
    this->setMouseTracking(true);
    this->setWindowFlags(Qt::FramelessWindowHint);//无法进行移动和调整大小
    setWindowState(Qt::WindowActive | Qt::WindowFullScreen);//激活窗口,全屏
}

void CaptureScreen::loadBackgroundPixmap()
{
    m_loadPixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); //抓取当前屏幕的图片;
    m_screenwidth = m_loadPixmap.width();
    m_screenheight = m_loadPixmap.height();
}

void CaptureScreen::mousePressEvent(QMouseEvent *event)//鼠标被按下,记录起点
{
    if (event->button() == Qt::LeftButton)
    {
        m_isMousePress1 = true;
        m_isMousePress2 = true;
        if(flag==1)
        {
            m_isMousePress1=false;
            m_begin_movePonit=event->pos();
        }
        else if(flag==0)
        {

            m_beginPoint = event->pos();
        }
    }

    return QWidget::mousePressEvent(event);
}

void CaptureScreen::mouseMoveEvent(QMouseEvent* event)//鼠标移动
{
    if (m_isMousePress1)
    {
        m_endPoint = event->pos();
        update();
        flag=1;
    }
    else if(m_isMousePress2)
    {
        this->setCursor(Qt::SizeAllCursor);
        m_end_movePoint = event->pos();
        update();

    }
    return QWidget::mouseMoveEvent(event);
}

void CaptureScreen::mouseReleaseEvent(QMouseEvent *event)//鼠标释放
{
    m_endPoint = event->pos();
    m_end_movePoint=QPoint(0,0);
    m_begin_movePonit=QPoint(0,0);
    m_isMousePress1 = false;
    m_isMousePress2=false;
    if(flag==1){m_endPoint=selectedRect.bottomRight();m_beginPoint=selectedRect.topLeft();}
    this->setCursor(Qt::ArrowCursor);
    clip=QApplication::clipboard();
    clip->setPixmap(m_capturePixmap);
    return QWidget::mouseReleaseEvent(event);
}

void CaptureScreen::paintEvent(QPaintEvent *event)
{
    m_painter.begin(this);          //进行重绘;

    QColor shadowColor = QColor(30, 0, 0, 200);                      //阴影颜色设置;
    m_painter.setPen(QPen(Qt::blue, 1, Qt::SolidLine, Qt::FlatCap));    //设置画笔;
    m_painter.drawPixmap(0, 0, m_loadPixmap);                       //将背景图片画到窗体上;
    m_painter.fillRect(m_loadPixmap.rect(), shadowColor);           //画影罩效果;
    if (m_isMousePress1)
    {
       selectedRect = getRect(m_beginPoint, m_endPoint);
        m_capturePixmap = m_loadPixmap.copy(selectedRect);            //可以这是关键,直接就复制下来了
        m_painter.drawPixmap(selectedRect.topLeft(), m_capturePixmap);//在选中的区域直接再次绘制,那样就没阴影了
        m_painter.drawRect(selectedRect);                             //再画一个矩形,完事
    }
    else if(m_isMousePress2)//进行移动
    {
        selectedRect=changePosition(m_begin_movePonit,m_end_movePoint);
        m_capturePixmap = m_loadPixmap.copy(selectedRect);            //可以这是关键,直接就复制下来了
        m_painter.drawPixmap(selectedRect.topLeft(), m_capturePixmap);//在选中的区域直接再次绘制,那样就没阴影了
        m_painter.drawRect(selectedRect);                             //再画一个矩形,完事
    }
    // 四个角坐标;
    topLeft = selectedRect.topLeft();
    topRight = selectedRect.topRight();
    bottomLeft = selectedRect.bottomLeft();
    bottomRight = selectedRect.bottomRight();
    // 四条边中间点坐标;
     QColor color = QColor(30, 174, 255);
    leftCenter = QPoint(topLeft.x(), (topLeft.y() + bottomLeft.y()) / 2);
    topCenter = QPoint((topLeft.x() + topRight.x()) / 2, topLeft.y());
    rightCenter = QPoint(topRight.x(), leftCenter.y());
    bottomCenter = QPoint(topCenter.x(), bottomLeft.y());

    m_painter.drawRect(QRect(topLeft.x()-2,topLeft.y()-2,6,6));
    m_painter.drawRect(QRect(topRight.x()-2,topRight.y()-2,6,6));
    m_painter.drawRect(QRect(bottomLeft.x()-2,bottomLeft.y()-2,6,6));
    m_painter.drawRect(QRect(leftCenter.x()-2,leftCenter.y()-2,6,6));
    m_painter.drawRect(QRect(topCenter.x()-2,topCenter.y()-2,6,6));
    m_painter.drawRect(QRect(rightCenter.x()-2,rightCenter.y()-2,6,6));
    m_painter.drawRect(QRect(topCenter.x()-2,topCenter.y()-2,6,6));
    m_painter.drawRect(QRect(bottomCenter.x()-2,bottomCenter.y()-2,6,6));
    m_painter.end();  //重绘结束;
}

void CaptureScreen::keyPressEvent(QKeyEvent *event)
{
    // Esc 键退出截图;
    if (event->key() == Qt::Key_Escape)
    {
        close();
    }
    // Eeter键完成截图;
    if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
    {
        signalCompleteCature(m_capturePixmap);
        close();
    }
}

QRect CaptureScreen::getRect(const QPoint &beginPoint, const QPoint &endPoint)//选取区域,思想不错
{
    int x, y, width, height;
    width = qAbs(beginPoint.x() - endPoint.x());
    height = qAbs(beginPoint.y() - endPoint.y());
    x = beginPoint.x() < endPoint.x() ? beginPoint.x() : endPoint.x();
    y = beginPoint.y() < endPoint.y() ? beginPoint.y() : endPoint.y();

   selectedRect = QRect(x, y, width, height);
    // 避免宽或高为零时拷贝截图有误;
    // 可以看QQ截图,当选取截图宽或高为零时默认为2;
    if (selectedRect.width() == 0)
    {
        selectedRect.setWidth(1);
    }
    if (selectedRect.height() == 0)
    {
        selectedRect.setHeight(1);
    }

    return selectedRect;
}

QRect CaptureScreen::changePosition(const QPoint &beginPoint, const QPoint &endPoint)//改变和移动截图位置
{
    QRect currentRect = getRect(m_beginPoint, m_endPoint);
    m_isMousePress1=false;
    m_changePoint=endPoint-beginPoint;
    // 检查当前是否移动超出屏幕;
    //移动选区是否超出屏幕左边界;
    if (currentRect.topLeft().x() + m_changePoint.x() < 2)
    {
        m_changePoint.setX(2 - currentRect.topLeft().x());
    }
    //移动选区是否超出屏幕上边界;
    if (currentRect.topLeft().y() + m_changePoint.y() < 2)
    {
        m_changePoint.setY(2 - currentRect.topLeft().y());
    }
    //移动选区是否超出屏幕右边界;
    if (currentRect.bottomRight().x() + m_changePoint.x()+2 > m_screenwidth)
    {
        m_changePoint.setX(m_screenwidth - currentRect.bottomRight().x()+2);
    }
    //移动选区是否超出屏幕下边界;
    if (currentRect.bottomRight().y() + m_changePoint.y() +2> m_screenheight)
    {
        m_changePoint.setY(m_screenheight - currentRect.bottomRight().y()+2);
    }

    //八个方向
//    if(QRect(leftCenter.x()-2,leftCenter.y()-2,30,30).contains(m_begin_movePonit)||
//            QRect(leftCenter.x()-2,leftCenter.y()-2,30,30).contains(m_begin_movePonit))//左右线上


    if(QRect(leftCenter.x()-2,leftCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==4)//左中
    {

        setCursor(Qt::SizeHorCursor);
        flag=4;
        selectedRect.setX(selectedRect.x()+m_changePoint.x());
        return selectedRect;
    }
    else if(QRect(rightCenter.x()-2,rightCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==5)//右中
    {
        setCursor(Qt::SizeHorCursor);
        flag=5;
        selectedRect.setWidth(selectedRect.width()+m_changePoint.x());
        return selectedRect;
    }
    else if(QRect(bottomCenter.x()-2,bottomCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==6)//下中
    {
        setCursor(Qt::SizeVerCursor);
        flag=6;
        selectedRect.setHeight(selectedRect.height()+m_changePoint.y());
        return selectedRect;
    }
    else if(QRect(topCenter.x()-2,topCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==7)//上中
    {
        setCursor(Qt::SizeVerCursor);
        flag=7;
        selectedRect.setY(selectedRect.y()+m_changePoint.y());
        return selectedRect;
    }
    else if(QRect(topLeft.x()-2,topCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==8)
    {
        setCursor(Qt::SizeVerCursor);
        flag=8;
        selectedRect.setY(selectedRect.y()+m_changePoint.y());
        selectedRect.setX(selectedRect.x()+m_changePoint.x());
        return selectedRect;
    }
    else if(QRect(topRight.x()-2,topRight.y()-2,10,10).contains(m_begin_movePonit)||flag==9)
    {
        setCursor(Qt::SizeVerCursor);
        flag=9;
        selectedRect.setX(selectedRect.x()+m_changePoint.y());
        selectedRect.setWidth(selectedRect.width()+m_changePoint.x());
        return selectedRect;
    }
    else if(QRect(bottomRight.x()-2,bottomRight.y()-2,10,10).contains(m_begin_movePonit)||flag==10)
    {
        setCursor(Qt::SizeVerCursor);
        flag=10;
        selectedRect.setHeight(selectedRect.height()+m_changePoint.y());
        selectedRect.setWidth(selectedRect.width()+m_changePoint.x());
        return selectedRect;
    }
    else if(QRect(bottomLeft.x()-2,bottomLeft.y()-2,10,10).contains(m_begin_movePonit)||flag==11)
    {
        setCursor(Qt::SizeVerCursor);
        flag=11;
        selectedRect.setHeight(selectedRect.height()+m_changePoint.y());
        selectedRect.setX(selectedRect.x()+m_changePoint.x());
        return selectedRect;
    }
        else//窗体移动
    {
        QPoint m_beginPoint1=m_beginPoint+m_changePoint;//都是以起始位置未计算点,如果起始点发生改变那么计算有误
        QPoint m_endPoint1=m_end_movePoint+m_changePoint;
        qDebug()<<selectedRect<<m_changePoint<<m_begin_movePonit<<m_end_movePoint<<m_beginPoint<<m_endPoint;
        selectedRect=getRect(m_beginPoint1,m_endPoint1);
        selectedRect.setWidth(qAbs(m_beginPoint.x() - m_endPoint.x()));
        selectedRect.setHeight(qAbs(m_beginPoint.y() - m_endPoint.y()));
        return  selectedRect;
    }
}

#include "capturescreen.h"
#include <QApplication>
int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
    CaptureScreen p;
    p.show();

    return a.exec();
}

猜你喜欢

转载自blog.csdn.net/qq_33564134/article/details/84279909