QT 任意區域截圖
阿新 • • 發佈:2019-02-14
setWindowFlags(Qt::WindowStaysOnTopHint|Qt::ToolTip);
setWindowState(Qt::WindowActive|Qt::WindowFullScreen);//全屏無框架顯示且處於啟用狀態
resize(qApp->desktop()->rect().size());
QPolygon vt_pos; //定義多邊形
bgpixmap=QPixmap::grabWindow(QApplication::desktop()->winId());//抓取當前螢幕的圖片
paintEvent(QPaintEvent *event)
{
painter.begin(this);//進行重繪
painter.drawPixmap(0,0,loadpixmap);//將背景圖片畫到窗體上
painter.setPen(QPen(Qt::blue,2,Qt::SolidLine,Qt::FlatCap));//設定畫筆
painter.drawPolygon(vt_pos);
painter.end();
}
mouseMoveEvent(QMouseEvent *event)
{
vt_pos.push_back(event.pos);//將滑鼠經過的點入棧
}
mouseReleaseEvent(QMouseEvent *event)
{
/*手動繪圖*/
path.addPolygon(vt_pos);
path.setFillRule(Qt::WindingFill);//設定填充規則(和setClipPath()配合)
QRectFrf=path.controlPointRect();//獲取路徑所在矩形區域
QPixmapmap;
map=loadpixmap.copy(rf.topLeft().x(),rf.topLeft().y(),rf.width(),rf.height());
//座標轉換以(0-rf.topLeft().x(),0-rf.topLeft().y()為座標原點)
QPainterPathpa=path.translated(0-rf.topLeft().x(),0-rf.topLeft().y());
//建立QPixmap裝置,將需要的區域map繪到QPixmap上
temp_image=QPixmap(rf.width(),rf.height());
//temp_image.fill(Qt::transparent);
temp_image.fill(QColor(255,255,255));
temp_paint.begin(&temp_image);
temp_paint.setClipping(true);
temp_paint.setClipPath(pa);//設定裁剪路徑
temp_paint.drawPixmap(0,0,map);
temp_paint.setClipping(false);
temp_paint.end();
shotPixmap=temp_image;//.copy(0,0,rf.width(),rf.height());//更新選區的Pixmap
}