利用qt提供的函数实现截屏:
QPixmap QPixmap::grabWindow(WID window, int x = 0, int y = 0, int width = -1, int height = -1)
window: 表示窗口ID号
x、y: 截取屏幕的其实坐标
width:截取屏幕的宽度 -1表示当前窗口宽度
height:截取屏幕的高度 -1表示当前窗口高度
示例:QString fileName;QPixmap pixmap;QPixmap pix;//pix = pixmap.grabWindow(this->winId(), 0, 0, -1, -1); // 獲取當前窗口pix = pixmap.grabWindow(QApplication::desktop()->winId()); // 獲取整個屏幕窗口// 當前時間命名fileName = QDateTime::currentDateTime().toString("yyyy-mm-dd hh-mm-ss") + ".bmp";if (pix.isNull()){QMessageBox::information(this, "error", "grab Screen failed!", QMessageBox::Ok);}else{if (!pix.save("../screen/" + fileName, "BMP")){QMessageBox::information(this, "right", "save error!", QMessageBox::Ok);}else{QMessageBox::information(this, "Grab", "save successful!", QMessageBox::Ok);}}