QT截屏工具

直接上项目:

项目框架:

 m_rect.h

#ifndef M_RECT_H
#define M_RECT_H

#include <QMainWindow>
#include <QDesktopWidget>
#include <QMenu>//右键菜单
#include <QWidget>
#include <QMouseEvent>
#include <QPainter>//画笔
#include <QPixmap>
#include <QDebug>
#include <QApplication>
#include <QClipboard>
#include <QFileDialog>
#include <QShowEvent>
#include <QMessageBox>
namespace Ui {
class M_rect;
}

class M_rect : public QMainWindow
{
    Q_OBJECT
public slots:
     void save();
     void copy();
protected:
    void mousePressEvent(QMouseEvent *e);//鼠标按下事件
    void mouseMoveEvent(QMouseEvent *e);//鼠标移动事件
    void mouseReleaseEvent(QMouseEvent *e);//鼠标释放(松开)事件
    void paintEvent(QPaintEvent *);//画图事件
    void showEvent(QShowEvent *);//窗体show事件
    void contextMenuEvent(QContextMenuEvent *);
public:
    explicit M_rect(QWidget *parent = nullptr);
    ~M_rect();
    QPixmap fullScreen;//全屏截图
    QPoint getBeginPos();//获取鼠标的起始位置
    QPoint getEndPos();//获取鼠标的结束位置
    void setBeginPos(QPoint p);//设置鼠标的起始位置
    void setEndPos(QPoint p);//设置鼠标的结束位置
private:
    Ui::M_rect *ui;
    QPoint beginPos;//记录鼠标的起始位置
    QPoint endPos;//记录鼠标的结束位置
    bool leftPres;//记录鼠标左键是否按下,按下为true
    QRect * rect; //矩形截图区域
    QMenu *menu;
signals:
    void Return(QPixmap);

};


#endif // M_RECT_H

screenshot.h

#ifndef SCREENSHOT_H
#define SCREENSHOT_H

#include <QMainWindow>
#include<QFileDialog>
#include <QPixmap>
#include <QDesktopWidget>
#include <QMessageBox>
#include<synchapi.h>//sleep头文件
#include <QClipboard>
#include <QClipboard>
#include"m_rect.h"

QT_BEGIN_NAMESPACE
namespace Ui { class screenshot; }
QT_END_NAMESPACE

class screenshot : public QMainWindow
{
    Q_OBJECT

public:
    screenshot(QWidget *parent = nullptr);
    ~screenshot();

    QRect getRect(const QPoint &beginPoint, const QPoint &endPoint);
    QPixmap   fullScreen;

private slots:
    void on_close_clicked();
    void on_newpicture_clicked();
    void on_preservation_clicked();
    void on_checkBox_stateChanged(int arg1);
    void on_pushButton_clicked();
    void on_copy_clicked();

private:
    Ui::screenshot *ui;
    QRect  *rect; //矩形截图区域
    QPixmap pixmap;
    int num;//是否隐藏窗口判断值
    M_rect *m_rect;
};
#endif // SCREENSHOT_H

m_rect.cpp

#include "m_rect.h"
#include "ui_m_rect.h"
#include "ui_screenshot.h"

M_rect::M_rect(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::M_rect)
{
    ui->setupUi(this);
    beginPos = QPoint(-1,-1);//初始坐标
    endPos = beginPos;//结束坐标等于初始坐标
    leftPres = false;
    rect = new QRect(0,0,QApplication::desktop()->width(),QApplication::desktop()->height());
    menu = new QMenu(this);//创建右键菜单
    menu->addAction("复制", this, SLOT(copy()));
    menu->addAction("截图另存为", this, SLOT(save()));
    menu->addAction("退出截图", this, SLOT(hide()));
}
M_rect::~M_rect()
{
    delete ui;
}
QPoint M_rect::getBeginPos()//获取鼠标的起始位置
{
    return beginPos;
}
QPoint M_rect::getEndPos()//获取鼠标的结束位置
{
    return endPos;
}
void M_rect::setBeginPos(QPoint p)//设置鼠标的起始位置
{
    this->beginPos = p;
}
void M_rect::setEndPos(QPoint p)//设置鼠标的结束位置
{
    this->endPos = p;
}
void M_rect::showEvent(QShowEvent *)
{
    setWindowOpacity(0.6); //设置透明度实现模糊背景
}
void M_rect::contextMenuEvent(QContextMenuEvent *)
{
    menu->exec(cursor().pos());//菜单显示的位置跟随鼠标
}
void M_rect::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton )//鼠标左键按下
    {
        leftPres = true;
        setBeginPos(e->pos());//鼠标相对窗体的位置,记录截图的开始位置
    }

}
void M_rect::mouseMoveEvent(QMouseEvent *e)
{
    if(leftPres)
    {
        setEndPos(e->pos());//不断的更新截图的结束位置
        update();//重绘、触发画图事件
    }
}
void M_rect::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)//鼠标左键释放
    {
        leftPres = false;
        setEndPos(e->pos());//记录截图的结束位置
        //使得起始点在左上角,结束点在右下角
        if(beginPos.x()>endPos.x())
        {
            beginPos.setX(beginPos.x() + endPos.x());
            endPos.setX(beginPos.x() - endPos.x());
            beginPos.setX(beginPos.x() - endPos.x());
        }
        if(beginPos.y()>endPos.y())
        {
            beginPos.setY(beginPos.y() + endPos.y());
            endPos.setY(beginPos.y() - endPos.y());
            beginPos.setY(beginPos.y() - endPos.y());
        }
        rect->setRect(beginPos.x(),beginPos.y(),endPos.x()-beginPos.x(),endPos.y()-beginPos.y());
    }
}
void M_rect::paintEvent(QPaintEvent *)
{
    QPainter painter(this); //将当前窗体对象设置为画布
    QPen pen;
    pen.setColor(Qt::red);//设置笔色
    pen.setWidth(0.8);     //画笔线条宽度
    painter.setPen(pen);//设置画笔
    int lx = beginPos.x()<endPos.x()?beginPos.x():endPos.x();//矩形截图区域左上角x坐标
    int ly = beginPos.y()<endPos.y()?beginPos.y():endPos.y();//矩形截图区域右上角x坐标
    int w = beginPos.x()<endPos.x()?endPos.x()-beginPos.x():beginPos.x()-endPos.x();//矩形截图区域宽度
    int h = beginPos.y()<endPos.y()?endPos.y()-beginPos.y():beginPos.y()-endPos.y();//矩形截图区域高度
    QRect rect = QRect(lx,ly,w,h);//矩形截图区域
    painter.drawPixmap(rect,fullScreen,rect);//重绘截图矩形部分,即恢复原图,达到去除幕布效果
    painter.drawRect(lx, ly, w, h);//画截图矩形
}
void M_rect::save()//截图另存为
{
    QString fileName = QFileDialog::getSaveFileName(this, "截图另存为", "test.bmp", "Image (*.jpg *.png *.bmp)");
    if (fileName.length() > 0){
        fullScreen.copy(*rect).save(fileName,"bmp");
        this->close();
    }
}
void M_rect::copy() //将截图复制到粘贴板
{
   QGuiApplication::clipboard()->setPixmap(fullScreen.copy(*rect));
   if(QGuiApplication::clipboard()){
   QMessageBox::information(this, "提示", "复制成功!",QMessageBox::Ok);//提示窗口
     }
}

screenshot.cpp

#include "screenshot.h"
#include "ui_screenshot.h"

screenshot::screenshot(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::screenshot)
{
    ui->setupUi(this);
    this->setFixedSize( this->width(),this->height ());//固定窗口
}
screenshot::~screenshot()
{
    delete ui;
}
void screenshot::on_checkBox_stateChanged(int arg1)//选择是否隐藏窗口
{
        if(arg1==2)
        {
             num=1;
        }
        else if(arg1==0)
        {
             num=0;
        }
}
void screenshot::on_newpicture_clicked()//新建全屏截图
{
    if(num==1)//通过num判断是否需要隐藏窗口
    {
         setWindowState( Qt::WindowMinimized );//最小化窗体
    }
    Sleep(180);//睡眠180毫秒
    pixmap = QPixmap::grabWindow ( QApplication::desktop()->winId());//获取全屏截图
    ui->label->setPixmap ( pixmap.scaled ( ui->label->size() ) );//输出图片,预览截图
    this->showNormal();//截屏后显示窗口
}
void screenshot::on_pushButton_clicked()//矩形截图
{
    if(num==1)
    {
         setWindowState( Qt::WindowMinimized );//最小化窗体
    }
    //先获取全屏截图,全屏显示
    M_rect *m=new M_rect();
    m->fullScreen = QPixmap::grabWindow(QApplication::desktop()->winId());
    m->showFullScreen();
}
void screenshot::on_copy_clicked()//复制到粘贴板
{
    QGuiApplication::clipboard()->setPixmap(pixmap.copy(*rect));
    if( QGuiApplication::clipboard()){
    QMessageBox::information(this, "提示", "复制成功!",QMessageBox::Ok);//提示窗口
    }
}
void screenshot::on_preservation_clicked()//保存图片
{
    QString fileName = QFileDialog::getSaveFileName(this, "文件另存为","",tr("Config Files (*.bmp)"));
    if(fileName.length() > 0 && pixmap.save(fileName,"bmp"))
    {
        QMessageBox::information(this, "提示", "保存成功!",QMessageBox::Ok);//提示窗口
    }
}
void screenshot::on_close_clicked()//取消按键
{
     this->close();//关闭窗体
}

main.cpp

#include "screenshot.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    screenshot w;
    w.show();
    return a.exec();
}

ui

视频预览:

预览

Ui介绍

图片预览区域

主窗口为一个Label存放图片,可以预览全屏图片,label下有一个checkBox可选按键,五个button,分别为全屏截图,矩形截图,复制,保存,退出功能。

隐藏窗口可选键

按键1为checkBox可选按键,在label的右下方,勾选后可实现在全屏截屏或矩形截图时隐藏此窗口。

全屏截图按键

按键2为全屏截图,点击此按键后会把图片投放到label上,实现预览图片,不满意可重复截图,满意后可复制可保存。

    矩形截图按键

按键3为矩形截图,点击后会在全屏截图的基础上使用鼠标自定义绘制矩形图片,绘制完成后,点击鼠标右键后会有可选菜单,有复制,保存,退出等功能。

复制按键

按键4为复制按键,点击后可复制到粘贴板。

保存按键

按键5为保存按键,点击后可跳转到文件保存,可自定义保存路径。

退出按键

按键6为退出按键,点击后会退出程序。

具体功能实现

截屏图片

全屏图片使用QPixmap类中的grabWindow函数,获取后使用setPixmap加载到Label。

Label显示提供预览图片。

隐藏窗口

隐藏窗口为checkBox,在checkBox中定义一个int型的num来为截屏提供是否隐藏窗口的判断,无论是全屏截图,矩形截图,在运行时都会先判断checkBox是否勾选,以此来判断是否隐藏此窗口。

矩形截图

矩形截图会new一个窗口,窗口会先加载全屏截图也是通过QPixmap类中的grabWindow函数,加载完成后会使用setWindowOpacity函数实现模糊背景,模糊背景后重写鼠标事件,

鼠标事件

使鼠标左键,移动,右键联动。鼠标左键按下后,实时记录截图开始位置,若左键再次按下会更新截图位置,重绘画图事件,左键释放后,矩形区域内的部分会使用drawPixmap恢复原图,去除模糊背景,达到截屏的效果。右键会出现可选菜单栏,会有复制,保存,退出等功能。复制功能使用QGuiApplication类中的clipboard()函数复制图片,保存图片为getfilename函数。

提示框

保存或复制后会弹出保存复制成功提示框,该提示框使用QMessageBox类的information函数,可自定义提示内容。

工程链接

https://download.csdn.net/download/longwei7758/86839116

总结:这是QT入门做的第一个小项目,不足之处望各路大佬批评指正。

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值