最近在学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