查看完整版本: [-- 简单的截屏对话框ScreenCutDialog --]

QTCN开发网 -> Qt代码秀 -> 简单的截屏对话框ScreenCutDialog [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

XChinux 2012-07-15 13:07

简单的截屏对话框ScreenCutDialog

简单的截屏对话框ScreenCutDialog ,代码仅供参考,无注释。

使用方法:

  1. ScreenCutDialog dlg;
    dlg.exec();
    QPixmap px = dlg.getPixmap();



ScreenCutDialog .hpp

  1. #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

  1. #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);
    }

zkh2006 2012-07-15 13:57
版主  貌似改过版了啊

kimtaikee 2012-07-15 16:04
恩,在悄无声息中进行的,”代码秀“是新加的版块吧,而却白老师更加积极了

XChinux 2012-07-15 16:42
一直想对论坛做修改,所以原来一直是只有论坛,而没有其它内容,就是为了一旦开始改版时修改程序或移植程序时方便,但一直工作忙。去年有个大改计划,但后来夭折了,只处理了一部分,现在在想如何逐步处理。因为现在开放的功能比较多,所以不打算升级phpwind程序了,怕不兼容的地方太多。discuz对移动设备的支持要比phpwind好,但还是因为开放功能比较多,怕移植到discuz麻烦。这段时间发生的事情,导致大家对Qt的前途感到担忧,有感当时MeeGo兴趣的许多网站,现在大多都关门了,Qt发展的现状估计又会有一批网站会关闭,qt.csdn.net是Nokia与CSDN的官方合作,但尽管会有保留,但估计会没有官方合作的名号了。meego丢失了csdn一级频道,qt不要丢失啊。感到有必要在QTCN中有所动作,现在逐步做些改良吧。Qt桌面的发展问题不大,但现在移动平台、Windows下Metro界面开发,这两大问题,现在失去大公司的快速推动,进展会比较慢,想有作为,qt爱好者我们自己多贡献些吧。

alexltr 2012-07-16 23:22
    

gongkongrs 2012-07-18 22:10
      

宁静灬致远 2012-07-23 22:01
支持下!

lyjbbq 2012-08-17 09:12
  

ppdayz 2012-08-21 16:35
      

xzoscar 2012-08-21 17:27
该贴是杰作,我也得到过真传,呵呵,顶一贴

ltpgt 2012-10-20 23:44

dayqr 2012-11-24 17:06
顶一贴

xpz123 2018-01-08 22:18
    


查看完整版本: [-- 简单的截屏对话框ScreenCutDialog --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled