Qt实现对选中区域屏幕截图和窗⼝图像保存
//.cpp
#include"paintscreenshot.h"
#include
#include
#include
#include
#include"screenshot.h"
PaintScreenshot::PaintScreenshot(QWidget*parent)
:QWidget(p百二秦关 arent)
,m_bIsMouPress(fal)
{
initWindow();
loadBackgroundPixmap();
}
PaintScreenshot::~PaintScreenshot()
{
}
voidPaintScreenshot::initWindow()
{
this->tMouTracking(true);
this->tWindowFlags(Qt::FramelessWindowHint);
tWindowState(Qt::WindowActive|Qt::WindowFullScreen);
}
voidPaintScreenshot::loadBackgroundPixmap()
{
m_loadPixmap=QPixmap::grabWindow(QApplication::desktop()->winId());//衣服海报 抓取当前屏幕的图⽚;
m_n幼儿安全教育教案 Screenwidth=m_();
m_nScreenhei正处级干部 ght=m_();
}
voidPaintScreenshot::mouPressEvent(QMouEvent*event)
{
if(event->button()==Qt::LeftButton)
{
m_bIsMouPress=true;
m_beginPoint=event->pos();
}
returnQWidget::mouPressEvent(event);
}
voidPaintScreenshot::mouMoveEvent(QMouEvent*event)
{
if(m_bIsMouPress)
{
m_endPoint=event->pos();
update();
}
returnQWidget::mouMoveEvent(event);
}
voidPaintScreenshot::mouReleaEvent(QMouEvent*event)
{
m_endPoint=event->pos();
m_bIsMouPress=fal;
if(nullptr!=m_pScreenshotWindow)
{
m_pScreenshotWindow->SendImageSaveAndShow(m_capturePixmap);
}
clo();
returnQWidget::mouReleaEvent(event);
}
voidPaintScreenshot::paintEvent(QPaintEvent*event)
{
m_(this);//进⾏重绘;
QColormShadowColor=QColor(0,0,0,100);//阴影颜⾊设置;
m_(QPen(Qt::blue,1,Qt::SolidLine,Qt::FlatCap));//设置画笔;
m_xmap(0,0,m_loadPixmap);//将背景图⽚画到窗体上;
m_ct(m_(),mShadowColor);//画影罩效果;
if(m_bIsMouPress)
{
QRectmSe产后能吃阿胶吗 lectedRect=getRect(m_beginPoint,m_endPoint);
m_capturePixmap=m_(mSelectedRect);
m_xmap(t(),m_capturePixmap);
m_ct(mSelectedRect);
}
m_();//重绘结束;
returnQWidget::paintEvent(event);
}
voidPaintScreenshot::keyPressEvent(QKeyEvent*event)
{
////Esc键退出截图;
//if(event->key()==Qt::Key_Escape)
//{
//clo();
//}
//Eeter键完成截图;
//if(event->key()==Qt::Key_Return||event->key()==Qt::Key_Enter)
//{
//emitsignalCompleteCature(m_capturePixmap);
//clo();
//}
returnQWidget::keyPressEvent(event);
}
QRectPaintScreenshot::getRect(constQPoint&beginPoint,constQPoint&endPoint)
{
intx,y,nWidth,nHeight;
nWidth=qAbs(beginPoint.x()-endPoint.x());
nHeight=qAbs(beginPoint.y()-e嘎达汤 ndPoint.y());
x=beginPoint.x()
y=beginPoint.y()
QRectmSelectedRect=QRect(x,y,nWidth,nHeight);
//避免宽或⾼为零时拷贝截图有误;
//可以看QQ截图,当选取截图宽或⾼为零时默认为2;
if(()==0)
{
th(1);
}
if(()==0)
{
ght(1);
}
returnmSelectedRect;
}
voidPaintScreenshot::SendScreenshotWindowPoint(Sc腹泻草 reenshot*mScreenshotWindowPoint)
{
if(nullptr!=mScreenshotWindowPoint)
{
m_pScreenshotWindow=mScreenshotWindowPoint;
}
}
//.h
#ifndefPAINTSCREENSHOT_H
#definePAINTSCREENSHOT_H
#include
#include
#include
classScreenshot;
classPaintScreenshot:publicQWidget
{
public:
PaintScreenshot(QWidget*parent=0);
~PaintScreenshot();
voidSendScreenshotWindowPoint(Screenshot*);
private:
voidinitWindow();
voidloadBackgroundPixmap();
QRectgetRect(constQPoint&mBeginPoint,constQPoint&mEndPoint);
voidmouPressEvent(QMouEven数学童话故事 t*event);
voidmouMoveEvent(QMouEvent*event);
voidmouReleaEvent(QMouEvent*event);
voidkeyPressEvent(QKeyEvent*event);
voidpaintEvent(QPaintEvent*event);
private:
boolm_bIsMouPress;
QPixmapm_loadPixmap,m_capturePixmap;
intm_nScreenwidth;
intm_nScreenheight;
QPointm_beginPoint,m_endPoint;
QPainterm_painter;
Screenshot*m_pScreenshotWindow;//接收截图窗⼝的指针地址
};
#endif//PAINTSCREENSHOT_H
本文发布于:2023-03-18 23:05:04,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1679151907303986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:window截图.doc
本文 PDF 下载地址:window截图.pdf
留言与评论(共有 0 条评论) |