QT 模仿QQ的截图框 支持基本的拖拉 移动 固定大小 可以复制到剪贴板和保存到文件中

#include "widget.h"
#include "InputDialog.h"
#include <QtGui>
Widget::Widget(QWidget *parent,QColor selectedColor,int quality)
    : QDialog(parent),quality(quality),sColor(selectedColor)
{

    saveAt=new QAction(tr("保存到文件"),this);
    saveAt->setIcon(QIcon(":/image/save.png"));
    saveTob=new QAction(tr("保存到剪贴板"),this);
    saveTob->setIcon(QIcon(":/image/savetob.png"));
    closeAt=new QAction(tr("退出"),this);
    closeAt->setIcon(QIcon(":/image/quit.png"));
    helpAt=new QAction(tr("使用帮助"),this);
    helpAt->setIcon(QIcon(":/image/help.png"));
    fixedSizeAt=new QAction(tr("设置固定尺寸"),this);
    fixedSizeAt->setIcon(QIcon(":/image/fixedsize.png"));
    menu=new QMenu(tr("截图菜单"),this);
    menu->setStyleSheet("background-image:url(:/image/back.png);color:white");
    menu->addAction(saveAt);
    menu->addAction(saveTob);
    menu->addSeparator();
    menu->addAction(fixedSizeAt);
    menu->addSeparator();
    menu->addAction(helpAt);
    menu->addSeparator();
    menu->addAction(closeAt);
    connect(saveAt,SIGNAL(triggered()),this,SLOT(saveImage()));
    connect(saveTob,SIGNAL(triggered()),this,SLOT(saveBoard()));
    connect(closeAt,SIGNAL(triggered()),this,SLOT(close()));
    connect(helpAt,SIGNAL(triggered()),this,SLOT(emitHelp()));
    connect(fixedSizeAt,SIGNAL(triggered()),this,SLOT(input()));
    this->setMouseTracking(true);
    leftPressed=rightPressed=runOnce=done=pull=bresize=showm=false;
    fullSize=QApplication::desktop()->size();
    this->resize(fullSize);
    this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
    //this->hide();
    //获取了当前屏幕并存储在pixmap
    screen=new QPixmap(QPixmap::grabWindow(QApplication::desktop()->winId(),
                                           0,0,fullSize.width(),fullSize.height()));
    //新建一层朦胧屏幕
    fogPix=new QPixmap(*screen);
    QPixmap fog(fullSize.width(),fullSize.height());
    fog.fill(QColor(182,182,182,192));
    QPainter painter(fogPix);
    painter.drawPixmap(0,0,fog);
    painter.setPen(QPen(QColor(255,255,0)));
    painter.drawText(fullSize.width()/2-50,50,tr("如需使用帮助,请按F1"));
}

void Widget::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    QPen pen;
    pen.setColor(Qt::yellow);
    pen.setWidth(1);
    painter.setPen(pen);
    painter.drawPixmap(0,0,*fogPix);
    QPainter painterInfo(this);
    if(pos.tlp.x()!=pos.brp.x()&&
            pos.tlp.y()!=pos.brp.y())
    {
        QPoint zPoint(1,1);

        painterInfo.setPen(QPen(sColor));
        painterInfo.drawRect(QRect(pos.tlp-zPoint,pos.brp));
        painterInfo.drawRect(pos.tlp.x()-3,pos.tlp.y()-3,3,3);
        painterInfo.drawRect(pos.trp.x(),pos.trp.y()-3,3,3);
        painterInfo.drawRect(pos.blp.x()-3,pos.blp.y()+1,3,3);
        painterInfo.drawRect(pos.brp.x()+1,pos.brp.y()+1,3,3);

        painterInfo.drawRect((pos.tlp.x()+pos.trp.x())/2-2,
                             (pos.tlp.y()-4),3,3);
        painterInfo.drawRect(pos.tlp.x()-4,
                             (pos.tlp.y()+pos.blp.y())/2-2,3,3);
        painterInfo.drawRect(pos.trp.x()+1,
                             (pos.trp.y()+pos.brp.y())/2-2,3,3);
        painterInfo.drawRect((pos.blp.x()+pos.brp.x())/2-2,
                             pos.blp.y()+1,3,3);
        painterInfo.drawPixmap(pos.tlp,
                               screen->copy(QRect(pos.tlp,pos.brp)));
        painterInfo.setBrush(QBrush(QColor(200,200,200,165)));
        painterInfo.setPen(QPen(QColor(200,200,200,165)));
        painterInfo.drawRect(pos.tlp.x(),pos.tlp.y()>=40?
                                 pos.tlp.y()-40:pos.tlp.y(),150,40);
        painterInfo.setPen(QPen(Qt::black));
        painterInfo.drawText(pos.tlp.x()+10,
                            pos.tlp.y()>=40?pos.tlp.y()-25:pos.tlp.y()+15,
                             tr("截图开始坐标(%1,%2)").arg(QString::number(pos.tlp.x()),
                                                     QString::number(pos.tlp.y())));
        painterInfo.drawText(pos.tlp.x()+10,
                             pos.tlp.y()>=40?pos.tlp.y()-10:pos.tlp.y()+30,
                             tr("截图尺寸大小(%1,%2)").arg(QString::number(pos.brp.x()-pos.tlp.x()),
                                                     QString::number(pos.brp.y()-pos.tlp.y())));
    }
    else
    {
        cursor.setShape(Qt::ArrowCursor);
        this->setCursor(cursor);
    }
}
void Widget::mousePressEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton)
    {
        leftPressed=true;
        if(!(e->globalPos().x()>pos.tlp.x()&&
             e->globalPos().y()>pos.tlp.y()&&
             e->globalPos().x()<pos.brp.x()&&
             e->globalPos().y()<pos.brp.y())&&
                !(e->globalPos().x()>=pos.tlp.x()-5&&
                  e->globalPos().x()<=pos.tlp.x()-1&&
                  e->globalPos().y()>=pos.tlp.y()-5&&
                  e->globalPos().y()<=pos.tlp.y()-1)&&
                !(e->globalPos().x()>=pos.trp.x()+1&&
                  e->globalPos().x()<=pos.trp.x()+5&&
                  e->globalPos().y()<=pos.trp.y()+1&&
                  e->globalPos().y()>=pos.trp.y()-5)&&
                !(e->globalPos().x()>=pos.blp.x()-5&&
                  e->globalPos().x()<=pos.blp.x()-1&&
                  e->globalPos().y()<=pos.blp.y()+5&&
                  e->globalPos().y()>=pos.blp.y()+1)&&
                !(e->globalPos().x()>=pos.brp.x()+1&&
                  e->globalPos().x()<=pos.brp.x()+5&&
                  e->globalPos().y()>=pos.brp.y()+1&&
                  e->globalPos().y()<=pos.brp.y()+5)&&
                !((e->globalPos().x()>=(pos.tlp.x()+pos.trp.x())/2-2&&
                   e->globalPos().x()<=(pos.tlp.x()+pos.trp.x())/2+2&&
                   e->globalPos().y()<=pos.tlp.y()-1&&
                   e->globalPos().y()>=pos.tlp.y()-4))&&
                !(e->globalPos().x()>=pos.tlp.x()-4&&
                  e->globalPos().x()<=pos.tlp.x()-1&&
                  e->globalPos().y()>=(pos.tlp.y()+pos.blp.y())/2-2&&
                  e->globalPos().y()<=(pos.tlp.y()+pos.blp.y())/2+2)&&
                !(e->globalPos().x()>=(pos.blp.x()+pos.brp.x())/2-2&&
                 e->globalPos().x()<=(pos.blp.x()+pos.brp.x())/2+2&&
                 e->gl
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值