资源简介

一个简单的小例子来看一看如何用Qt实现一个简单的截图功能,详情见博客 http://blog.csdn.net/goforwardtostep/article/details/54237689

资源截图

代码片段和文件信息

#include “capturescreen.h“
#include 
#include 
#include 

CaptureScreen::CaptureScreen(QWidget *parent)
: QWidget(parent)
 m_currentCaptureState(initCapture)
{
initWindow();
loadBackgroundPixmap();
}

CaptureScreen::~CaptureScreen()
{

}

void CaptureScreen::initWindow()
{
this->setMouseTracking(true);
// 由于存在类似QQ这样界面始终显示在最顶层,设置属性 Qt::WindowStaysOnTopHint;
this->setWindowFlags(Qt::framelessWindowHint | Qt::WindowStaysOnTopHint);
setWindowState(Qt::WindowActive | Qt::WindowFullScreen);
this->setMouseTracking(true);
}

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_currentCaptureState == initCapture)
{
m_currentCaptureState = beginCaptureImage;
m_beginPoint = event->pos();
}
else if (event->button() == Qt::LeftButton && isPressPointInSelectRect(event->pos()))
{
m_currentCaptureState = beginMoveCaptureArea;
setCursor(Qt::SizeAllCursor);
m_beginMovePoint = event->pos();
}

return QWidget::mousePressEvent(event);
}

void CaptureScreen::mouseMoveEvent(QMouseEvent* event)
{
if (m_currentCaptureState == beginCaptureImage)
{
m_endPoint = event->pos();
update();
}
else if (m_currentCaptureState == beginMoveCaptureArea)
{
m_endMovePoint = event->pos();
update();
}

// 根据鼠标是否在选中区域内设置鼠标样式;
if (isPressPointInSelectRect(event->pos()))
{
setCursor(Qt::SizeAllCursor);
}
else if (!isPressPointInSelectRect(event->pos()) && m_currentCaptureState != beginMoveCaptureArea)
{
setCursor(Qt::ArrowCursor);
}

return QWidget::mouseMoveEvent(event);
}

void CaptureScreen::mouseReleaseEvent(QMouseEvent *event)
{
if (m_currentCaptureState == beginCaptureImage)
{
m_currentCaptureState = finishCaptureImage;
m_endPoint = event->pos();
update();
}
else if (m_currentCaptureState == beginMoveCaptureArea)
{
m_currentCaptureState = finishMoveCaptureArea;
m_endMovePoint = event->pos();
update();
}
return QWidget::mouseReleaseEvent(event);
}

// 当前鼠标坐标是否在选取的矩形区域内;
bool CaptureScreen::isPressPointInSelectRect(QPoint mousePressPoint)
{
QRect selectRect = getRect(m_beginPoint m_endPoint);
if (selectRect.contains(mousePressPoint))
{
return true;
}

return false;
}

void CaptureScreen::paintEvent(QPaintEvent *event)
{
m_painter.begin(this);          //进行重绘;

QColor shadowColor = QColor(0 0 0 100);                      //阴影颜色设置;
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);           //画影罩效果;

switch (m_currentCaptureState)
{

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6446  2017-01-09 13:33  Qt 之 实现简单截图功能(二)\capturescreen.cpp

     文件       1342  2017-01-09 13:47  Qt 之 实现简单截图功能(二)\capturescreen.h

     目录          0  2017-01-09 13:48  Qt 之 实现简单截图功能(二)

----------- ---------  ---------- -----  ----

                 7788                    3


评论

共有 条评论