Qt实现截图(可移动截屏区域并修改大小)


前言

可以显示截图区域大小,可以任意拖动截图区域,四个边框可拖拽修改大小。


提示:以下是本篇文章正文内容,下面案例可供参考

1.ScreenCaptureDlg.h

代码如下(示例):

#ifndef SCREENCAPTUREDLG_H
#define SCREENCAPTUREDLG_H

#include <QPen>
#include <QDebug>
#include <QTimer>
#include <QPoint>
#include <QScreen>
#include <QPixmap>
#include <QPixmap>
#include <QPainter>
#include <QDateTime>
#include <QKeyEvent>
#include <QClipboard>
#include <QPushButton>
#include <QMouseEvent>
#include <QMainWindow>
#include <QFileDialog>
#include <QApplication>
#include <QDesktopWidget>
#include <QDesktopServices>
#include "windows.h"

QT_BEGIN_NAMESPACE
namespace Ui { class ScreenCaptureDlg; }
QT_END_NAMESPACE

class ScreenCaptureDlg : public QMainWindow
{
    Q_OBJECT

public:
    ScreenCaptureDlg(QWidget *parent = nullptr);
    ~ScreenCaptureDlg();
    Init_Data();
signals:
    void Send_Data();                   //初始化数据
public slots:
    void MouseShape();
private slots:
    void addAction_clicked();
private:
    Ui::ScreenCaptureDlg *ui;
    QPixmap m_pixmap;
    QTimer *m_timer;
    QPoint m_pressPos;
    QPoint selectRect_Point;
    QPoint movePoint;
    QPoint m_Point;
    QPoint LowerLeft_Point;
    int m_width;
    int m_height;
    QRect m_selectRect;
    QRect Unsigned_selectRect;
    int DrawFalg = 0;
    int MoveFalg = 0;
    int PressFalg = 0;
    int UppeFalg, LeftFalg, lowerFalg, RightFalg= 0;
    int Lower_right, Upper_right  = 0;

    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void keyReleaseEvent(QKeyEvent *event);
};
#endif // SCREENCAPTUREDLG_H


2.ScreenCaptureDlg.cpp

代码如下(示例):

#include "screencapturedlg.h"
#include "ui_screencapturedlg.h"

ScreenCaptureDlg::ScreenCaptureDlg(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::ScreenCaptureDlg)
{
    ui->setupUi(this);
    //初始化数据
    Init_Data();
}

ScreenCaptureDlg::~ScreenCaptureDlg()
{
    delete ui;
}

//初始化数据
ScreenCaptureDlg::Init_Data()
{
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setFocusPolicy(Qt::StrongFocus);
    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, &ScreenCaptureDlg::MouseShape);
    m_timer->start(200);
    // 置顶
    ::SetWindowPos(HWND(this->winId()), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
    //开始截图
    {
        QScreen *pScreen = QGuiApplication::primaryScreen();
        m_pixmap = pScreen->grabWindow(0);
        this->setGeometry(0, 0, m_pixmap.width(), m_pixmap.height());
    }
    //先定义一个QAction的对象.
    QAction *action_Save_to_clipboard = new QAction(this);
    //设置触发QAction对象的快捷操作.
    action_Save_to_clipboard->setShortcut(tr("Ctrl+x"));
    //把这个QAction的对象加入到tableWidget
    addAction(action_Save_to_clipboard);
    //连接信号与槽.连接好了以后,当你按下ctrl+o时,就会调用槽函数,也就是这里自定义的openfile()函数;
    connect(action_Save_to_clipboard, &QAction::triggered, this, &ScreenCaptureDlg::addAction_clicked);
}


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);

    //显示截取区域信息
    {
        if(PressFalg == 1)
        {
            //截取区域大小信息
            QString pixmap_size = QString("%1").arg(Unsigned_selectRect.width())
                    + " X " + QString("%1").arg(Unsigned_selectRect.height())
                    + "(按ESC退出截图,按Enter保存截图,按Ctrl+X复制到剪切板)";
            QColor information_color = QColor(0, 0, 0, 150);
            painter.setPen(QColor(Qt::red));
            QFont font("黑体", 14, QFont::Bold, true);
            //使用字体
            painter.setFont(font);
            QFontMetrics fm(painter.font());
            QRect fm_rect = fm.boundingRect(pixmap_size);
            QRect pixmap_size_rect(Unsigned_selectRect.topLeft().x(), Unsigned_selectRect.topLeft().y() -25, fm_rect.width() + 10, 25);
            painter.fillRect(pixmap_size_rect, information_color);
            painter.drawText(pixmap_size_rect,Qt::AlignCenter, pixmap_size);
        }
    }
    if(PressFalg == 1)
    {
        //中间画一个小矩形来模拟数据位置
        painter.setPen(QPen(QColor(255, 0, 0)));
        painter.drawRect(m_selectRect.x() + m_selectRect.width()/2,m_selectRect.y()-10,20,20);
        painter.drawRect(m_selectRect.x()-10,m_selectRect.y() + m_selectRect.height()/2,20,20);
        painter.drawRect(m_selectRect.x() + m_selectRect.width()/2,m_selectRect.y() + m_selectRect.height()-10,20,20);
        painter.drawRect(m_selectRect.x() + m_selectRect.width()-10,m_selectRect.y() + m_selectRect.height()/2,20,20);


        //右上角
        painter.drawRect(m_selectRect.x() + m_selectRect.width()-10,m_selectRect.y()-10,20,20);
        //右下角
        painter.drawRect(m_selectRect.x() + m_selectRect.width()-10,m_selectRect.y() + m_selectRect.height()-10,20,20);
    }
    //绘制结束
    painter.end();
}

void ScreenCaptureDlg::resizeEvent(QResizeEvent *event)
{

}

void ScreenCaptureDlg::mousePressEvent(QMouseEvent *event)
{
    PressFalg = 1;
    m_pressPos = event->pos();
    if(DrawFalg == 0)
    {
        m_selectRect.setTopLeft(event->pos());
        m_selectRect.setWidth(0);
        m_selectRect.setHeight(0);
        update();
    }
    if(DrawFalg == 1)
    {
        if((m_pressPos.x() > m_selectRect.x()+10 && m_pressPos.x()< m_selectRect.x() + m_selectRect.width()-10) &&
                (m_pressPos.y() > m_selectRect.y()+10 && m_pressPos.y() < m_selectRect.y() + m_selectRect.height()-10))
        {
            setCursor(Qt::OpenHandCursor);
            MoveFalg = 0;
            selectRect_Point.setX(m_selectRect.x());
            selectRect_Point.setY(m_selectRect.y());
            movePoint = event->globalPos() - selectRect_Point;
        }
        //上
        if(((m_pressPos.x() >= m_selectRect.x() + m_selectRect.width()/2 - 10
             && m_pressPos.x()<= m_selectRect.x() + m_selectRect.width()/2 + 10)
            &&((m_pressPos.y() >= m_selectRect.y()-10 && m_pressPos.y()<= m_selectRect.y()+10))))
        {
            setCursor(Qt::SizeVerCursor);
            UppeFalg = 1;
        }
        //左
        if(((m_pressPos.x() >= m_selectRect.x()-10 && m_pressPos.x()<= m_selectRect.x()+10)
            &&((m_pressPos.y() >= m_selectRect.y() + m_selectRect.height()/2-10
                && m_pressPos.y()<= m_selectRect.y() + m_selectRect.height()/2+10))))
        {
            setCursor(Qt::SizeHorCursor);
            LeftFalg = 1;
        }
        //下
        if(((m_pressPos.x() >= m_selectRect.x() + m_selectRect.width()/2 -10 && m_pressPos.x()<= m_selectRect.x() + m_selectRect.width()/2 +10)
            &&((m_pressPos.y() >= m_selectRect.y() + m_selectRect.height()-10
                && m_pressPos.y()<= m_selectRect.y() + m_selectRect.height()+10))))
        {
            setCursor(Qt::SizeVerCursor);
            lowerFalg = 1;
        }
        //右
        if(((m_pressPos.x() >= m_selectRect.x() + m_selectRect.width()-10 && m_pressPos.x()<= m_selectRect.x() + m_selectRect.width()+10)
            &&((m_pressPos.y() >= m_selectRect.y() + m_selectRect.height()/2-10
                && m_pressPos.y()<= m_selectRect.y() + m_selectRect.height()/2+10))))
        {
            setCursor(Qt::SizeHorCursor);
            RightFalg = 1;
        }
        //右上角
        if(((m_pressPos.x() >= m_selectRect.x() + m_selectRect.width()-10 && m_pressPos.x()<= m_selectRect.x() + m_selectRect.width()+10)
            &&((m_pressPos.y() >= m_selectRect.y()-10
                && m_pressPos.y()<= m_selectRect.y()+10))))
        {
            setCursor(Qt::SizeBDiagCursor);
            Upper_right = 1;
        }
        //右下脚
        if(((m_pressPos.x() >= m_selectRect.x() + m_selectRect.width() -10 && m_pressPos.x()<= m_selectRect.x() + m_selectRect.width() +10)
            &&((m_pressPos.y() >= m_selectRect.y() + m_selectRect.height()-10
                && m_pressPos.y()<= m_selectRect.y() + m_selectRect.height()+10))))
        {
            setCursor(Qt::SizeFDiagCursor);
            Lower_right = 1;
        }
    }
}

void ScreenCaptureDlg::mouseMoveEvent(QMouseEvent *event)
{
    Unsigned_selectRect = m_selectRect;
    if(m_width < 0)
    {
        QPoint mypoint = QPoint(m_selectRect.x() + m_selectRect.width(), m_selectRect.y());
        Unsigned_selectRect.setTopLeft(mypoint);
        Unsigned_selectRect.setWidth(QString("%1").arg(m_width).replace("-","").toInt());
        Unsigned_selectRect.setHeight(QString("%1").arg(m_height).replace("-","").toInt());
        qDebug()<< "2---mypoint = " << mypoint << "3------ = " << Unsigned_selectRect.width() << "4------ = " << Unsigned_selectRect.height();
        update();
    }
    if(m_height < 0)
    {
        QPoint mypoint = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
        Unsigned_selectRect.setTopLeft(mypoint);
        Unsigned_selectRect.setWidth(QString("%1").arg(m_width).replace("-","").toInt());
        Unsigned_selectRect.setHeight(QString("%1").arg(m_height).replace("-","").toInt());
        qDebug()<< "5---mypoint = " << mypoint << "6------ = " << Unsigned_selectRect.width() << "7------ = " << Unsigned_selectRect.height();
        update();
    }

    if(DrawFalg == 0)
    {
        m_width = event->pos().x() - m_pressPos.x();
        m_height = event->pos().y() - m_pressPos.y();
        m_selectRect.setWidth(m_width);
        m_selectRect.setHeight(m_height);
        update();
    }
    if(DrawFalg == 1)
    {
        if(MoveFalg == 0)
        {
            QPoint movePos = event->globalPos();
            QPoint m_Point = movePos-movePoint;
            if((m_Point.x() >= 0 && m_Point.y() >= 0) && (m_Point.x() + m_selectRect.width() <= m_pixmap.width() && m_Point.y() + m_selectRect.height() <= m_pixmap.height()))
            {
                m_selectRect.setTopLeft(m_Point);
                m_selectRect.setWidth(m_width);
                m_selectRect.setHeight(m_height);
                //更新移动后的区域左下角坐标
                LowerLeft_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
                update();
            }
        }
        if(UppeFalg == 1)
        {
            m_Point = QPoint(m_selectRect.x(), event->globalPos().y());
            m_selectRect.setTopLeft(m_Point);
            m_height =  event->globalPos().y() - m_selectRect.y()  + m_selectRect.height();
            m_selectRect.setWidth(m_width);
            m_selectRect.setHeight(m_height);
            //更新移动后的区域左下角坐标
            LowerLeft_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
            update();
        }
        if(LeftFalg == 1)
        {
            m_Point = QPoint(event->globalPos().x(), m_selectRect.y());
            m_selectRect.setTopLeft(m_Point);
            m_width = event->globalPos().x() - m_selectRect.x()  + m_selectRect.width();

            m_selectRect.setWidth(m_width);
            m_selectRect.setHeight(m_height);
            //更新移动后的区域左下角坐标
            LowerLeft_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
            update();
        }
        if(lowerFalg == 1)
        {
            m_Point = QPoint(m_selectRect.x(), m_selectRect.y());
            m_selectRect.setTopLeft(m_Point);
            m_height = event->globalPos().y() - m_selectRect.y();

            m_selectRect.setWidth(m_width);
            m_selectRect.setHeight(m_height);
            //更新移动后的区域左下角坐标
            LowerLeft_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
            update();
        }
        if(RightFalg == 1)
        {
            m_Point = QPoint(m_selectRect.x(), m_selectRect.y());
            m_selectRect.setTopLeft(m_Point);
            m_width = event->globalPos().x() - m_selectRect.x();

            m_selectRect.setWidth(m_width);
            m_selectRect.setHeight(m_height);
            //更新移动后的区域左下角坐标
            LowerLeft_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
            update();
        }
        if(Upper_right == 1)
        {
            m_selectRect.setTopLeft(LowerLeft_Point);
            m_width = event->globalPos().x() - m_selectRect.x();
            m_height = event->globalPos().y() - m_selectRect.y();
            qDebug()<< "m_Point = " << m_Point << "m_width = " << m_width << "m_height = " << m_height;
            m_selectRect.setWidth(m_width);
            m_selectRect.setHeight(m_height);
            update();
        }
        if(Lower_right == 1)
        {
            m_Point = QPoint(m_selectRect.x(), m_selectRect.y());
            m_selectRect.setTopLeft(m_Point);
            m_width = event->globalPos().x() - m_selectRect.x();
            m_height = event->globalPos().y() - m_selectRect.y();
            qDebug()<< "m_Point = " << m_Point << "m_width = " << m_width << "m_height = " << m_height;
            m_selectRect.setWidth(m_width);
            m_selectRect.setHeight(m_height);
            //更新移动后的区域左下角坐标
            LowerLeft_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
            update();
        }
    }
}

void ScreenCaptureDlg::mouseReleaseEvent(QMouseEvent *event)
{
    if(DrawFalg == 0)
    {
        m_selectRect = m_selectRect.normalized();
        //设置第一次绘制完成后的区域左下角图标
        LowerLeft_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
    }

    DrawFalg = 1;
    MoveFalg = 1;
    UppeFalg = 0;
    LeftFalg = 0;
    lowerFalg = 0;
    RightFalg = 0;
    Lower_right = 0;
    Upper_right = 0;
    setCursor(Qt::ArrowCursor);

    //鼠标放开的时候重新定义文本显示的位置
    {
        if(m_width < 0)
        {
            m_Point = QPoint(m_selectRect.x() + m_selectRect.width(), m_selectRect.y());
            m_selectRect.setTopLeft(m_Point);

            m_width =  QString("%1").arg(m_width).replace("-","").toInt();
            m_selectRect.setWidth(m_width);
            update();
        }
        if(m_height < 0)
        {
            m_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
            m_selectRect.setTopLeft(m_Point);

            m_height =  QString("%1").arg(m_height).replace("-","").toInt();
            m_selectRect.setHeight(m_height);
            update();
        }
    }
    //每次鼠标放开后都更新下左下角坐标
    LowerLeft_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
}

void ScreenCaptureDlg::keyReleaseEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Escape)
    {
        m_timer->stop();
        m_selectRect = QRect();
        repaint();
        emit Send_Data();
        this->close();
    }

    if (event->key() == Qt::Key_Return)
    {
        //获取系统时间
        QDateTime time;
        QString temp;
        time = QDateTime::currentDateTime();
        temp = time.toString("yyyy-MM-dd hh-mm-ss");

        //设置保存的文件名
        QString screenFileName = QFileDialog::getSaveFileName(this,tr("保存截图"), QString("%1.png").arg(temp), tr("picture Files(*.png)"));
        qDebug()<< "screenFileName = " << screenFileName;
        if(screenFileName != nullptr)
        {
            if(m_height < 0)
            {
                m_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
                m_selectRect.setTopLeft(m_Point);

                m_height =  QString("%1").arg(m_height).replace("-","").toInt();
                m_selectRect.setHeight(m_height);
            }
            if(m_width < 0)
            {
                m_Point = QPoint(m_selectRect.x() + m_selectRect.width(), m_selectRect.y());
                m_selectRect.setTopLeft(m_Point);

                m_width =  QString("%1").arg(m_width).replace("-","").toInt();
                m_selectRect.setWidth(m_width);
            }
            //防止高度和宽度为0时保存错误
            if(m_width <= 0 && m_width > -1)
                m_selectRect.setWidth(1);
            if(m_height <= 0 && m_height > -1)
                m_selectRect.setHeight(1);

            QPixmap pixmap = m_pixmap.copy(m_selectRect);
            pixmap.save(screenFileName,Q_NULLPTR,100);

            QClipboard *clipboard = QApplication::clipboard();   //获取系统剪贴板指针
            QString originalText = clipboard->text();            //获取剪贴板上文本信息
            qDebug()<< "剪切板中的值:" << originalText;
            clipboard->setPixmap(pixmap);                        //设置剪贴板内容</span>
            m_timer->stop();
            QDesktopServices::openUrl(QUrl::fromLocalFile(screenFileName));
            DrawFalg = 0;
            MoveFalg = 0;
            emit Send_Data();
            this->close();
        }
    }
}

void ScreenCaptureDlg::MouseShape()
{
    int m_falg = 0;
    if(DrawFalg == 1)
    {
        if((QCursor::pos().x() > m_selectRect.x()+10 && QCursor::pos().x()< m_selectRect.x() + m_selectRect.width()-10) &&
                (QCursor::pos().y() > m_selectRect.y()+10 && QCursor::pos().y() < m_selectRect.y() + m_selectRect.height()-10))
        {
            setCursor(Qt::OpenHandCursor);
            m_falg = 1;
        }
        //上
        if(((QCursor::pos().x() >= m_selectRect.x() + m_selectRect.width()/2 - 10
             && QCursor::pos().x()<= m_selectRect.x() + m_selectRect.width()/2 + 10)
            &&((QCursor::pos().y() >= m_selectRect.y()-10 && QCursor::pos().y()<= m_selectRect.y()+10))))
        {
            setCursor(Qt::SizeVerCursor);
            m_falg = 1;
        }
        //左
        if(((QCursor::pos().x() >= m_selectRect.x()-10 && QCursor::pos().x()<= m_selectRect.x()+10)
            &&((QCursor::pos().y() >= m_selectRect.y() + m_selectRect.height()/2-10
                && QCursor::pos().y()<= m_selectRect.y() + m_selectRect.height()/2+10))))
        {
            setCursor(Qt::SizeHorCursor);
            m_falg = 1;
        }
        //下
        if(((QCursor::pos().x() >= m_selectRect.x() + m_selectRect.width()/2 -10 && QCursor::pos().x()<= m_selectRect.x() + m_selectRect.width()/2 +10)
            &&((QCursor::pos().y() >= m_selectRect.y() + m_selectRect.height()-10
                && QCursor::pos().y()<= m_selectRect.y() + m_selectRect.height()+10))))
        {
            setCursor(Qt::SizeVerCursor);
            m_falg = 1;
        }
        //右
        if(((QCursor::pos().x() >= m_selectRect.x() + m_selectRect.width()-10 && QCursor::pos().x()<= m_selectRect.x() + m_selectRect.width()+10)
            &&((QCursor::pos().y() >= m_selectRect.y() + m_selectRect.height()/2-10
                && QCursor::pos().y()<= m_selectRect.y() + m_selectRect.height()/2+10))))
        {
            setCursor(Qt::SizeHorCursor);
            m_falg = 1;
        }
        //右上角
        if(((QCursor::pos().x() >= m_selectRect.x() + m_selectRect.width()-10 && QCursor::pos().x()<= m_selectRect.x() + m_selectRect.width()+10)
            &&((QCursor::pos().y() >= m_selectRect.y()-10
                && QCursor::pos().y()<= m_selectRect.y()+10))))
        {
            setCursor(Qt::SizeBDiagCursor);
            m_falg = 1;
        }
        //右下脚
        if(((QCursor::pos().x() >= m_selectRect.x() + m_selectRect.width() -10 && QCursor::pos().x()<= m_selectRect.x() + m_selectRect.width() +10)
            &&((QCursor::pos().y() >= m_selectRect.y() + m_selectRect.height()-10
                && QCursor::pos().y()<= m_selectRect.y() + m_selectRect.height()+10))))
        {
            setCursor(Qt::SizeFDiagCursor);
            m_falg = 1;
        }
        if(m_falg == 0)
           setCursor(Qt::ArrowCursor);
    }
}


void ScreenCaptureDlg::addAction_clicked()
{
    if(m_height < 0)
    {
        m_Point = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
        m_selectRect.setTopLeft(m_Point);

        m_height =  QString("%1").arg(m_height).replace("-","").toInt();
        m_selectRect.setHeight(m_height);
    }
    if(m_width < 0)
    {
        m_Point = QPoint(m_selectRect.x() + m_selectRect.width(), m_selectRect.y());
        m_selectRect.setTopLeft(m_Point);

        m_width =  QString("%1").arg(m_width).replace("-","").toInt();
        m_selectRect.setWidth(m_width);
    }
    //防止高度和宽度为0时保存错误
    if(m_width <= 0 && m_width > -1)
        m_selectRect.setWidth(1);
    if(m_height <= 0 && m_height > -1)
        m_selectRect.setHeight(1);

    QPixmap pixmap = m_pixmap.copy(m_selectRect);
    QClipboard *clipboard = QApplication::clipboard();   //获取系统剪贴板指针
    clipboard->setPixmap(pixmap);                        //设置剪贴板内容</span>
    emit Send_Data();
    close();
}

4.Dialog.h

代码如下(示例):

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QMovie>
#include <QBitmap>
#include <QLabel>
#include "screencapturedlg.h"

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = nullptr);
    ~Dialog();

public slots:
    void Get_Data();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Dialog *ui;
    ScreenCaptureDlg *m_ScreenCaptureDlg;
    int m_x, m_y, m_w, m_h;
};

#endif // DIALOG_H

5.Dialog.cpp

代码如下(示例):

#include "dialog.h"
#include "ui_dialog.h"
#include <QTimer>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->setWindowTitle("截屏");
    //隐藏帮助按钮
    Qt::WindowFlags flags = windowFlags();
    Qt::WindowFlags helpFlag = Qt::WindowContextHelpButtonHint;
    flags = flags & (~helpFlag);
    setWindowFlags(flags);

}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::Get_Data()
{
    this->setGeometry(m_x, m_y, m_w, m_h);
    this->show();
}

void Dialog::on_pushButton_clicked()
{
    m_x = this->x();
    m_y = this->y();
    m_w = this->width();
    m_h = this->height();
    this->hide();
    m_ScreenCaptureDlg = new ScreenCaptureDlg(this);
    connect(m_ScreenCaptureDlg, &ScreenCaptureDlg::Send_Data, this, &Dialog::Get_Data);
    m_ScreenCaptureDlg->show();
}

6.pro文件

代码如下(示例):

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    dialog.cpp \
    main.cpp \
    screencapturedlg.cpp

HEADERS += \
    dialog.h \
    screencapturedlg.h \

FORMS += \
    dialog.ui \
    screencapturedlg.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    pic.qrc

RC_ICONS += trayIcon.ico	


7.UI

请添加图片描述

8.效果展示

请添加图片描述
GIF太大上传不了,演示图片经过两次压缩,有点模糊

8.总结

该项目只是用来练习,没有截图二次操作,感兴趣的可以自己添加.

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值