#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