QT-------截图工具

screenshot.h source code:

#ifndef SCREENSHOT_H
#define SCREENSHOT_H
 
 
#include <QWidget>
#include <QMainWindow>
#include <QTimer>
#include <QPixmap>
#include <QMessageBox>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QDesktopServices>
#include <QDir>
#include <QDebug>
#include <QClipboard>
#include <QContextMenuEvent>
#include <QMenu>
#include <QAction>
#include <QCursor>
#include <QProcess>
 
 
namespace Ui {
class screenshot;
}
 
 
class screenshot : public QWidget
{
    Q_OBJECT
 
 
public:
    explicit screenshot(QWidget *parent = 0);
    ~screenshot();
protected:
    void contextMenuEvent(QContextMenuEvent *event);
private slots:
    void on_pushButton_clicked();
    void shotScreenSlot();
    void savePictureSlot();
    void startNotepadSlot();
    void on_pushButton_3_clicked();
 
 
private:
    Ui::screenshot *ui;
    QTimer *timer;
    QPixmap pixmap;//截取图片
};
 
 

#endif // SCREENSHOT_H

screenshot.cpp source code:

#include "screenshot.h"
#include "ui_screenshot.h"
 
 
screenshot::screenshot(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::screenshot)
{
    ui->setupUi(this);
    QObject::connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(savePictureSlot()));
}
 
 
screenshot::~screenshot()
{
    delete ui;
}
 
 
void screenshot::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *menu=new QMenu(this);
    QAction *action=new QAction(this);
    QAction *processAction=new QAction(this);
    QObject::connect(processAction,SIGNAL(triggered()),this,SLOT(startNotepadSlot()));
    QObject::connect(action,SIGNAL(triggered()),this,SLOT(savePictureSlot()));
    action->setText("save As");
    processAction->setText("start notepad");
    menu->addAction(processAction);
    menu->addAction(action);
    menu->exec(QCursor::pos());
 
 
}
void screenshot::on_pushButton_clicked()
{
    if(ui->checkBox->isChecked())
    {
        this->hide();//隐葳窗体
        this->timer=new QTimer;
        QObject::connect(this->timer,SIGNAL(timeout()),SLOT(shotScreenSlot()));
        this->timer->start(1000/5);
    }
    else
    {
        qApp->beep();//泵的一声
    }
}
 
 
void screenshot::shotScreenSlot()
{
    //pixmap
    this->pixmap=QPixmap::grabWindow(QApplication::desktop()->winId());
    ui->label->setPixmap(this->pixmap.scaled(ui->label->size()));
    QClipboard *clipboard=QApplication::clipboard();
    clipboard->setPixmap(this->pixmap);
    this->show();
    this->timer->stop();
}
 
 
void screenshot::savePictureSlot()
{
    //QString fileName=QDir::currentPath();
    QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),"./未命命.bmp",tr("images(*.bmp)"));
    this->pixmap.save(fileName);
}
 
 
void screenshot::startNotepadSlot()
{
    QProcess *process=new QProcess;
    process->start("notepad.exe");
}
void screenshot::on_pushButton_3_clicked()
{
    this->close();
}
 
 

main.cpp source code:

#include "screenshot.h"
#include <QApplication>
#include <QWidget>
#include <QDir>
#include <QIcon>
 
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString strPath=QApplication::applicationDirPath();
    strPath+="/convert.png";
    a.setWindowIcon(QIcon(strPath));
    screenshot w;
    w.setMaximumSize(884,765);
    w.setMinimumSize(884,765);
    w.show();
    return a.exec();
}
 
  
 

 

猜你喜欢

转载自blog.csdn.net/u013934107/article/details/80906785