1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| #include "screen.h" Screen::Screen(QWidget *parent) : QWidget(parent) { beginPos = QPoint(-1,-1); endPos = beginPos; leftPres = false; setMouseTracking(true); rect = new QRect(0,0,QApplication::desktop()->width(),QApplication::desktop()->height()); menu = new QMenu(this); menu->addAction("复制(CTRL+C)", this, SLOT(copyScreen())); menu->addAction("截图另存为(ALT+C)", this, SLOT(saveScreenOther())); menu->addAction("全屏截图(ALT+A)", this, SLOT(grapFullScreen())); menu->addAction("退出截图(ESC)", this, SLOT(hide())); } void Screen::copyScreen() { QGuiApplication::clipboard()->setPixmap(fullScreen.copy(*rect)); } void Screen::contextMenuEvent(QContextMenuEvent *) { this->setCursor(Qt::ArrowCursor); menu->exec(cursor().pos()); } void Screen::mousePressEvent(QMouseEvent *e) { if (e->button() == Qt::LeftButton) { leftPres = true; setBeginPos(e->pos()); } } void Screen::mouseMoveEvent(QMouseEvent *e) { if(leftPres) { setEndPos(e->pos()); } update(); } void Screen::mouseReleaseEvent(QMouseEvent *e) { 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()); } QPoint Screen::getBeginPos() { return beginPos; } QPoint Screen::getEndPos() { return endPos; } void Screen::setBeginPos(QPoint p) { this->beginPos = p; } void Screen::setEndPos(QPoint p) { this->endPos = p; } void Screen::paintEvent(QPaintEvent *) { QPainter painter(this); QPen pen; pen.setColor(Qt::red); pen.setWidth(1); painter.setPen(pen); int lx = beginPos.x()<endPos.x()?beginPos.x():endPos.x(); int ly = beginPos.y()<endPos.y()?beginPos.y():endPos.y(); 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); if(lx!=-1 && w>0 && h>0) { painter.drawPixmap(rect,fullScreen,rect); painter.drawRect(lx, ly, w, h); if(ly>10) { painter.drawText(lx + 2, ly - 8, tr("截图范围(%1,%2) - (%3,%4) 截图大小:(%5 x %6)") .arg(lx).arg(ly).arg(lx + w).arg(ly + h).arg(w).arg(h)); } else { painter.drawText(lx + 2, ly + 12, tr("截图范围(%1,%2) - (%3,%4) 截图大小:(%5 x %6)") .arg(lx).arg(ly).arg(lx + w).arg(ly + h).arg(w).arg(h)); } } painter.drawText(cursor().pos().x(), cursor().pos().y(), tr("(%1,%2)") .arg(cursor().pos().x()).arg(cursor().pos().y())); } void Screen::showEvent(QShowEvent *) { setWindowOpacity(0.7); } void Screen::saveScreenOther() { QString fileName = QFileDialog::getSaveFileName(this, "截图另存为", "", "Image (*.jpg *.png *.bmp)"); if (fileName.length() > 0) { fullScreen.copy(*rect).save(fileName,"bmp"); close(); } } void Screen::grapFullScreen() { endPos.setX(-1); QString fileName = QFileDialog::getSaveFileName(this, "保存全屏截图", "", "JPEG Files (*.jpg)"); if (fileName.length() > 0) { fullScreen.save(fileName, "jpg"); close(); } this->hide(); } void Screen::keyPressEvent(QKeyEvent *e) { if (e->key() == Qt::Key_Escape) { hide(); } else if(e->key() == Qt::Key_C && e->modifiers() == Qt::ControlModifier) { QGuiApplication::clipboard()->setPixmap(fullScreen.copy(*rect)); } else if(e->key() == Qt::Key_C && e->modifiers() == Qt::AltModifier) { saveScreenOther(); } else if(e->key() == Qt::Key_A && e->modifiers() == Qt::AltModifier) { grapFullScreen(); } else { e->ignore(); } }
|