日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 芯聞號(hào) > 充電吧
[導(dǎo)讀] 最近在學(xué)Qt。學(xué)東西怎么能不動(dòng)手。 就寫了些小程序??碤Q截圖能夠動(dòng)態(tài)吸附直線的功能挺有意思,所以就模仿了一個(gè)。 開發(fā)環(huán)境:VS2013 Qt5.7.1 先上效果圖 界面很簡單。。呵呵

最近在學(xué)Qt。學(xué)東西怎么能不動(dòng)手。

就寫了些小程序??碤Q截圖能夠動(dòng)態(tài)吸附直線的功能挺有意思,所以就模仿了一個(gè)。

開發(fā)環(huán)境:VS2013 Qt5.7.1

先上效果圖

界面很簡單。。呵呵


移動(dòng)鼠標(biāo),會(huì)把鼠標(biāo)所在最小矩形選中。把沒有選中的地方給模糊化,以示我們選中的區(qū)域很清楚。


還可以選中窗口中控件的區(qū)域。


小菜單


編程思路:

1.動(dòng)態(tài)找到鼠標(biāo)所在區(qū)域的矩形,肯定是要獲得桌面上每個(gè)窗口以及其子控件的大小位置屬性。

想獲得這些屬性Qt貌似沒有提供相關(guān)的API,只能用windows的API EnumWindows 和EnumChildWindows枚舉出所有的窗口的位置坐標(biāo)和大小屬性保存在 一個(gè)vector中。

2.有了位置和大?。ū4嬖谝粋€(gè)Rect中就行了)就好辦了。重寫Qt的鼠標(biāo)移動(dòng)事件,自己定義了一個(gè)結(jié)構(gòu)體

[cpp]view plaincopy structMyRect { QRectmyRect_;//矩形 intdistance;//鼠標(biāo)當(dāng)前點(diǎn)到所有邊的距離之和,用于比較 }; 每當(dāng)鼠標(biāo)移動(dòng)就把每個(gè)包含鼠標(biāo)當(dāng)前點(diǎn)的矩形保存到myRect_中并且計(jì)算他的大小distance。

然后找到最小的distance對(duì)應(yīng)的矩形。這個(gè)就是上圖我們要顯示的矩形了。

3.該怎么處理

我是通過QPixmap類的grabWindow獲得整個(gè)屏幕,然后組合繪圖 變色整個(gè)屏幕。

當(dāng)鼠標(biāo)移動(dòng)到某個(gè)區(qū)域時(shí)把這個(gè)區(qū)域清晰顯示。即上圖效果。

4.保存圖片QPixmap的save即可


說了這么多了上代碼吧。

.CPP

[cpp]view plaincopy #include"imagewidget.h" #include #include #include #include #include #include #include #include #include #include std::vectorallWindowRect;//用于存儲(chǔ)所有的窗口 std::vectorallWindowHwnd;//用于存儲(chǔ)所有的窗口句柄 std::vectormyRectRestlt;//找到所有包含鼠標(biāo)當(dāng)前移動(dòng)點(diǎn)的矩形,并保存其到各邊的距離之和。 //聲明回調(diào)函數(shù) boolCALLBACKMyEnumWindowsProc(HWNDhwnd,LPARAMlParam); ImageWidget::ImageWidget(QWidget*parent) :QWidget(parent) { ui.setupUi(this); //用于獲取窗口大小 QDesktopWidget*dtw=QApplication::desktop(); //獲得整個(gè)屏幕 pixmap_=pixmap_.grabWindow(QApplication::desktop()->winId(),0,0,dtw->width(),dtw->height()); isPressed=false; isDragging=false; captureMenu_=newCaptureMenu(); //打開鼠標(biāo)跟蹤 setMouseTracking(true); //關(guān)聯(lián)用于保存文件名 connect(captureMenu_,SIGNAL(toSaveFile(QString)),this,SLOT(slotGetFileName(QString))); //遍歷窗口獲得各個(gè)窗口的大小 ::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移動(dòng)中的X:%3Y:%4")).arg(pStart_.x()).arg(pStart_.y()).arg(pMove_.x()).arg(pMove_.y()); painter.drawText(100,100,tempStr); //顯示截圖拖動(dòng)的區(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)//如果按下鼠標(biāo)開始區(qū)域截圖 { isDragging=true; pMove_.setX(event->x()); pMove_.setY(event->y()); } else//如果沒有按下鼠標(biāo)開始自動(dòng)尋找合適窗口//、應(yīng)該改為找到距離最近的矩形塊。。。?。。。。?! { //每次移動(dòng)都清空 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ā)射信號(hào)給截圖軟件窗口可以顯示 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進(jìn)制數(shù)據(jù) QBufferbuffer(&bytes);//設(shè)置緩存 buffer.open(QIODevice::ReadOnly); pixmapSave_.save(filename,"PNG",1); } voidImageWidget::calculateRectDistance(QRectrect) { intdis=rect.width()+rect.height(); MyRecttempMyRect; tempMyRect.myRect_=rect; tempMyRect.distance=dis; //添加進(jìn)入 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;//鼠標(biāo)當(dāng)前點(diǎn)到所有邊的距離之和,用于比較 }; classImageWidget:publicQWidget { Q_OBJECT public: ImageWidget(QWidget*parent=0); ~ImageWidget(); voidpaintEvent(QPaintEvent*event); //重寫鼠標(biāo)按下事件,記錄截圖起始點(diǎn) voidmousePressEvent(QMouseEvent*event); //重寫鼠標(biāo)松下事件,記錄截圖結(jié)束點(diǎn) voidmouseReleaseEvent(QMouseEvent*event); //重寫鼠標(biāo)移動(dòng)事件,當(dāng)拉動(dòng)截圖區(qū)域時(shí)改變截圖區(qū)域?yàn)檎D片(非蒙塵) voidmouseMoveEvent(QMouseEvent*event); //用于計(jì)算鼠標(biāo)當(dāng)前點(diǎn)到各個(gè)邊的距離之和 voidcalculateRectDistance(QRectrect); private: Ui::ImageWidgetui; QPixmappixmap_;//用于顯示截的整個(gè)屏幕 QPixmappixmapSave_;//用于保存截圖 QPointpStart_;//記錄開始截圖位置 QPointpEnd_;//記錄結(jié)束截圖位置 QPointpMove_;//記錄移動(dòng)中的坐標(biāo) boolisPressed;//是否按下按鈕 boolisDragging;//是否用戶拖選 MyRectminiRect;//最小矩形 CaptureMenu*captureMenu_;//截圖結(jié)束時(shí)的菜單 QStringfullPath;//保存文件名以及路徑 publicslots: voidslotGetFileName(QStringfilename); signals: voidbeVisible();//給截圖軟件發(fā)射可見信號(hào) }; #endif//IMAGEWIDGET_H 不僅動(dòng)態(tài)吸附直線,小菜單中的功能都已實(shí)現(xiàn)。

源碼鏈接:http://download.csdn.net/download/caoshangpa/10134153

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

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉