当前位置:首页 > > 充电吧
[导读] 最近在学Qt。学东西怎么能不动手。 就写了些小程序。看QQ截图能够动态吸附直线的功能挺有意思,所以就模仿了一个。 开发环境:VS2013 Qt5.7.1 先上效果图 界面很简单。。呵呵

最近在学Qt。学东西怎么能不动手。

就写了些小程序。看QQ截图能够动态吸附直线的功能挺有意思,所以就模仿了一个。

开发环境:VS2013 Qt5.7.1

先上效果图

界面很简单。。呵呵


移动鼠标,会把鼠标所在最小矩形选中。把没有选中的地方给模糊化,以示我们选中的区域很清楚。


还可以选中窗口中控件的区域。


小菜单


编程思路:

1.动态找到鼠标所在区域的矩形,肯定是要获得桌面上每个窗口以及其子控件的大小位置属性。

想获得这些属性Qt貌似没有提供相关的API,只能用windows的API EnumWindows 和EnumChildWindows枚举出所有的窗口的位置坐标和大小属性保存在 一个vector中。

2.有了位置和大小(保存在一个Rect中就行了)就好办了。重写Qt的鼠标移动事件,自己定义了一个结构体

[cpp]view plaincopy structMyRect { QRectmyRect_;//矩形 intdistance;//鼠标当前点到所有边的距离之和,用于比较 }; 每当鼠标移动就把每个包含鼠标当前点的矩形保存到myRect_中并且计算他的大小distance。

然后找到最小的distance对应的矩形。这个就是上图我们要显示的矩形了。

3.该怎么处理

我是通过QPixmap类的grabWindow获得整个屏幕,然后组合绘图 变色整个屏幕。

当鼠标移动到某个区域时把这个区域清晰显示。即上图效果。

4.保存图片QPixmap的save即可


说了这么多了上代码吧。

.CPP

[cpp]view plaincopy #include"imagewidget.h" #include #include #include #include #include #include #include #include #include #include std::vectorallWindowRect;//用于存储所有的窗口 std::vectorallWindowHwnd;//用于存储所有的窗口句柄 std::vectormyRectRestlt;//找到所有包含鼠标当前移动点的矩形,并保存其到各边的距离之和。 //声明回调函数 boolCALLBACKMyEnumWindowsProc(HWNDhwnd,LPARAMlParam); ImageWidget::ImageWidget(QWidget*parent) :QWidget(parent) { ui.setupUi(this); //用于获取窗口大小 QDesktopWidget*dtw=QApplication::desktop(); //获得整个屏幕 pixmap_=pixmap_.grabWindow(QApplication::desktop()->winId(),0,0,dtw->width(),dtw->height()); isPressed=false; isDragging=false; captureMenu_=newCaptureMenu(); //打开鼠标跟踪 setMouseTracking(true); //关联用于保存文件名 connect(captureMenu_,SIGNAL(toSaveFile(QString)),this,SLOT(slotGetFileName(QString))); //遍历窗口获得各个窗口的大小 ::EnumWindows((WNDENUMPROC)MyEnumWindowsProc,0); } ImageWidget::~ImageWidget() { } voidImageWidget::paintEvent(QPaintEvent*event) { QPainterpainter(this); pixmap_=pixmap_.scaled(width(),height(),Qt::KeepAspectRatio); //pixmap_没有alpha通道添加通道 QPixmaptemp(pixmap_.size()); temp.fill(Qt::transparent); QPainterp(&temp); p.setCompositionMode(QPainter::CompositionMode_Source); p.drawPixmap(0,0,pixmap_); p.setCompositionMode(QPainter::CompositionMode_DestinationIn); p.fillRect(temp.rect(),QColor(50,50,50,100));//把图片调暗以显示截图全屏 //pixmap_=temp; //水印???? painter.drawPixmap(0,0,temp); QPenpenWather; penWather.setWidth(10); penWather.setBrush(QColor(125,125,125,125)); painter.setPen(penWather); QStringtempStr; tempStr=QString(tr("开始按钮X:%1Y:%2移动中的X:%3Y:%4")).arg(pStart_.x()).arg(pStart_.y()).arg(pMove_.x()).arg(pMove_.y()); painter.drawText(100,100,tempStr); //显示截图拖动的区域 QPenpen; pen.setWidth(5); pen.setColor(QColor(0,255,255,127)); painter.setPen(pen); if(isDragging) { painter.drawPixmap(pStart_.x(),pStart_.y(),pixmap_,pStart_.x(),pStart_.y(),pMove_.x()-pStart_.x(),pMove_.y()-pStart_.y()); painter.drawRect(pStart_.x()-2,pStart_.y()-2,pMove_.x()-pStart_.x()-2,pMove_.y()-pStart_.y()-2); } else { painter.drawPixmap(miniRect.myRect_.left(),miniRect.myRect_.top(),pixmap_,miniRect.myRect_.left(),miniRect.myRect_.top(),miniRect.myRect_.width(),miniRect.myRect_.height()); painter.drawRect(miniRect.myRect_.left()-2,miniRect.myRect_.top()-2,miniRect.myRect_.width()-2,miniRect.myRect_.height()-2); } } voidImageWidget::mousePressEvent(QMouseEvent*event) { pStart_.setX(event->x()); pStart_.setY(event->y()); isPressed=true; } voidImageWidget::mouseMoveEvent(QMouseEvent*event) { if(isPressed)//如果按下鼠标开始区域截图 { isDragging=true; pMove_.setX(event->x()); pMove_.setY(event->y()); } else//如果没有按下鼠标开始自动寻找合适窗口//、应该改为找到距离最近的矩形块。。。!!!!!! { //每次移动都清空 myRectRestlt.clear(); for(std::vector::iteratorit=allWindowRect.begin()+1;it!=allWindowRect.end();it++) { if(it->contains(event->x(),event->y())) { calculateRectDistance(*it); } } MyRecttempMinRect; for(std::vector::iteratorit=myRectRestlt.begin();it!=myRectRestlt.end();it++) { if(it->distancex()); pEnd_.setY(event->y()); } else { pStart_.setX(miniRect.myRect_.left()); pStart_.setY(miniRect.myRect_.top()); pEnd_.setX(miniRect.myRect_.right()); pEnd_.setY(miniRect.myRect_.bottom()); } isPressed=false; //isDragging=false; //新建菜单窗口 captureMenu_->move(event->x()-152,event->y()); captureMenu_->setWindowFlags(Qt::FramelessWindowHint); captureMenu_->exec(); //退出窗口 close(); //发射信号给截图软件窗口可以显示 emitbeVisible(); } //回调函数 boolCALLBACKMyEnumWindowsProc(HWNDhwnd,LPARAMlParam) { if(::IsWindow(hwnd)&&::IsWindowVisible(hwnd)) { RECTtempRect; QRecttempQRect; ::GetWindowRect(hwnd,&tempRect); tempQRect.setTopLeft(QPoint(tempRect.left,tempRect.top)); tempQRect.setBottomRight(QPoint(tempRect.right,tempRect.bottom)); allWindowRect.push_back(tempQRect); allWindowHwnd.push_back(hwnd); ::EnumChildWindows(hwnd,(WNDENUMPROC)MyEnumWindowsProc,0); } returntrue; } voidImageWidget::slotGetFileName(QStringfilename) { pixmapSave_=pixmap_.copy(pStart_.x(),pStart_.y(),pEnd_.x()-pStart_.x(),pEnd_.y()-pStart_.y()); //保存截图 QByteArraybytes;//用于存放2进制数据 QBufferbuffer(&bytes);//设置缓存 buffer.open(QIODevice::ReadOnly); pixmapSave_.save(filename,"PNG",1); } voidImageWidget::calculateRectDistance(QRectrect) { intdis=rect.width()+rect.height(); MyRecttempMyRect; tempMyRect.myRect_=rect; tempMyRect.distance=dis; //添加进入 myRectRestlt.push_back(tempMyRect); } .H

[cpp]view plaincopy #ifndefIMAGEWIDGET_H #defineIMAGEWIDGET_H #include #include"ui_imagewidget.h" #include #include #include #include #include structMyRect { QRectmyRect_;//矩形 intdistance;//鼠标当前点到所有边的距离之和,用于比较 }; classImageWidget:publicQWidget { Q_OBJECT public: ImageWidget(QWidget*parent=0); ~ImageWidget(); voidpaintEvent(QPaintEvent*event); //重写鼠标按下事件,记录截图起始点 voidmousePressEvent(QMouseEvent*event); //重写鼠标松下事件,记录截图结束点 voidmouseReleaseEvent(QMouseEvent*event); //重写鼠标移动事件,当拉动截图区域时改变截图区域为正常图片(非蒙尘) voidmouseMoveEvent(QMouseEvent*event); //用于计算鼠标当前点到各个边的距离之和 voidcalculateRectDistance(QRectrect); private: Ui::ImageWidgetui; QPixmappixmap_;//用于显示截的整个屏幕 QPixmappixmapSave_;//用于保存截图 QPointpStart_;//记录开始截图位置 QPointpEnd_;//记录结束截图位置 QPointpMove_;//记录移动中的坐标 boolisPressed;//是否按下按钮 boolisDragging;//是否用户拖选 MyRectminiRect;//最小矩形 CaptureMenu*captureMenu_;//截图结束时的菜单 QStringfullPath;//保存文件名以及路径 publicslots: voidslotGetFileName(QStringfilename); signals: voidbeVisible();//给截图软件发射可见信号 }; #endif//IMAGEWIDGET_H 不仅动态吸附直线,小菜单中的功能都已实现。

源码链接:http://download.csdn.net/download/caoshangpa/10134153

原文链接:http://blog.csdn.net/kfbyj/article/details/8811010

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

Qt是一款由Qt公司(前身为Trolltech)开发的跨平台应用程序框架。它提供了丰富的功能,包括图形用户界面、数据库操作、网络通信等,使得开发者能够更加便捷地创建高质量、可移植性强的应用程序。Qt采用C++编写,同时也...

关键字: QT RTOS

摘 要 :为解决制鞋行业中喷胶精度不高、灵活性差、生产效率低的问题,设计一种基于机器视觉的鞋模喷胶系统。该系统硬件由工业摄像头、工控机及路由器构成,软件则采用图像识别库 OpenCV 与图形界面应用程序开发框架 Qt 编...

关键字: 机器视觉 图像处理 鞋模 喷胶 OpenCV QT

嵌入式系统是指以应用为中心、以计算机技术为基础,软件硬件可裁剪、适应应用系统对功能、可靠性、成本、体积、功耗严格要求的专用计算机系统。

关键字: QT 嵌入式 C++

摘 要:仓储作为物流与供应链的核心环节,对食品安全的控制起着至关重要的作用。温湿度是影响粮食仓储过程安全与品质的重要因素。文中使用无线传感网络进行数据采集,通过Qt平台设计软件系统,并借助数据库进行数据存储与分析处理,实...

关键字: 成品粮 仓储 温湿度监测 无线传感网络 QT

在此部件上绘制行号,并将其放置在CodeEditor的viewport()的左边距区域上,QWidget类也可以帮助我们对其内容进行滚动。

关键字: QT 代码编辑器

不管是Qt新手还是开发过qt的群体来说,对Qt Designer、Qt Quick Designer、Qt Creator这几个应用程序肯定是熟悉的。

关键字: QT IDE C

在当今社会,人们的生活水平普遍提高,工作强度越来越大,营养的过剩和运动量的减少,导致心脑血管疾病的发病率是越来越高。

关键字: Linux QT GPRS 远程集群式 心脏病人实时诊断系

qt值得学习吗? 嵌入式要学的东西真的很多,我们可能会说不写界面的话就不用学qt了?我不赞同。

关键字: 嵌入式 QT UI

什么是qt?简单点说,Qt 就是一个跨平台的 C++ 图形用户界面库,可以同时支持桌面应用程序开发、嵌入式开发和移动开发,覆盖了现有的所有主流平台。

关键字: QT 程序 开发

qt值得学习吗? 嵌入式要学的东西真的很多,我们可能会说不写界面的话就不用学qt了?我不赞同,原因是……

关键字: 嵌入式 QT
关闭