XChinux |
2012-07-15 13:07 |
简单的截屏对话框ScreenCutDialog
简单的截屏对话框ScreenCutDialog ,代码仅供参考,无注释。
使用方法:
ScreenCutDialog dlg; dlg.exec(); QPixmap px = dlg.getPixmap();
ScreenCutDialog .hpp
#ifndef SCREENCUTDILAOG_HPP #define SCREENCUTDIALOG_HPP
#include <QtCore/QRect> #include <QtCore/QPoint> #include <QtGui/QDialog> #include <QtGui/QPixmap>
class QMouseEvent; class QPaintEvent;
class ScreenCutDialog : public QDialog { Q_OBJECT public: ScreenCutDialog(QWidget *parent = 0, Qt::WindowFlags f = 0); virtual ~ScreenCutDialog(); QPixmap getPixmap(); protected: virtual void mousePressEvent(QMouseEvent *event); virtual void mouseMoveEvent(QMouseEvent *event); virtual void mouseReleaseEvent(QMouseEvent *event); virtual void mouseDoubleClickEvent(QMouseEvent *event); virtual void paintEvent(QPaintEvent *event); private: bool _bDrag; QPoint _pos; QRect _rect; QImage _image; QImage _darkImage; };
#endif
ScreenCutDialog .cpp
#include <QtGui/QPainter> #include <QtGui/QMouseEvent> #include <QtGui/QPaintEvent> #include <QtGui/QApplication> #include <QtGui/QDesktopWidget> #include "ScreenCutDialog.hpp"
ScreenCutDialog::ScreenCutDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f), _bDrag(false) { setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); setWindowState(Qt::WindowFullScreen); setCursor(Qt::CrossCursor); QDesktopWidget *desk = QApplication::desktop(); setGeometry(desk->availableGeometry()); _image = QPixmap::grabWindow(desk->winId()).toImage(); _darkImage = _image; int bytesPerLine = _darkImage.width() * _darkImage.depth() / 8; int h = _darkImage.height(); for (int i = 0; i < h; i++) { unsigned char *lineBuf = _darkImage.scanLine(i); for (int x = 0; x < bytesPerLine; x++) { lineBuf[x] /= 2; } } }
ScreenCutDialog::~ScreenCutDialog() { }
void ScreenCutDialog::mousePressEvent(QMouseEvent *event) { _bDrag = true; _pos = event->pos(); _rect = QRect(_pos.x(), _pos.y(), 0, 0); QDialog::mousePressEvent(event); }
void ScreenCutDialog::mouseMoveEvent(QMouseEvent *event) { if (_bDrag = true) { QPoint pos = event->pos(); if (pos.x() > _pos.x()) { if (pos.y() > _pos.y()) { _rect = QRect(_pos, pos); } else { _rect = QRect(QPoint(_pos.x(), pos.y()), QPoint(pos.x(), _pos.y())); } } else { if (pos.y() > _pos.y()) { _rect = QRect(QPoint(pos.x(), _pos.y()), QPoint(_pos.x(), pos.y())); } else { _rect = QRect(pos, _pos); } } update(); } QDialog::mouseMoveEvent(event); }
void ScreenCutDialog::mouseReleaseEvent(QMouseEvent *event) { _bDrag = false; QDialog::mouseReleaseEvent(event); accept(); }
void ScreenCutDialog::mouseDoubleClickEvent(QMouseEvent *event) { QDialog::mouseDoubleClickEvent(event); /* if (_rect.contains(event->pos())) { accept(); } */ }
QPixmap ScreenCutDialog::getPixmap() { QPixmap px(_rect.size()); QPainter p(&px); p.drawImage(QRect(0, 0, _rect.width(), _rect.height()), _image, _rect); p.end(); return px; }
void ScreenCutDialog::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.drawImage(0, 0, _darkImage); painter.drawImage(_rect, _image, _rect); painter.drawRect(_rect); }
|
|