Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2534884
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2011-05-18 17:27:57

    最近因为需要开发一个类似QQ的截屏功能,花了不少的时间,开始的时候很多也不懂,不知道如何下手,在网上找了很多的资料,现已经实现了截屏,移动截屏选区,通过拖动选区的八个控制点来改变选区的大小。
    QT实现截屏的原理是,当点击截屏按钮(菜单栏上的截屏按钮)时,调用fullScreenWidget对象getFullScreenPixmap()方法,返回当前屏幕的QPixmap图片对象。在构造调用窗体的构造函数中,关联信号与槽,使其在点击“截屏”按钮发送信息给fullScreenWidget对象对应的槽函数,以便加载当前的背景图片。
connect(newAct,SIGNAL(triggered()),this,SLOT(screenshot()));
connect(this,SIGNAL(setPixmap(QPixmap)),fullWidget,SLOT(loadBackgroundPixmap(QPixmap)));
    在fullScreenWidget类中,主要是根据当前截屏的状态来实时的判断如果截取屏幕。我在类中定义了shotState枚举类型,用来判断当前截屏的状态。定义了QPoint beginPoint,endPoint,moveBeginPoint,moveEndPoint;四个对象主要用来记录开始,结束的截屏坐标。移动的开始,结束坐标。根据不同的截屏状态和这4个坐标点的值来计算截屏的区域。我的表达能力不太好,先把核心代码上面来。fullScreenWidget.h头文件代码如下:
  1. #ifndef FULLSCREENWIDGET_H
  2. #define FULLSCREENWIDGET_H

  3. #include <QWidget>
  4. #include <QPixmap>
  5. #include <QPoint>
  6. #include <QtGui>
  7. #include <QPainter>
  8. #include <QBrush>

  9. class fullScreenWidget : public QWidget{

  10.   Q_OBJECT
  11.   
  12.   public:
  13.   fullScreenWidget(); //构造函数
  14.   enum shotState{initShot,beginShot,finishShot,endShot,beginMoveShot,finishMoveShot,beginControl,finishControl}; //进行截屏的状态
  15.   //移动选区中的8个控制点,按照顺时针方向从左上控制点到左中控制点分配编号为1~8
  16.   enum controlPointEnum{moveControl0,moveControl1,moveControl2,moveControl3,moveControl4,moveControl5,moveControl6,moveControl7,moveControl8};
  17.   QPixmap getFullScreenPixmap(); //获取全屏的Pixmap
  18.   
  19.   public slots:
  20.   void loadBackgroundPixmap(const QPixmap &bgPixmap);//加载背景Pixmap槽函数
  21.   void loadBackgroundPixmap(const QPixmap &bgPixmap, int x, int y, int width, int height); //加载背景pixmap槽函数,设置x,y,width,height
  22.   void cancelSelectedRect(); //取消选择区域
  23.   void savePixmap(); //保选取行为的方法
  24.   
  25.   signals:
  26.   void finishPixmap(const QPixmap &finishPixmap); //完成切图后的图片,发送信号给连接者

  27.   private:
  28.   //选区框的8个点选取
  29.   QRect tlRect; //左上点
  30.   QRect trRect; //右上点
  31.   QRect blRect; //左下点
  32.   QRect brRect; //右下点
  33.   QRect tcRect; //上中点
  34.   QRect bcRect; //下中点
  35.   QRect lcRect;//左中点
  36.   QRect rcRect; //右中点

  37.   QPainter painter;
  38.   QPoint beginPoint,endPoint,moveBeginPoint,moveEndPoint;
  39.   QRect selectedRect; //选择区域
  40.   QPixmap loadPixmap,shotPixmap;
  41.   shotState currentShotState; //当前的截屏状态
  42.   controlPointEnum controlValue; //记录移动控制点的值
  43.   QAction *savePixmapAction; //保存图片行为
  44.   QAction *cancelAction; //取消选取行为
  45.   QAction *quitAction; //退出选取行为
  46.   QMenu *contextMenu; //选中区域右键菜单
  47.   int screenwidth; //整个屏幕的宽度
  48.   int screenheight; //整个屏幕的高度
  49.   int screenx; //选区的X
  50.   int screeny; //选区的Y
  51.   int tipWidth,tipHeight,infoWidth,infoHeight; //加载初始框的宽度,高度;显示坐标信息的宽度,高度
  52.   
  53.   QRect getSelectedRect(); //获取选取
  54.   QRect getRect(const QPoint &beginPoint, const QPoint &endPoint); //根据两个点获取选取坐标
  55.   void initFullScreenWidget(); //初始化抓全屏的相关参数
  56.   bool isInSelectedRect(const QPoint &point); //判断该点是否在选中区域
  57.   void initSelectedMenu();//初始化右键菜单
  58.   void drawTipsText(); //在屏幕上打印提示信息
  59.   void drawSelectedPixmap(void); //在屏幕上画选取的屏幕
  60.   void updateBeginEndPointValue(const QRect &rect); //当移动选取后,对beginPoint,endPoint坐标进行重新修改
  61.   void checkMoveEndPoint(); //对移动的选区进行判断
  62.   void draw8ControlPoint(const QRect &rect);
  63.   void updateMouseShape(const QPoint &point); //更新鼠标的当前状态
  64.   void updateMoveControlMouseShape(controlPointEnum controlValue);
  65.   controlPointEnum getMoveControlState(const QPoint &point); //获取移动控制点状态
  66.   QRect getMoveAllSelectedRect(void); //获取移动整个选中的选区
  67.   QRect getMoveControlSelectedRect(void);//获取移动控制点的选区
  68.   int getMinValue(int num1, int num2);//获取两个数中的最小值
  69.   void drawXYWHInfo(void); //打印选取的x,y,h,w值信息

  70.   //重写基类方法
  71.   void keyPressEvent(QKeyEvent *event);
  72.   void paintEvent(QPaintEvent *event);
  73.   void mousePressEvent(QMouseEvent *event);
  74.   void mouseReleaseEvent(QMouseEvent *event);
  75.   void mouseMoveEvent(QMouseEvent *event);
  76.   void mouseDoubleClickEvent(QMouseEvent *event);
  77.   void contextMenuEvent(QContextMenuEvent *event);
  78. };

  79. #endif
fullScreenWidget.cpp类的实现文件如下:
  1. #include "fullScreenWidget.h"
  2. fullScreenWidget::fullScreenWidget()
  3. {
  4.   setWindowState(Qt::WindowActive|Qt::WindowFullScreen);
  5.   tipWidth = 300; //温馨提示框的宽度
  6.   tipHeight = 100; //温馨提示框的高度
  7.   infoWidth = 100; //坐标信息框的宽度
  8.   infoHeight = 50; //坐标信息框的高度
  9.   initFullScreenWidget();
  10. }

  11. void fullScreenWidget::initSelectedMenu()
  12. {
  13.   savePixmapAction = new QAction(tr("保存选区"),this);
  14.   cancelAction = new QAction(tr("重选"),this);
  15.   quitAction = new QAction(tr("退出"),this);
  16.   contextMenu = new QMenu(this);

  17.   connect(savePixmapAction,SIGNAL(triggered()),this,SLOT(savePixmap()));
  18.   connect(cancelAction,SIGNAL(triggered()),this,SLOT(cancelSelectedRect()));
  19.   connect(quitAction,SIGNAL(triggered()),this,SLOT(hide()));
  20. }

  21. void fullScreenWidget::savePixmap()
  22. {
  23.   QString fileName;
  24.   fileName = QFileDialog::getSaveFileName(this,tr("保存图片"),QDir::currentPath(),tr("Images (*.jpg *.png *.bmp)"));
  25.   if(fileName.isNull())
  26.     return;
  27.   
  28.   shotPixmap.save(fileName);
  29.   hide();
  30. }

  31. void fullScreenWidget::loadBackgroundPixmap(const QPixmap &bgPixmap)
  32. {
  33.   int width,height;
  34.   width = QApplication::desktop()->size().width();
  35.   height = QApplication::desktop()->size().height();

  36.   loadBackgroundPixmap(bgPixmap,0,0,width,height);
  37. }

  38. void fullScreenWidget::loadBackgroundPixmap(const QPixmap &bgPixmap, int x, int y, int width, int height)
  39. {
  40.   loadPixmap = bgPixmap;
  41.   screenx = x;
  42.   screeny = y;
  43.   screenwidth = width;
  44.   screenheight = height;
  45.   initFullScreenWidget();
  46. }

  47. QPixmap fullScreenWidget::getFullScreenPixmap()
  48. {
  49.   initFullScreenWidget();
  50.   QPixmap result = QPixmap();
  51.   result = QPixmap::grabWindow(QApplication::desktop()->winId()); //抓取当前屏幕的图片
  52.   
  53.   return result;
  54. }

  55. void fullScreenWidget::paintEvent(QPaintEvent *event)
  56. {
  57.   QColor shadowColor;
  58.   shadowColor= QColor(0,0,0,100); //阴影颜色设置
  59.   painter.begin(this); //进行重绘

  60.   painter.setPen(QPen(Qt::blue,2,Qt::SolidLine,Qt::FlatCap));//设置画笔
  61.   painter.drawPixmap(screenx,screeny,loadPixmap); //将背景图片画到窗体上
  62.   painter.fillRect(screenx,screeny,screenwidth,screenheight,shadowColor); //画影罩效果

  63.   switch(currentShotState){
  64.     case initShot:
  65.       drawTipsText();
  66.       break;
  67.     case beginShot:
  68.     case finishShot:
  69.       selectedRect = getRect(beginPoint,endPoint); //获取选区
  70.       drawSelectedPixmap();
  71.       break;
  72.     case beginMoveShot:
  73.     case finishMoveShot:
  74.       selectedRect = getMoveAllSelectedRect(); //获取选区
  75.       drawSelectedPixmap();
  76.       break;
  77.     case beginControl:
  78.     case finishControl:
  79.       selectedRect = getMoveControlSelectedRect();
  80.       drawSelectedPixmap();
  81.       break;
  82.     default:
  83.       break;
  84.   }
  85.   drawXYWHInfo(); //打印坐标信息
  86.   painter.end(); //重绘结束
  87.   
  88.   if(currentShotState == finishMoveShot || currentShotState == finishControl){
  89.     updateBeginEndPointValue(selectedRect); //当移动完选区后,更新beginPoint,endPoint;为下一次移动做准备工作
  90.   }

  91. }

  92. void fullScreenWidget::keyPressEvent(QKeyEvent *event)
  93. {
  94.   if(event->key() == Qt::Key_Escape){
  95.     initFullScreenWidget();
  96.     hide();
  97.   }
  98. }

  99. void fullScreenWidget::mousePressEvent(QMouseEvent *event)
  100. {
  101.   //当开始进行拖动进行选择区域时,确定开始选取的beginPoint坐标
  102.   if(event->button() == Qt::LeftButton && currentShotState == initShot){
  103.     currentShotState = beginShot; //设置当前状态为beginShot状态
  104.     beginPoint = event->pos();
  105.   }
  106.   
  107.   //移动选区改变选区的所在位置
  108.   if(event->button() == Qt::LeftButton && isInSelectedRect(event->pos()) &&
  109.      getMoveControlState(event->pos()) == moveControl0){
  110.       currentShotState = beginMoveShot; //启用开始移动选取选项,beginMoveShot状态
  111.       moveBeginPoint = event->pos();
  112.   }
  113.   //移动控制点改变选区大小
  114.   if(event->button() == Qt::LeftButton && getMoveControlState(event->pos()) != moveControl0){
  115.     currentShotState = beginControl; //开始移动控制点
  116.     controlValue = getMoveControlState(event->pos());
  117.     moveBeginPoint = event->pos();
  118.   }
  119. }

  120. void fullScreenWidget::mouseReleaseEvent(QMouseEvent *event)
  121. {
  122.   if(event->button() == Qt::LeftButton && currentShotState == beginShot){
  123.     currentShotState = finishShot;
  124.     endPoint = event->pos();
  125.     update();
  126.   }

  127.   if(event->button() == Qt::LeftButton && currentShotState == beginMoveShot){
  128.     currentShotState = finishMoveShot;
  129.     moveEndPoint = event->pos();
  130.     update();
  131.   }

  132.   //当前状态为beginControl状态时,设置状态为finishControl
  133.   if(event->button() == Qt::LeftButton && currentShotState == beginControl){
  134.     currentShotState = finishControl;
  135.     moveEndPoint = event->pos();
  136.     update();
  137.   }
  138. }

  139. void fullScreenWidget::mouseMoveEvent(QMouseEvent *event)
  140. {
  141.   //当拖动时,动态的更新所选择的区域
  142.   if(currentShotState == beginShot){
  143.     endPoint = event->pos();
  144.     update();
  145.   }

  146.   //当确定选区后,对选区进行移动操作
  147.   if(currentShotState == beginMoveShot || currentShotState == beginControl){
  148.     moveEndPoint = event->pos();
  149.     update();
  150.   }

  151.   updateMouseShape(event->pos()); //修改鼠标的形状
  152.   setMouseTracking(true);
  153. }

  154. void fullScreenWidget::mouseDoubleClickEvent(QMouseEvent *event)
  155. {
  156.   if(currentShotState == finishShot || currentShotState == finishMoveShot || currentShotState == finishControl){
  157.     emit finishPixmap(shotPixmap); //当完成时发送finishPixmap信号
  158.     hide();
  159.   }
  160. }

  161. QRect fullScreenWidget::getRect(const QPoint &beginPoint, const QPoint &endPoint)
  162. {
  163.   int x,y,width,height;
  164.   width = qAbs(beginPoint.x() - endPoint.x());
  165.   height = qAbs(beginPoint.y() - endPoint.y());
  166.   x = beginPoint.x() < endPoint.x() ? beginPoint.x() : endPoint.x();
  167.   y = beginPoint.y() < endPoint.y() ? beginPoint.y() : endPoint.y();
  168.   
  169.   return QRect(x,y,width,height);
  170. }

  171. void fullScreenWidget::initFullScreenWidget()
  172. {
  173.     currentShotState = initShot;
  174.     controlValue = moveControl0;
  175.     beginPoint =QPoint(0,0);
  176.     endPoint = QPoint(0,0);
  177.     moveBeginPoint = QPoint(0,0);
  178.     moveEndPoint = QPoint(0,0);

  179.     tlRect = QRect(0,0,0,0); //左上点
  180.     trRect = QRect(0,0,0,0); //上右点
  181.     blRect = QRect(0,0,0,0); //左下点
  182.     brRect = QRect(0,0,0,0); //右下点
  183.     tcRect = QRect(0,0,0,0); //上中点
  184.     bcRect = QRect(0,0,0,0); //下中点
  185.     lcRect = QRect(0,0,0,0); //左中点
  186.     rcRect = QRect(0,0,0,0); //右中点

  187.     setCursor(Qt::CrossCursor);
  188. }

  189. bool fullScreenWidget::isInSelectedRect(const QPoint &point)
  190. {
  191.   int x,y;
  192.   QRect selectedRect;
  193.   if(currentShotState == initShot || currentShotState == beginShot)
  194.     return false;

  195.   selectedRect = getSelectedRect();
  196.   x = point.x();
  197.   y = point.y();
  198.   
  199.   return selectedRect.contains(x,y);
  200. }

  201. void fullScreenWidget::cancelSelectedRect()
  202. {
  203.   initFullScreenWidget();
  204.   update(); //进行重绘,将选取区域去掉
  205. }

  206. void fullScreenWidget::contextMenuEvent(QContextMenuEvent *event)
  207. {
  208.   initSelectedMenu();

  209.   if(isInSelectedRect(event->pos())){
  210.     contextMenu->addAction(savePixmapAction);
  211.   }
  212.   else{
  213.     contextMenu->addAction(cancelAction);
  214.     contextMenu->addAction(quitAction);
  215.   }

  216.   contextMenu->exec(event->pos());
  217. }

  218. void fullScreenWidget::drawTipsText()
  219. {
  220.   int x = (screenwidth - tipWidth)/2;
  221.   int y = (screenheight - tipHeight)/2;
  222.   QColor color = QColor(100,100,100,200);
  223.   QRect rect = QRect(x,y,tipWidth,tipHeight);
  224.   QString strTipsText = QString(tr("温馨提示\n鼠标拖动进行截屏;截屏区域内右键保存;\n截屏区域外右键取消;ESC退出;"));

  225.   painter.fillRect(rect,color);
  226.   painter.setPen(QPen(Qt::white));//设置画笔的颜色为白色
  227.   painter.drawText(rect,Qt::AlignCenter,strTipsText);

  228. }

  229. QRect fullScreenWidget::getSelectedRect()
  230. {
  231.   if(currentShotState == beginMoveShot){
  232.      return getMoveAllSelectedRect();
  233.   }
  234.   else if(currentShotState == beginControl){
  235.      return getMoveControlSelectedRect();
  236.   }
  237.   else{
  238.     return getRect(beginPoint,endPoint);
  239.   }
  240. }

  241. void fullScreenWidget::updateBeginEndPointValue(const QRect &rect)
  242. {
  243.   beginPoint = rect.topLeft();
  244.   endPoint = rect.bottomRight();
  245.  
  246.   moveBeginPoint = QPoint(0,0);
  247.   moveEndPoint = QPoint(0,0);
  248. }

  249. void fullScreenWidget::checkMoveEndPoint()
  250. {
  251.   int x,y;
  252.   QRect selectedRect = getRect(beginPoint, endPoint);
  253.   QPoint bottomRightPoint = selectedRect.bottomRight();
  254.   x = moveEndPoint.x() - moveBeginPoint.x();
  255.   y = moveEndPoint.y() - moveBeginPoint.y();

  256.   if(x + selectedRect.x() < 0){ //当移动后X坐标小于零时,则出现选区丢失,则计算出moveEndPoint的X最大坐标值,进行赋值
  257.     moveEndPoint.setX(qAbs(selectedRect.x()-moveBeginPoint.x()));
  258.   }

  259.   if(y + selectedRect.y() < 0){ //当移动后Y坐标小于零时,则出现选区丢失,则计算出moveEndPoint的Y最大坐标值,进行赋值
  260.     moveEndPoint.setY(qAbs(selectedRect.y() - moveBeginPoint.y()));
  261.   }

  262.   if(x + bottomRightPoint.x() > screenwidth){ //当移动选区后,出现超出整个屏幕的右面时,设置moveEndPoint的X的最大坐标
  263.     moveEndPoint.setX(screenwidth - bottomRightPoint.x() + moveBeginPoint.x());
  264.   }

  265.   if(y + bottomRightPoint.y() > screenheight){ //当移动选区后,出现超出整个屏幕的下面时,设置moveEndPoint的Y的最大坐标值
  266.     moveEndPoint.setY(screenheight - bottomRightPoint.y() + moveBeginPoint.y());
  267.   }
  268. }

  269. void fullScreenWidget::draw8ControlPoint(const QRect &rect)
  270. {
  271.   int x,y;
  272.   QColor color= QColor(0,0,255); //画点的颜色设置
  273.   QPoint tlPoint = rect.topLeft(); //左上点
  274.   QPoint trPoint = rect.topRight(); //右上点
  275.   QPoint blPoint = rect.bottomLeft(); //左下点
  276.   QPoint brPoint = rect.bottomRight(); //右下点
  277.   
  278.   x = (tlPoint.x()+trPoint.x())/2;
  279.   y = tlPoint.y();
  280.   QPoint tcPoint = QPoint(x,y);

  281.   x = (blPoint.x()+brPoint.x())/2;
  282.   y = blPoint.y();
  283.   QPoint bcPoint = QPoint(x,y);

  284.   x = tlPoint.x();
  285.   y = (tlPoint.y()+blPoint.y())/2;
  286.   QPoint lcPoint = QPoint(x,y);

  287.   x = trPoint.x();
  288.   y = (trPoint.y()+brPoint.y())/2;
  289.   QPoint rcPoint = QPoint(x,y);

  290.   tlRect = QRect(tlPoint.x()-2,tlPoint.y()-2,6,6); //左上点
  291.   trRect = QRect(trPoint.x()-2,trPoint.y()-2,6,6); //右上点
  292.   blRect = QRect(blPoint.x()-2,blPoint.y()-2,6,6); //左下点
  293.   brRect = QRect(brPoint.x()-2,brPoint.y()-2,6,6); //右下点
  294.   tcRect = QRect(tcPoint.x()-2,tcPoint.y()-2,6,6); //上中点
  295.   bcRect = QRect(bcPoint.x()-2,bcPoint.y()-2,6,6); //下中点
  296.   lcRect = QRect(lcPoint.x()-2,lcPoint.y()-2,6,6);//左中点
  297.   rcRect = QRect(rcPoint.x()-2,rcPoint.y()-2,6,6); //右中点

  298.   painter.fillRect(tlRect,color);
  299.   painter.fillRect(trRect,color);
  300.   painter.fillRect(blRect,color);
  301.   painter.fillRect(brRect,color);
  302.   painter.fillRect(tcRect,color);
  303.   painter.fillRect(bcRect,color);
  304.   painter.fillRect(lcRect,color);
  305.   painter.fillRect(rcRect,color);
  306. }

  307. void fullScreenWidget::updateMouseShape(const QPoint &point)
  308. {
  309.   switch(currentShotState){
  310.     case initShot:
  311.     case beginShot:
  312.       setCursor(Qt::CrossCursor);
  313.       break;
  314.     case beginMoveShot:
  315.       setCursor(Qt::OpenHandCursor);
  316.       break;
  317.     case finishShot:
  318.     case finishMoveShot:
  319.     case finishControl:
  320.       if(getSelectedRect().contains(point))
  321.         setCursor(Qt::OpenHandCursor);
  322.       else
  323.         updateMoveControlMouseShape(getMoveControlState(point));
  324.       break;
  325.     case beginControl:
  326.       updateMoveControlMouseShape(controlValue); //调用函数对移动8个控制点进行鼠标状态的改变
  327.       break;
  328.     default:
  329.       setCursor(Qt::ArrowCursor);
  330.       break;
  331.   }
  332. }

  333. void fullScreenWidget::updateMoveControlMouseShape(controlPointEnum controlValue){
  334.   switch(controlValue){
  335.     case moveControl1:
  336.     case moveControl5:
  337.       setCursor(Qt::SizeFDiagCursor);
  338.       break;
  339.     case moveControl2:
  340.     case moveControl6:
  341.       setCursor(Qt::SizeVerCursor);
  342.       break;
  343.     case moveControl3:
  344.     case moveControl7:
  345.       setCursor(Qt::SizeBDiagCursor);
  346.       break;
  347.     case moveControl4:
  348.     case moveControl8:
  349.       setCursor(Qt::SizeHorCursor);
  350.       break;
  351.     default:
  352.       setCursor(Qt::ArrowCursor);
  353.       break;
  354.   }
  355. }

  356. fullScreenWidget::controlPointEnum fullScreenWidget::getMoveControlState(const QPoint &point)
  357. {
  358.   fullScreenWidget::controlPointEnum result = moveControl0;
  359.   if(currentShotState == initShot || currentShotState == beginShot) {
  360.     result = moveControl0;
  361.   }
  362.   else if(tlRect.contains(point)){
  363.     result = moveControl1;
  364.   }
  365.   else if(tcRect.contains(point)){
  366.     result = moveControl2;
  367.   }
  368.   else if(trRect.contains(point)){
  369.     result = moveControl3;
  370.   }
  371.   else if(rcRect.contains(point)){
  372.     result = moveControl4;
  373.   }
  374.   else if(brRect.contains(point)){
  375.     result = moveControl5;
  376.   }
  377.   else if(bcRect.contains(point)){
  378.     result = moveControl6;
  379.   }
  380.   else if(blRect.contains(point)){
  381.     result = moveControl7;
  382.   }
  383.   else if(lcRect.contains(point)){
  384.     result = moveControl8;
  385.   }
  386.   else{
  387.     result = moveControl0;
  388.   }

  389.   return result;
  390. }

  391. QRect fullScreenWidget::getMoveAllSelectedRect(void)
  392. {
  393.   QRect result;
  394.   QPoint tmpBeginPoint,tmpEndPoint;
  395.   int moveX,moveY;
  396.   checkMoveEndPoint(); //对移动选区进行判断,当移动的选区超出边界,则停止移动
  397.   moveX = moveEndPoint.x() - moveBeginPoint.x();
  398.   moveY = moveEndPoint.y() - moveBeginPoint.y();
  399.   tmpBeginPoint.setX(beginPoint.x() + moveX);
  400.   tmpBeginPoint.setY(beginPoint.y() + moveY);
  401.   tmpEndPoint.setX(endPoint.x() + moveX);
  402.   tmpEndPoint.setY(endPoint.y() + moveY);

  403.   result = getRect(tmpBeginPoint, tmpEndPoint);
  404.   return result;
  405. }

  406. QRect fullScreenWidget::getMoveControlSelectedRect(void)
  407. {
  408.   int x,y,w,h;
  409.   QRect rect = getRect(beginPoint,endPoint);
  410.   QRect result;
  411.   switch(controlValue){
  412.     case moveControl1:
  413.       result = getRect(rect.bottomRight(),moveEndPoint);
  414.       return result;
  415.       break;
  416.     case moveControl2:
  417.       x = rect.x();
  418.       y = getMinValue(moveEndPoint.y(),rect.bottomLeft().y());
  419.       w = rect.width();
  420.       h = qAbs(moveEndPoint.y() - rect.bottomRight().y());
  421.       break;
  422.     case moveControl3:
  423.       result = getRect(rect.bottomLeft(),moveEndPoint);
  424.       return result;
  425.       break;
  426.     case moveControl4:
  427.       x = getMinValue(rect.x(),moveEndPoint.x());
  428.       y = rect.y();
  429.       w = qAbs(rect.bottomLeft().x() - moveEndPoint.x());
  430.       h = rect.height();
  431.       break;
  432.     case moveControl5:
  433.       result = getRect(rect.topLeft(),moveEndPoint);
  434.       return result;
  435.       break;
  436.     case moveControl6:
  437.       x = rect.x();
  438.       y = getMinValue(rect.y(),moveEndPoint.y());
  439.       w = rect.width();
  440.       h = qAbs(moveEndPoint.y() - rect.topLeft().y());
  441.       break;
  442.     case moveControl7:
  443.       result = getRect(moveEndPoint,rect.topRight());
  444.       return result;
  445.       break;
  446.     case moveControl8:
  447.       x = getMinValue(moveEndPoint.x(),rect.bottomRight().x());
  448.       y = rect.y();
  449.       w = qAbs(rect.bottomRight().x() - moveEndPoint.x());
  450.       h = rect.height();
  451.       break;
  452.     default:
  453.       result = getRect(beginPoint,endPoint);
  454.       return result;
  455.       break;
  456.   }

  457.   return QRect(x,y,w,h); //获取选区
  458. }

  459. int fullScreenWidget::getMinValue(int num1, int num2)
  460. {
  461.   return num1<num2?num1:num2;
  462. }

  463. void fullScreenWidget::drawSelectedPixmap(void)
  464. {
  465.   painter.drawRect(selectedRect); //画选中的矩形框
  466.   shotPixmap = loadPixmap.copy(selectedRect); //更新选区的Pixmap
  467.   if(selectedRect.width() > 0 && selectedRect.height()){
  468.     painter.drawPixmap(selectedRect.topLeft(),shotPixmap); //画选中区域的图片
  469.   }
  470.   draw8ControlPoint(selectedRect); //画出选区的8个控制点
  471. }

  472. void fullScreenWidget::drawXYWHInfo(void)
  473. {
  474.   int x,y;
  475.   QColor color = QColor(239,234,228,200);
  476.   QRect rect;
  477.   QString strTipsText;
  478.   switch(currentShotState){
  479.     case beginShot:
  480.     case finishShot:
  481.     case beginMoveShot:
  482.     case finishMoveShot:
  483.     case beginControl:
  484.     case finishControl:
  485.       x = selectedRect.x() + 5;
  486.       y = selectedRect.y() > infoHeight ? selectedRect.y()-infoHeight:selectedRect.y();
  487.       rect = QRect(x,y,infoWidth,infoHeight);
  488.       strTipsText = QString(tr(" 坐标信息\n x:%1 y:%2\n w:%3 h:%4")).arg(selectedRect.x(),4).arg(selectedRect.y(),4)
  489.                       .arg(selectedRect.width(),4).arg(selectedRect.height(),4);
  490.     painter.fillRect(rect,color);
  491.     painter.setPen(QPen(Qt::black));//设置画笔的颜色为黑色
  492.     painter.drawText(rect,Qt::AlignLeft|Qt::AlignVCenter,strTipsText);
  493.     break;
  494.     default:
  495.     break;
  496.   }
  497. }
    您可以下载下来,进行测试使用。解压文件后,使用qtcreator工具进行加载screenshot.pro文件。执行qmake,build,run。即可。
    注:因我提供的源代码在ubuntu操作系统上编写,如果您使用的是windows操作系统,可能需要qtcreator修改字符集为uft-8即可。本代码使用的是qt4.6.2进行编译,使用emacs编辑器进行编写。
阅读(24652) | 评论(1) | 转发(4) |
2

上一篇:渔夫捕鱼问题

下一篇:计算组合数

给主人留下些什么吧!~~

mzero0072014-03-12 10:39:09

运行了一下觉得很好哎。。。谢谢楼主分享,我会好好研究的、、、