最近在學Qt。學東西怎么能不動手。
就寫了些小程序??碤Q截圖能夠動態(tài)吸附直線的功能挺有意思,所以就模仿了一個。
開發(fā)環(huán)境:VS2013 Qt5.7.1
先上效果圖
界面很簡單。。呵呵
移動鼠標,會把鼠標所在最小矩形選中。把沒有選中的地方給模糊化,以示我們選中的區(qū)域很清楚。
還可以選中窗口中控件的區(qū)域。
小菜單
編程思路:
1.動態(tài)找到鼠標所在區(qū)域的矩形,肯定是要獲得桌面上每個窗口以及其子控件的大小位置屬性。
想獲得這些屬性Qt貌似沒有提供相關的API,只能用windows的API EnumWindows 和EnumChildWindows枚舉出所有的窗口的位置坐標和大小屬性保存在 一個vector中。
2.有了位置和大?。ū4嬖谝粋€Rect中就行了)就好辦了。重寫Qt的鼠標移動事件,自己定義了一個結構體
[cpp]view
plaincopy
structMyRect
{
QRectmyRect_;//矩形
intdistance;//鼠標當前點到所有邊的距離之和,用于比較
};
每當鼠標移動就把每個包含鼠標當前點的矩形保存到myRect_中并且計算他的大小distance。
然后找到最小的distance對應的矩形。這個就是上圖我們要顯示的矩形了。
3.該怎么處理
我是通過QPixmap類的grabWindow獲得整個屏幕,然后組合繪圖 變色整個屏幕。
當鼠標移動到某個區(qū)域時把這個區(qū)域清晰顯示。即上圖效果。
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;//找到所有包含鼠標當前移動點的矩形,并保存其到各邊的距離之和。
//聲明回調(diào)函數(shù)
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);
//關聯(lián)用于保存文件名
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));//把圖片調(diào)暗以顯示截圖全屏
//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);
//顯示截圖拖動的區(qū)域
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)//如果按下鼠標開始區(qū)域截圖
{
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();
//發(fā)射信號給截圖軟件窗口可以顯示
emitbeVisible();
}
//回調(diào)函數(shù)
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進制數(shù)據(jù)
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);
//重寫鼠標移動事件,當拉動截圖區(qū)域時改變截圖區(qū)域為正常圖片(非蒙塵)
voidmouseMoveEvent(QMouseEvent*event);
//用于計算鼠標當前點到各個邊的距離之和
voidcalculateRectDistance(QRectrect);
private:
Ui::ImageWidgetui;
QPixmappixmap_;//用于顯示截的整個屏幕
QPixmappixmapSave_;//用于保存截圖
QPointpStart_;//記錄開始截圖位置
QPointpEnd_;//記錄結束截圖位置
QPointpMove_;//記錄移動中的坐標
boolisPressed;//是否按下按鈕
boolisDragging;//是否用戶拖選
MyRectminiRect;//最小矩形
CaptureMenu*captureMenu_;//截圖結束時的菜單
QStringfullPath;//保存文件名以及路徑
publicslots:
voidslotGetFileName(QStringfilename);
signals:
voidbeVisible();//給截圖軟件發(fā)射可見信號
};
#endif//IMAGEWIDGET_H
不僅動態(tài)吸附直線,小菜單中的功能都已實現(xiàn)。
源碼鏈接:http://download.csdn.net/download/caoshangpa/10134153
原文鏈接:http://blog.csdn.net/kfbyj/article/details/8811010