QT截图
#ifndef CAPTURESCREEN_H
#define CAPTURESCREEN_H
#include <QWidget>
#include <QPainter>
#include<QRect>
#include<QLabel>
#include<QClipboard>
class CaptureScreen : public QWidget
{
Q_OBJECT
public:
CaptureScreen(QWidget *parent = 0);
~CaptureScreen();
QRect getRect(const QPoint &beginPoint, const QPoint &endPoint);
Q_SIGNALS:
void signalCompleteCature(QPixmap catureImage);
private:
void initWindow();
void loadBackgroundPixmap();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void paintEvent(QPaintEvent *event);
QRect changePosition(const QPoint &beginPoint, const QPoint &endPoint);
private:
bool m_isMousePress1=false,m_isMousePress2=false;
QPixmap m_loadPixmap, m_capturePixmap;
int m_screenwidth;
int m_screenheight;
QPoint m_beginPoint, m_endPoint;
QPainter m_painter;
QPoint m_changePoint,m_begin_movePonit,m_end_movePoint;
QClipboard *clip;
QRect selectedRect;
int flag=0;
QPoint topLeft;
QPoint topRight ;
QPoint bottomLeft ;
QPoint bottomRight;
QPoint leftCenter ;
QPoint topCenter ;
QPoint rightCenter ;
QPoint bottomCenter ;
};
#endif
#include "capturescreen.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QMouseEvent>
#include<QDebug>
CaptureScreen::CaptureScreen(QWidget *parent)
: QWidget(parent)
, m_isMousePress1(false)
{
initWindow();
loadBackgroundPixmap();
}
CaptureScreen::~CaptureScreen()
{
}
void CaptureScreen::initWindow()
{
this->setMouseTracking(true);
this->setWindowFlags(Qt::FramelessWindowHint);
setWindowState(Qt::WindowActive | Qt::WindowFullScreen);
}
void CaptureScreen::loadBackgroundPixmap()
{
m_loadPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
m_screenwidth = m_loadPixmap.width();
m_screenheight = m_loadPixmap.height();
}
void CaptureScreen::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_isMousePress1 = true;
m_isMousePress2 = true;
if(flag==1)
{
m_isMousePress1=false;
m_begin_movePonit=event->pos();
}
else if(flag==0)
{
m_beginPoint = event->pos();
}
}
return QWidget::mousePressEvent(event);
}
void CaptureScreen::mouseMoveEvent(QMouseEvent* event)
{
if (m_isMousePress1)
{
m_endPoint = event->pos();
update();
flag=1;
}
else if(m_isMousePress2)
{
this->setCursor(Qt::SizeAllCursor);
m_end_movePoint = event->pos();
update();
}
return QWidget::mouseMoveEvent(event);
}
void CaptureScreen::mouseReleaseEvent(QMouseEvent *event)
{
m_endPoint = event->pos();
m_end_movePoint=QPoint(0,0);
m_begin_movePonit=QPoint(0,0);
m_isMousePress1 = false;
m_isMousePress2=false;
if(flag==1){m_endPoint=selectedRect.bottomRight();m_beginPoint=selectedRect.topLeft();}
this->setCursor(Qt::ArrowCursor);
clip=QApplication::clipboard();
clip->setPixmap(m_capturePixmap);
return QWidget::mouseReleaseEvent(event);
}
void CaptureScreen::paintEvent(QPaintEvent *event)
{
m_painter.begin(this);
QColor shadowColor = QColor(30, 0, 0, 200);
m_painter.setPen(QPen(Qt::blue, 1, Qt::SolidLine, Qt::FlatCap));
m_painter.drawPixmap(0, 0, m_loadPixmap);
m_painter.fillRect(m_loadPixmap.rect(), shadowColor);
if (m_isMousePress1)
{
selectedRect = getRect(m_beginPoint, m_endPoint);
m_capturePixmap = m_loadPixmap.copy(selectedRect);
m_painter.drawPixmap(selectedRect.topLeft(), m_capturePixmap);
m_painter.drawRect(selectedRect);
}
else if(m_isMousePress2)
{
selectedRect=changePosition(m_begin_movePonit,m_end_movePoint);
m_capturePixmap = m_loadPixmap.copy(selectedRect);
m_painter.drawPixmap(selectedRect.topLeft(), m_capturePixmap);
m_painter.drawRect(selectedRect);
}
topLeft = selectedRect.topLeft();
topRight = selectedRect.topRight();
bottomLeft = selectedRect.bottomLeft();
bottomRight = selectedRect.bottomRight();
QColor color = QColor(30, 174, 255);
leftCenter = QPoint(topLeft.x(), (topLeft.y() + bottomLeft.y()) / 2);
topCenter = QPoint((topLeft.x() + topRight.x()) / 2, topLeft.y());
rightCenter = QPoint(topRight.x(), leftCenter.y());
bottomCenter = QPoint(topCenter.x(), bottomLeft.y());
m_painter.drawRect(QRect(topLeft.x()-2,topLeft.y()-2,6,6));
m_painter.drawRect(QRect(topRight.x()-2,topRight.y()-2,6,6));
m_painter.drawRect(QRect(bottomLeft.x()-2,bottomLeft.y()-2,6,6));
m_painter.drawRect(QRect(leftCenter.x()-2,leftCenter.y()-2,6,6));
m_painter.drawRect(QRect(topCenter.x()-2,topCenter.y()-2,6,6));
m_painter.drawRect(QRect(rightCenter.x()-2,rightCenter.y()-2,6,6));
m_painter.drawRect(QRect(topCenter.x()-2,topCenter.y()-2,6,6));
m_painter.drawRect(QRect(bottomCenter.x()-2,bottomCenter.y()-2,6,6));
m_painter.end();
}
void CaptureScreen::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape)
{
close();
}
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
signalCompleteCature(m_capturePixmap);
close();
}
}
QRect CaptureScreen::getRect(const QPoint &beginPoint, const QPoint &endPoint)
{
int x, y, width, height;
width = qAbs(beginPoint.x() - endPoint.x());
height = qAbs(beginPoint.y() - endPoint.y());
x = beginPoint.x() < endPoint.x() ? beginPoint.x() : endPoint.x();
y = beginPoint.y() < endPoint.y() ? beginPoint.y() : endPoint.y();
selectedRect = QRect(x, y, width, height);
if (selectedRect.width() == 0)
{
selectedRect.setWidth(1);
}
if (selectedRect.height() == 0)
{
selectedRect.setHeight(1);
}
return selectedRect;
}
QRect CaptureScreen::changePosition(const QPoint &beginPoint, const QPoint &endPoint)
{
QRect currentRect = getRect(m_beginPoint, m_endPoint);
m_isMousePress1=false;
m_changePoint=endPoint-beginPoint;
if (currentRect.topLeft().x() + m_changePoint.x() < 2)
{
m_changePoint.setX(2 - currentRect.topLeft().x());
}
if (currentRect.topLeft().y() + m_changePoint.y() < 2)
{
m_changePoint.setY(2 - currentRect.topLeft().y());
}
if (currentRect.bottomRight().x() + m_changePoint.x()+2 > m_screenwidth)
{
m_changePoint.setX(m_screenwidth - currentRect.bottomRight().x()+2);
}
if (currentRect.bottomRight().y() + m_changePoint.y() +2> m_screenheight)
{
m_changePoint.setY(m_screenheight - currentRect.bottomRight().y()+2);
}
if(QRect(leftCenter.x()-2,leftCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==4)
{
setCursor(Qt::SizeHorCursor);
flag=4;
selectedRect.setX(selectedRect.x()+m_changePoint.x());
return selectedRect;
}
else if(QRect(rightCenter.x()-2,rightCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==5)
{
setCursor(Qt::SizeHorCursor);
flag=5;
selectedRect.setWidth(selectedRect.width()+m_changePoint.x());
return selectedRect;
}
else if(QRect(bottomCenter.x()-2,bottomCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==6)
{
setCursor(Qt::SizeVerCursor);
flag=6;
selectedRect.setHeight(selectedRect.height()+m_changePoint.y());
return selectedRect;
}
else if(QRect(topCenter.x()-2,topCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==7)
{
setCursor(Qt::SizeVerCursor);
flag=7;
selectedRect.setY(selectedRect.y()+m_changePoint.y());
return selectedRect;
}
else if(QRect(topLeft.x()-2,topCenter.y()-2,10,10).contains(m_begin_movePonit)||flag==8)
{
setCursor(Qt::SizeVerCursor);
flag=8;
selectedRect.setY(selectedRect.y()+m_changePoint.y());
selectedRect.setX(selectedRect.x()+m_changePoint.x());
return selectedRect;
}
else if(QRect(topRight.x()-2,topRight.y()-2,10,10).contains(m_begin_movePonit)||flag==9)
{
setCursor(Qt::SizeVerCursor);
flag=9;
selectedRect.setX(selectedRect.x()+m_changePoint.y());
selectedRect.setWidth(selectedRect.width()+m_changePoint.x());
return selectedRect;
}
else if(QRect(bottomRight.x()-2,bottomRight.y()-2,10,10).contains(m_begin_movePonit)||flag==10)
{
setCursor(Qt::SizeVerCursor);
flag=10;
selectedRect.setHeight(selectedRect.height()+m_changePoint.y());
selectedRect.setWidth(selectedRect.width()+m_changePoint.x());
return selectedRect;
}
else if(QRect(bottomLeft.x()-2,bottomLeft.y()-2,10,10).contains(m_begin_movePonit)||flag==11)
{
setCursor(Qt::SizeVerCursor);
flag=11;
selectedRect.setHeight(selectedRect.height()+m_changePoint.y());
selectedRect.setX(selectedRect.x()+m_changePoint.x());
return selectedRect;
}
else
{
QPoint m_beginPoint1=m_beginPoint+m_changePoint;
QPoint m_endPoint1=m_end_movePoint+m_changePoint;
qDebug()<<selectedRect<<m_changePoint<<m_begin_movePonit<<m_end_movePoint<<m_beginPoint<<m_endPoint;
selectedRect=getRect(m_beginPoint1,m_endPoint1);
selectedRect.setWidth(qAbs(m_beginPoint.x() - m_endPoint.x()));
selectedRect.setHeight(qAbs(m_beginPoint.y() - m_endPoint.y()));
return selectedRect;
}
}
#include "capturescreen.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CaptureScreen p;
p.show();
return a.exec();
}