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

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

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

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

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

先上效果圖

界面很簡單。。呵呵


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


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


小菜單


編程思路:

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

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

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

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

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

3.該怎么處理

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

當(dāng)鼠標(biāo)移動到某個區(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;//找到所有包含鼠標(biāo)當(dāng)前移動點的矩形,并保存其到各邊的距離之和。 //聲明回調(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(); //打開鼠標(biāo)跟蹤 setMouseTracking(true); //關(guān)聯(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)//如果按下鼠標(biāo)開始區(qū)域截圖 { isDragging=true; pMove_.setX(event->x()); pMove_.setY(event->y()); } else//如果沒有按下鼠標(biāo)開始自動尋找合適窗口//、應(yī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ā)射信號給截圖軟件窗口可以顯示 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);//設(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; //添加進入 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)前點到所有邊的距離之和,用于比較 }; classImageWidget:publicQWidget { Q_OBJECT public: ImageWidget(QWidget*parent=0); ~ImageWidget(); voidpaintEvent(QPaintEvent*event); //重寫鼠標(biāo)按下事件,記錄截圖起始點 voidmousePressEvent(QMouseEvent*event); //重寫鼠標(biāo)松下事件,記錄截圖結(jié)束點 voidmouseReleaseEvent(QMouseEvent*event); //重寫鼠標(biāo)移動事件,當(dāng)拉動截圖區(qū)域時改變截圖區(qū)域為正常圖片(非蒙塵) voidmouseMoveEvent(QMouseEvent*event); //用于計算鼠標(biāo)當(dāng)前點到各個邊的距離之和 voidcalculateRectDistance(QRectrect); private: Ui::ImageWidgetui; QPixmappixmap_;//用于顯示截的整個屏幕 QPixmappixmapSave_;//用于保存截圖 QPointpStart_;//記錄開始截圖位置 QPointpEnd_;//記錄結(jié)束截圖位置 QPointpMove_;//記錄移動中的坐標(biāo) boolisPressed;//是否按下按鈕 boolisDragging;//是否用戶拖選 MyRectminiRect;//最小矩形 CaptureMenu*captureMenu_;//截圖結(jié)束時的菜單 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

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

Qt是一款由Qt公司(前身為Trolltech)開發(fā)的跨平臺應(yīng)用程序框架。它提供了豐富的功能,包括圖形用戶界面、數(shù)據(jù)庫操作、網(wǎng)絡(luò)通信等,使得開發(fā)者能夠更加便捷地創(chuàng)建高質(zhì)量、可移植性強的應(yīng)用程序。Qt采用C++編寫,同時也...

關(guān)鍵字: QT RTOS

摘 要 :為解決制鞋行業(yè)中噴膠精度不高、靈活性差、生產(chǎn)效率低的問題,設(shè)計一種基于機器視覺的鞋模噴膠系統(tǒng)。該系統(tǒng)硬件由工業(yè)攝像頭、工控機及路由器構(gòu)成,軟件則采用圖像識別庫 OpenCV 與圖形界面應(yīng)用程序開發(fā)框架 Qt 編...

關(guān)鍵字: 機器視覺 圖像處理 鞋模 噴膠 OpenCV QT

嵌入式系統(tǒng)是指以應(yīng)用為中心、以計算機技術(shù)為基礎(chǔ),軟件硬件可裁剪、適應(yīng)應(yīng)用系統(tǒng)對功能、可靠性、成本、體積、功耗嚴(yán)格要求的專用計算機系統(tǒng)。

關(guān)鍵字: QT 嵌入式 C++

摘 要:倉儲作為物流與供應(yīng)鏈的核心環(huán)節(jié),對食品安全的控制起著至關(guān)重要的作用。溫濕度是影響糧食倉儲過程安全與品質(zhì)的重要因素。文中使用無線傳感網(wǎng)絡(luò)進行數(shù)據(jù)采集,通過Qt平臺設(shè)計軟件系統(tǒng),并借助數(shù)據(jù)庫進行數(shù)據(jù)存儲與分析處理,實...

關(guān)鍵字: 成品糧 倉儲 溫濕度監(jiān)測 無線傳感網(wǎng)絡(luò) QT

在此部件上繪制行號,并將其放置在CodeEditor的viewport()的左邊距區(qū)域上,QWidget類也可以幫助我們對其內(nèi)容進行滾動。

關(guān)鍵字: QT 代碼編輯器

不管是Qt新手還是開發(fā)過qt的群體來說,對Qt Designer、Qt Quick Designer、Qt Creator這幾個應(yīng)用程序肯定是熟悉的。

關(guān)鍵字: QT IDE C

在當(dāng)今社會,人們的生活水平普遍提高,工作強度越來越大,營養(yǎng)的過剩和運動量的減少,導(dǎo)致心腦血管疾病的發(fā)病率是越來越高。

關(guān)鍵字: Linux QT GPRS 遠(yuǎn)程集群式 心臟病人實時診斷系

qt值得學(xué)習(xí)嗎? 嵌入式要學(xué)的東西真的很多,我們可能會說不寫界面的話就不用學(xué)qt了?我不贊同。

關(guān)鍵字: 嵌入式 QT UI

什么是qt?簡單點說,Qt 就是一個跨平臺的 C++ 圖形用戶界面庫,可以同時支持桌面應(yīng)用程序開發(fā)、嵌入式開發(fā)和移動開發(fā),覆蓋了現(xiàn)有的所有主流平臺。

關(guān)鍵字: QT 程序 開發(fā)

qt值得學(xué)習(xí)嗎? 嵌入式要學(xué)的東西真的很多,我們可能會說不寫界面的話就不用學(xué)qt了?我不贊同,原因是……

關(guān)鍵字: 嵌入式 QT
關(guān)閉