├── .gitignore ├── .gitmodules ├── KeyString.cpp ├── KeyString.h ├── LICENSE ├── README.md ├── ScreenShotTool.pro ├── canvas.cpp ├── canvas.h ├── doc ├── 1.gif ├── 1.jpg ├── 2.gif ├── 2.jpg ├── 3.gif ├── 3.jpg └── 4.gif ├── en.ts ├── hotkeybar.cpp ├── hotkeybar.h ├── linepaint.cpp ├── linepaint.h ├── main.cpp ├── operateSet.cpp ├── operateSet.h ├── pic ├── 1.ico ├── 2.ico ├── 3.ico ├── 4.ico └── 5.ico ├── rectpaint.cpp ├── rectpaint.h ├── screenshottool.cpp ├── screenshottool.h ├── screenshottool.qrc ├── screenshottool.ui ├── setting.ini ├── ss.ico ├── textpaint.cpp ├── textpaint.h └── zh_cn.ts /.gitignore: -------------------------------------------------------------------------------- 1 | .directory 2 | *.pro.user 3 | *.qm 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "qxtglobalshortcut5"] 2 | path = qxtglobalshortcut5 3 | url = https://github.com/ddqd/qxtglobalshortcut5 4 | -------------------------------------------------------------------------------- /KeyString.cpp: -------------------------------------------------------------------------------- 1 | #include "KeyString.h" 2 | 3 | /* 4 | * Author:qiuzhiqian 5 | * Email:xia_mengliang@163.com 6 | * Github:https://github.com/qiuzhiqian 7 | * Date:2017.07.23 8 | * Description:这个类主要用来转换快捷按键的键值和字符串 9 | **/ 10 | 11 | KeyString::KeyString() 12 | { 13 | keymap[Qt::Key_Escape]="Esc"; 14 | keymap[Qt::Key_Tab]="Tab"; 15 | keymap[Qt::Key_Backtab]="Backtab"; 16 | keymap[Qt::Key_Backspace]="Backspace"; 17 | keymap[Qt::Key_Return]="Return"; 18 | keymap[Qt::Key_Insert]="Insert"; 19 | keymap[Qt::Key_Delete]="Delete"; 20 | keymap[Qt::Key_Pause]="Pause"; 21 | keymap[Qt::Key_Home]="Home"; 22 | keymap[Qt::Key_End]="End"; 23 | keymap[Qt::Key_Left]="Left"; 24 | keymap[Qt::Key_Up]="Up"; 25 | keymap[Qt::Key_Right]="Right"; 26 | keymap[Qt::Key_Down]="Down"; 27 | keymap[Qt::Key_PageUp]="PageUp"; 28 | keymap[Qt::Key_PageDown]="PageDown"; 29 | keymap[Qt::Key_F1]="F1"; 30 | keymap[Qt::Key_F2]="F2"; 31 | keymap[Qt::Key_F3]="F3"; 32 | keymap[Qt::Key_F4]="F4"; 33 | keymap[Qt::Key_F5]="F5"; 34 | keymap[Qt::Key_F6]="F6"; 35 | keymap[Qt::Key_F7]="F7"; 36 | keymap[Qt::Key_F8]="F8"; 37 | keymap[Qt::Key_F9]="F9"; 38 | keymap[Qt::Key_F10]="F10"; 39 | keymap[Qt::Key_F11]="F11"; 40 | keymap[Qt::Key_F12]="F12"; 41 | keymap[Qt::Key_Space]="Space"; 42 | keymap[Qt::Key_0]="0"; 43 | keymap[Qt::Key_1]="1"; 44 | keymap[Qt::Key_2]="2"; 45 | keymap[Qt::Key_3]="3"; 46 | keymap[Qt::Key_4]="4"; 47 | keymap[Qt::Key_5]="5"; 48 | keymap[Qt::Key_6]="6"; 49 | keymap[Qt::Key_7]="7"; 50 | keymap[Qt::Key_8]="8"; 51 | keymap[Qt::Key_9]="9"; 52 | keymap[Qt::Key_A]="A"; 53 | keymap[Qt::Key_B]="B"; 54 | keymap[Qt::Key_C]="C"; 55 | keymap[Qt::Key_D]="D"; 56 | keymap[Qt::Key_E]="E"; 57 | keymap[Qt::Key_F]="F"; 58 | keymap[Qt::Key_G]="G"; 59 | keymap[Qt::Key_H]="F"; 60 | keymap[Qt::Key_I]="I"; 61 | keymap[Qt::Key_J]="J"; 62 | keymap[Qt::Key_K]="K"; 63 | keymap[Qt::Key_L]="L"; 64 | keymap[Qt::Key_M]="M"; 65 | keymap[Qt::Key_N]="N"; 66 | keymap[Qt::Key_O]="O"; 67 | keymap[Qt::Key_P]="P"; 68 | keymap[Qt::Key_Q]="Q"; 69 | keymap[Qt::Key_R]="R"; 70 | keymap[Qt::Key_S]="S"; 71 | keymap[Qt::Key_T]="T"; 72 | keymap[Qt::Key_U]="U"; 73 | keymap[Qt::Key_V]="V"; 74 | keymap[Qt::Key_W]="W"; 75 | keymap[Qt::Key_X]="X"; 76 | keymap[Qt::Key_Y]="Y"; 77 | keymap[Qt::Key_Z]="Z"; 78 | } 79 | 80 | QString KeyString::Key2String(Qt::Key t_key, Qt::KeyboardModifiers t_mod) 81 | { 82 | QString showtext=""; 83 | QString modstr=""; 84 | QString keystr=""; 85 | switch(t_mod) 86 | { 87 | case Qt::NoModifier: 88 | modstr=""; 89 | break; 90 | case Qt::ShiftModifier: 91 | modstr="Shift"; 92 | break; 93 | case Qt::ControlModifier: 94 | modstr="Ctrl"; 95 | break; 96 | case Qt::AltModifier: 97 | modstr="Atl"; 98 | break; 99 | default: 100 | modstr=""; 101 | break; 102 | } 103 | 104 | keystr=keymap[t_key]; 105 | 106 | if(modstr!="") 107 | { 108 | showtext=modstr+"+"+keystr; 109 | } 110 | else 111 | { 112 | showtext=keystr; 113 | } 114 | 115 | return showtext; 116 | } 117 | 118 | void KeyString::String2Key(QString str,Qt::Key &t_key, Qt::KeyboardModifiers &t_mod) 119 | { 120 | QString modstr=""; 121 | QString keystr=""; 122 | 123 | QStringList keyList=str.split('+'); 124 | 125 | if(keyList.size()==2) 126 | { 127 | modstr=keyList[0]; 128 | keystr=keyList[1]; 129 | } 130 | else 131 | { 132 | keystr=keyList[0]; 133 | } 134 | 135 | if(modstr=="") 136 | { 137 | t_mod=Qt::NoModifier; 138 | } 139 | else if(modstr=="Shift") 140 | { 141 | t_mod=Qt::ShiftModifier; 142 | } 143 | else if(modstr=="Ctrl") 144 | { 145 | t_mod=Qt::ControlModifier; 146 | } 147 | else if(modstr=="Atl") 148 | { 149 | t_mod=Qt::AltModifier; 150 | } 151 | 152 | t_key=Qt::Key(keymap.keys(keystr)[0]); 153 | } 154 | -------------------------------------------------------------------------------- /KeyString.h: -------------------------------------------------------------------------------- 1 | #ifndef KEYSTRING_H 2 | #define KEYSTRING_H 3 | 4 | #include 5 | #include 6 | 7 | /* 8 | * Author:qiuzhiqian 9 | * Email:xia_mengliang@163.com 10 | * Github:https://github.com/qiuzhiqian 11 | * Date:2017.07.23 12 | **/ 13 | 14 | class KeyString{ 15 | public: 16 | KeyString(); 17 | QString Key2String(Qt::Key t_key, Qt::KeyboardModifiers t_mod); 18 | void String2Key(QString str,Qt::Key &t_key, Qt::KeyboardModifiers &t_mod); 19 | 20 | private: 21 | QMap keymap; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 qiuzhiqian 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 概述 2 | 这是一个使用QT设计的截图工具 3 | 4 | 目前效果图 5 | ![效果图1](https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/master/doc/1.gif) 6 | 7 | ![效果图2](https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/master/doc/2.gif) 8 | 9 | ![效果图3](https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/master/doc/3.gif) 10 | 11 | ![效果图4](https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/master/doc/4.gif) 12 | 13 | ![效果图5](https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/master/doc/3.jpg) 14 | 15 | # 历程 16 | ## 意动 17 | 现在网上免费的截图工具很多,最近用了一款很不错的,叫Snipaste。 18 | 这个软件就是基于QT开发的,不过并没有开源,软件设计的很好用,界面也很清新,于是我也想自己尝试这设计一个这样的工具来练练手。 19 | 于是就有了这样的一个工程 20 | 21 | ## 参考 22 | 虽然有了想法,但是还是完全不知道怎么办啊。不用担心,QT有大把的官方例程。 23 | 我从官方找到了一个叫做sceenshot的例程,这是一个简单的全屏截图的例子,从这个例子中,我们知道了两个重要的东西 24 | 1. **获取屏幕分辨率** 25 | 26 | ``` 27 | const QRect screenGeometry = QApplication::desktop()->screenGeometry(this); 28 | screen_width=screenGeometry.width(); 29 | screen_height=screenGeometry.height(); 30 | ``` 31 | 2. **截图API** 32 | 33 | ``` 34 | QScreen *screen = QGuiApplication::primaryScreen(); 35 | originalPixmap = screen->grabWindow(0,x,y,width,height); 36 | ``` 37 | 38 | 其实,在有了以上两个知识后,我们就已经可以截取屏幕上的任意区域的图片了。 39 | grabWindow这个函数我也最关心的就是四个参数x,y,width,height。这四个参数就是一个矩形框参数,只要给定了这四个参数,想截哪里截哪里。 40 | 比如我想截取屏幕上的从点(30,50)-->(240,350)的区域截图 41 | 42 | ``` 43 | QScreen *screen = QGuiApplication::primaryScreen(); 44 | originalPixmap = screen->grabWindow(0,30,50,(240-30),(350-50)); 45 | ``` 46 | 47 | ## 思考 48 | 那么用户在使用过程中,如何通过交互将这四个参数传递给程序呢?方法还是很多的,比如: 49 | 1. 我设计四个输入框,用户在截屏前先手动输入这四个参数对应的值,然后点击按键截屏。嗯...想想都感觉好傻。 50 | 2. 用鼠标选定一个区域,然后将矩形区域转化为这四个参数值,然后截屏。嗯大家都是这么干的,既然如此,我也这么来吧。 51 | 52 | 那么,我们的方案基本定下来了,即用户通过点击左键并拖动鼠标来选择需要截屏的区域,然后程序将坐标传递给grabWindow,从而完成截图。 53 | 54 | 1. 由于我们需要通过点击鼠标、拖动鼠标这些动作完成一些特定的功能,所以我们需要重载各种鼠标事件。 55 | 2. 为了让用户知道自己拖动的范围在什么地方,所以在拖动时要绘制出矩形的图形,即我们需要绘制矩形。 56 | 57 | 所以我们的步骤是: 58 | 1. 用户触发截图动作 59 | 2. 用户开始截图,按下鼠标左键然后拖动来选定区域,并实时将选在的区域显示在屏幕上 60 | 3. 完成截图,图片保存到本地或剪切板 61 | 62 | ## 深入 63 | 现在有了上面的步骤,我们就按照这个步骤来实现,我们可以看到,主要的任务都在第二步。 64 | 那么要实现第二步,我们又会想到一些问题: 65 | 66 | ``` 67 | 问:我们要绘制矩形框,这个矩形框绘制在什么上面? 68 | 思考:由QT的特性可以绘制在QWidget或者继承自QWidget的控件上 69 | 答:QWidget及QWidget属性控件 70 | ``` 71 | 72 | ``` 73 | 问:这个控件有什么特性? 74 | 思考:我们来仔细思考一下截图时拖动选框的过程,首先我们能看到整个屏幕当前的画面,然后鼠标可以在整个屏幕范围移动,绘制区域可以在整个屏幕内选择。 75 | 当然还有一点,我们鼠标的动作都是在这个控件上完成的,那么这个控件必须在所有窗体的顶层,即置顶。 76 | 答:全屏,显示屏幕画面,置顶 77 | ``` 78 | 79 | ``` 80 | 问:全屏和置顶好实现,显示屏幕画面怎么弄? 81 | 思考:由于我们的绘图控件是全屏的,并且置顶的,那么其实屏幕画面是在这个绘图控件的下面,即要截屏的画面被绘图控件(以下称为画布)挡住了。 82 | 所以要实现画布显示屏幕画面有两种方法。 83 | 第一种就是让画布透明,画布是透明的,自然就能够显示到下面的屏幕画面了。 84 | 第二种方法就是在屏幕画面被画布挡住之前将屏幕截下来,然后将这个截取的图片显示到幕布上,此处我们用第二种方法来实现。 85 | 答:在画布显示之前先截取屏幕图片,然后显示画布,然后之前截取的屏幕画面在画布上显示。 86 | ``` 87 | 88 | 所以。通过以上的问答,基本就确定了我们的设计思路。 89 | 90 | ## 最终思路 91 | 92 | 1、触发截图(快捷键或单击图标) 93 | 2、截取全屏画面 94 | 3、显示画布,画布全屏,画布置顶,画布显示之前截取的全屏画面 95 | 4、通过重写鼠标和绘图函数来达到显示鼠标拖动区间, 96 | 5、截取鼠标拖动区间图案,弹出保存选项 97 | 6、手动调整截图区域 98 | 7、添加线、矩形以及椭圆标记功能,设置标记画笔 99 | 8、保存截屏到文件或保存截屏到剪切板或放弃截屏 100 | 9、退出 101 | 102 | ## 待完善功能 103 | 1、标记大小位置调整 104 | 105 | # 多语言 106 | 本源码提供了两种内置语言的翻译文件:英文和中文。 107 | - 英文翻译文件:en.ts 108 | - 中文翻译文件:zh_cn.ts 109 | 110 | ## 使用方法 111 | ### 1、生成ts对应的qm文件 112 | 方法一:使用打开源代码,选择"工具"->"外部"->"Qt语言专家"->"发布翻译",然后得到对应的qm文件。 113 | 方法二:直接使用qtcreator对应的翻译家Linguist生成对应的qm文件,具体使用方法自行了解。 114 | ### 2、将qm文件放置于运行目录下 115 | 使用qtcreator时编译调试时类似于build-ScreenShotTool-Desktop_Qt_5_9_0_MinGW_32bit-Debug目录下 116 | 发布时请至于ScreenShotTool执行程序的同级目录下 117 | 118 | # 使用 119 | 使用很简单,无需过多介绍,展示一些示意图 120 | 英文界面 121 | 122 | ![英文界面](https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/master/doc/1.jpg) 123 | 124 | 中文界面 125 | 126 | ![中文界面](https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/master/doc/2.jpg) 127 | 128 | ## 提醒 129 | 130 | 如果发现软件的中文无法起作用,可以按以下步骤处理: 131 | 1、请确保你的软件安装目录下面有en.qm何zh_cn.qm这两个文件 132 | 2、如果有这两个文件,但仍然不起作用,请确保你的qm文件和本软件的版本是兼容的 133 | 3、如果以上的方法都没有用,请下载本软件源码,自行编译二进制文件,同时更新qm文件 134 | 4、如果以上还无法解决,请联系我 -------------------------------------------------------------------------------- /ScreenShotTool.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-07-18T14:06:17 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = ScreenShotTool 12 | TEMPLATE = app 13 | 14 | include(qxtglobalshortcut5/qxt.pri) 15 | 16 | # The following define makes your compiler emit warnings if you use 17 | # any feature of Qt which as been marked as deprecated (the exact warnings 18 | # depend on your compiler). Please consult the documentation of the 19 | # deprecated API in order to know how to port your code away from it. 20 | DEFINES += QT_DEPRECATED_WARNINGS 21 | 22 | # You can also make your code fail to compile if you use deprecated APIs. 23 | # In order to do so, uncomment the following line. 24 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 25 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 26 | 27 | 28 | SOURCES += \ 29 | main.cpp \ 30 | screenshottool.cpp \ 31 | canvas.cpp \ 32 | KeyString.cpp \ 33 | hotkeybar.cpp \ 34 | rectpaint.cpp \ 35 | linepaint.cpp \ 36 | textpaint.cpp 37 | 38 | HEADERS += \ 39 | screenshottool.h \ 40 | canvas.h \ 41 | KeyString.h \ 42 | hotkeybar.h \ 43 | operateSet.h \ 44 | rectpaint.h \ 45 | linepaint.h \ 46 | textpaint.h 47 | 48 | FORMS += \ 49 | screenshottool.ui 50 | 51 | RESOURCES += \ 52 | screenshottool.qrc 53 | 54 | RC_ICONS = ss.ico 55 | 56 | TRANSLATIONS += zh_cn.ts \ 57 | en.ts 58 | -------------------------------------------------------------------------------- /canvas.cpp: -------------------------------------------------------------------------------- 1 | #include "canvas.h" 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | 9 | #include "operateSet.h" 10 | #include 11 | 12 | /* 13 | * Author:qiuzhiqian 14 | * Email:xia_mengliang@163.com 15 | * Github:https://github.com/qiuzhiqian 16 | * Date:2017.07.23 17 | * Description:这个类主要用来创建一个桌面的截图画布 18 | **/ 19 | 20 | //考虑继承QLabel 21 | Canvas::Canvas(QWidget *parent) : QWidget(parent) 22 | { 23 | okFlag=1; 24 | const QRect screenGeometry = QApplication::desktop()->screenGeometry(this); 25 | screen_width=screenGeometry.width(); 26 | screen_height=screenGeometry.height(); 27 | 28 | //this->setWindowFlags(Qt::WindowStaysOnTopHint); //窗口置顶 29 | this->raise(); //软置顶 30 | this->showFullScreen(); //画布全屏显示 31 | 32 | clipboard = QApplication::clipboard(); //获取系统剪贴板指针 33 | 34 | setMouseTracking(true); //鼠标移动捕捉 35 | 36 | canvasInit(); 37 | 38 | initToolBar(); 39 | 40 | drawEditFlag=0; 41 | } 42 | 43 | void Canvas::canvasInit() 44 | { 45 | pointS.rx()=0; 46 | pointS.ry()=0; 47 | pointE.rx()=0; 48 | pointE.ry()=0; 49 | pointDrag.rx()=0; 50 | pointDrag.ry()=0; 51 | rectFlag=DrawStatus::waitDraw; 52 | 53 | lineList.clear(); 54 | rectList.clear(); 55 | ellipseList.clear(); 56 | textList.clear(); 57 | 58 | drawPen.setBrush(Qt::red); 59 | drawPen.setWidthF(2); 60 | drawPen.setStyle(Qt::SolidLine); 61 | } 62 | 63 | void Canvas::mousePressEvent(QMouseEvent *event) 64 | { 65 | if(event->button()==Qt::LeftButton) 66 | { 67 | if(rectFlag==DrawStatus::waitDraw) 68 | { 69 | pointS.rx()=event->x(); 70 | pointS.ry()=event->y(); 71 | pointE.rx()=event->x(); 72 | pointE.ry()=event->y(); 73 | shotArea=getRectF(pointS,pointE); 74 | rectFlag=DrawStatus::drawing; 75 | } 76 | else if(rectFlag==DrawStatus::drawed) //捕捉拖拽 77 | { 78 | if(drawEditFlag==0) 79 | { 80 | pointDrag.setX(event->x()); 81 | pointDrag.setY(event->y()); 82 | } 83 | else if(drawEditFlag==1) //画线 84 | { 85 | pointS.rx()=event->x(); 86 | pointS.ry()=event->y(); 87 | pointE.rx()=event->x(); 88 | pointE.ry()=event->y(); 89 | 90 | LinePaint tempLine(pointS.toPoint(),pointE.toPoint()); 91 | //qDebug()<<"drawPen"; 92 | tempLine.setPen(drawPen); 93 | lineList.append(tempLine); 94 | } 95 | else if(drawEditFlag==2) //画矩形 96 | { 97 | pointS.rx()=event->x(); 98 | pointS.ry()=event->y(); 99 | pointE.rx()=event->x(); 100 | pointE.ry()=event->y(); 101 | 102 | RectPaint tempRect=getRectF(pointS,pointE); 103 | tempRect.setPen(drawPen); 104 | rectList.append(tempRect); 105 | } 106 | else if(drawEditFlag==3) //画椭圆 107 | { 108 | pointS.rx()=event->x(); 109 | pointS.ry()=event->y(); 110 | pointE.rx()=event->x(); 111 | pointE.ry()=event->y(); 112 | 113 | RectPaint tempRect=getRectF(pointS,pointE); 114 | tempRect.setPen(drawPen); 115 | ellipseList.append(tempRect); 116 | } 117 | else if(drawEditFlag==4) //添加文本 118 | { 119 | int x=event->x(); 120 | int y=event->y(); 121 | TextPaint *curText=new TextPaint(this); 122 | int cnt=textList.length(); 123 | for(int i=0;iclearFocus(); 126 | } 127 | 128 | //QFont tempFont("Timers",32,QFont::Black); 129 | 130 | QFontMetrics fm(textfont); 131 | 132 | curText->setGeometry(x,y,10,fm.height()+2); 133 | curText->setFont(textfont); 134 | QPalette palette; 135 | //palette.setColor(QPalette::Text,Qt::red); 136 | palette.setColor(QPalette::Text,textcolor); 137 | curText->setPalette(palette); 138 | 139 | curText->setVisible(true); 140 | textList.append(curText); 141 | curText->setFocus(); 142 | QWidget::mousePressEvent(event); 143 | update(); 144 | } 145 | } 146 | } 147 | else if(event->button()==Qt::RightButton) //重新绘制区域 148 | { 149 | setCursor(Qt::ArrowCursor); 150 | 151 | if(rectFlag==DrawStatus::waitDraw) 152 | { 153 | slt_cancel(); 154 | } 155 | else 156 | { 157 | canvasInit(); 158 | hideToolBar(); 159 | update(); 160 | } 161 | } 162 | } 163 | 164 | void Canvas::mouseMoveEvent(QMouseEvent *event) 165 | { 166 | if(event->buttons()&Qt::LeftButton) 167 | { 168 | if(rectFlag==DrawStatus::drawing) 169 | { 170 | pointE.setX(event->x()); 171 | pointE.setY(event->y()); 172 | shotArea=getRectF(pointS,pointE); 173 | } 174 | else if(rectFlag==DrawStatus::drawed) //拖拽 175 | { 176 | QPointF tempTL,tempBR; 177 | 178 | tempTL=shotArea.topLeft(); 179 | tempBR=shotArea.bottomRight(); 180 | 181 | switch(cursorCaptureFlag) 182 | { 183 | case 0: //无效区域 184 | setCursor(Qt::ArrowCursor); 185 | break; 186 | case 1: //左上 187 | setCursor(Qt::SizeFDiagCursor); 188 | tempTL.setX(event->x()); 189 | tempTL.setY(event->y()); 190 | break; 191 | case 2: //左下 192 | setCursor(Qt::SizeBDiagCursor); 193 | tempTL.setX(event->x()); 194 | tempBR.setY(event->y()); 195 | break; 196 | case 3: //左边 197 | setCursor(Qt::SizeHorCursor); 198 | tempTL.setX(event->x()); 199 | break; 200 | 201 | case 4: //右上 202 | setCursor(Qt::SizeBDiagCursor); 203 | tempBR.setX(event->x()); 204 | tempTL.setY(event->y()); 205 | break; 206 | case 5: //右下 207 | setCursor(Qt::SizeFDiagCursor); 208 | tempBR.setX(event->x()); 209 | tempBR.setY(event->y()); 210 | break; 211 | case 6: //右边 212 | setCursor(Qt::SizeHorCursor); 213 | tempBR.setX(event->x()); 214 | shotArea=getRectF(pointS,pointE); 215 | break; 216 | case 7: //上边 217 | setCursor(Qt::SizeVerCursor); 218 | tempTL.setY(event->y()); 219 | break; 220 | case 8: //下边 221 | setCursor(Qt::SizeVerCursor); 222 | tempBR.setY(event->y()); 223 | break; 224 | case 9: //中央 225 | if(drawEditFlag==0) 226 | { 227 | setCursor(Qt::SizeAllCursor); 228 | qreal dx=event->x()-pointDrag.x(); //获取坐标差 229 | qreal dy=event->y()-pointDrag.y(); 230 | 231 | pointDrag.setX(event->x()); //刷新拖拽点坐标 232 | pointDrag.setY(event->y()); 233 | 234 | if( (tempTL.x()+dx)>0 && (tempBR.x()+dx)0 && (tempBR.y()+dy)x()); 249 | pointE.setY(event->y()); 250 | 251 | lineList.last().setPoints(pointS.toPoint(),pointE.toPoint()); 252 | } 253 | else if(drawEditFlag==2) //绘矩形 254 | { 255 | pointE.setX(event->x()); 256 | pointE.setY(event->y()); 257 | 258 | rectList.last().setTopLeft(pointS); 259 | rectList.last().setBottomRight(pointE); 260 | } 261 | else if(drawEditFlag==3) //绘椭圆 262 | { 263 | pointE.setX(event->x()); 264 | pointE.setY(event->y()); 265 | 266 | ellipseList.last().setTopLeft(pointS); 267 | ellipseList.last().setBottomRight(pointE); 268 | } 269 | 270 | break; 271 | } 272 | 273 | shotArea.setTopLeft(tempTL); 274 | shotArea.setBottomRight(tempBR); 275 | 276 | showToolBar(); //实时更新工具条位置 277 | } 278 | update(); 279 | } 280 | else if(event->buttons()==Qt::NoButton) //没有按键按下 281 | { 282 | if(rectFlag==DrawStatus::drawed) //捕捉拖拽 283 | { 284 | cursorCaptureFlag=caputerRect(shotArea,event->x(),event->y()); 285 | 286 | switch(cursorCaptureFlag) 287 | { 288 | case 0: //无效区域 289 | setCursor(Qt::ArrowCursor); 290 | break; 291 | case 1: //左上 292 | setCursor(Qt::SizeFDiagCursor); 293 | break; 294 | case 2: //左下 295 | setCursor(Qt::SizeBDiagCursor); 296 | break; 297 | case 3: //左边 298 | setCursor(Qt::SizeHorCursor); 299 | break; 300 | 301 | case 4: //右上 302 | setCursor(Qt::SizeBDiagCursor); 303 | break; 304 | case 5: //右下 305 | setCursor(Qt::SizeFDiagCursor); 306 | break; 307 | case 6: //右边 308 | setCursor(Qt::SizeHorCursor); 309 | break; 310 | case 7: //上边 311 | setCursor(Qt::SizeVerCursor); 312 | break; 313 | case 8: //下边 314 | setCursor(Qt::SizeVerCursor); 315 | break; 316 | case 9: //中央 317 | if(drawEditFlag==0) 318 | { 319 | setCursor(Qt::SizeAllCursor); 320 | } 321 | else //追加图形 322 | { 323 | setCursor(Qt::ArrowCursor); 324 | } 325 | break; 326 | } 327 | 328 | } 329 | } 330 | } 331 | 332 | void Canvas::mouseReleaseEvent(QMouseEvent *event) 333 | { 334 | if(event->button()==Qt::LeftButton) 335 | { 336 | if(rectFlag==DrawStatus::drawing) 337 | { 338 | pointE.setX(event->x()); 339 | pointE.setY(event->y()); 340 | shotArea=getRectF(pointS,pointE); 341 | rectFlag=DrawStatus::drawed; //矩形绘制完成 342 | qDebug()<<"draw end."; 343 | showToolBar(); 344 | } 345 | else if(rectFlag==DrawStatus::drawed) 346 | { 347 | if(drawEditFlag==0) 348 | { 349 | showToolBar(); 350 | } 351 | else if(drawEditFlag==1) 352 | { 353 | if( (pointS.x()==pointE.x()) && (pointS.y()==pointE.y()) )//直线长度为0 354 | { 355 | lineList.removeLast(); 356 | } 357 | } 358 | else if(drawEditFlag==2) 359 | { 360 | if( (pointS.x()==pointE.x()) && (pointS.y()==pointE.y()) )//矩形区域为空 361 | { 362 | rectList.removeLast(); 363 | } 364 | } 365 | else if(drawEditFlag==3) 366 | { 367 | if( (pointS.x()==pointE.x()) && (pointS.y()==pointE.y()) )//矩形区域为空 368 | { 369 | ellipseList.removeLast(); 370 | } 371 | } 372 | } 373 | 374 | update(); 375 | } 376 | } 377 | 378 | void Canvas::paintEvent(QPaintEvent *e) 379 | { 380 | 381 | QPainter painter(this); 382 | 383 | QPixmap tempmask(screen_width, screen_width); 384 | tempmask.fill((QColor(0, 0, 0, 160))); 385 | 386 | painter.drawPixmap(0,0,fullPixmap); //先绘制全屏原图背景 387 | painter.drawPixmap(0,0,tempmask); //然后绘制半透明背景,用来降低亮度 388 | 389 | switch(rectFlag) //截图状态机 390 | { 391 | case DrawStatus::waitDraw: 392 | { 393 | break; 394 | } 395 | case DrawStatus::drawing: 396 | { 397 | painter.setPen(QPen(Qt::green,2,Qt::DashLine));//设置画笔形式 398 | //painter.setBrush(Qt::white); 399 | painter.drawRect(shotArea); //然后绘制矩形框 400 | fullPixmap.setDevicePixelRatio(2.0); 401 | QScreen *screen = QGuiApplication::primaryScreen(); 402 | RectPaint shotAreaOnImage; 403 | shotAreaOnImage.setX(shotArea.x()*screen->orientation()); 404 | shotAreaOnImage.setY(shotArea.y()*screen->orientation()); 405 | shotAreaOnImage.setWidth(shotArea.width()*screen->orientation()); 406 | shotAreaOnImage.setHeight(shotArea.height()*screen->orientation()); 407 | painter.drawPixmap(shotArea,fullPixmap,shotAreaOnImage); //然后将矩形框中的半透明图像替换成原图 408 | break; 409 | } 410 | case DrawStatus::drawed: 411 | { 412 | painter.setPen(QPen(Qt::green,2,Qt::DashLine));//设置画笔形式 413 | //painter.setBrush(Qt::white); 414 | QScreen *screen = QGuiApplication::primaryScreen(); 415 | RectPaint shotAreaOnImage; 416 | shotAreaOnImage.setX(shotArea.x()*screen->orientation()); 417 | shotAreaOnImage.setY(shotArea.y()*screen->orientation()); 418 | shotAreaOnImage.setWidth(shotArea.width()*screen->orientation()); 419 | shotAreaOnImage.setHeight(shotArea.height()*screen->orientation()); 420 | painter.drawRect(shotArea); //然后绘制矩形框 421 | painter.drawPixmap(shotArea,fullPixmap,shotAreaOnImage); //然后将矩形框中的半透明图像替换成原图 422 | break; 423 | } 424 | default: 425 | { 426 | break; 427 | } 428 | } 429 | 430 | quint16 len=lineList.length(); 431 | if(len) 432 | { 433 | for(quint16 i=0;igrabWindow(0,rect.x(),rect.y(),rect.width(),rect.height()); 483 | 484 | } 485 | 486 | void Canvas::initToolBar() //工具条初始化 487 | { 488 | toolbar=new QWidget(this); 489 | 490 | QWidget *MainToolBar=new QWidget(); //主工具栏 491 | QHBoxLayout *mainToolLayout=new QHBoxLayout(); 492 | 493 | btn_cancel=new QPushButton(tr("Quit")); 494 | btn_saveClipboard=new QPushButton(tr("Copy")); 495 | btn_saveFile=new QPushButton(tr("Save")); 496 | 497 | btn_drawLine=new QPushButton(tr("Line")); 498 | btn_drawRect=new QPushButton(tr("Rect")); 499 | btn_drawEllipse=new QPushButton(tr("Ellipse")); 500 | btn_drawText=new QPushButton(tr("Text")); 501 | 502 | btn_cancel->setStyleSheet("background-color: rgb(255, 255, 255);"); 503 | btn_saveClipboard->setStyleSheet("background-color: rgb(255, 255, 255);"); 504 | btn_saveFile->setStyleSheet("background-color: rgb(255, 255, 255);"); 505 | 506 | mainToolLayout->addWidget(btn_drawLine); 507 | mainToolLayout->addWidget(btn_drawRect); 508 | mainToolLayout->addWidget(btn_drawEllipse); 509 | mainToolLayout->addWidget(btn_drawText); 510 | mainToolLayout->addWidget(btn_cancel); 511 | mainToolLayout->addWidget(btn_saveClipboard); 512 | mainToolLayout->addWidget(btn_saveFile); 513 | mainToolLayout->setContentsMargins(0,0,0,0); //去除边框间隙 514 | mainToolLayout->setSpacing(0); 515 | MainToolBar->setLayout(mainToolLayout); 516 | 517 | shapeToolBar=new QWidget(); //形状拓展工具条 518 | QHBoxLayout *shapeToolLayout=new QHBoxLayout(); 519 | 520 | textToolBar=new QWidget(); //文本拓展工具条 521 | QHBoxLayout *textToolLayout=new QHBoxLayout(); 522 | 523 | cbx_lineSize=new QComboBox(); 524 | btn_colorSelect=new QPushButton(); 525 | cbx_lineStyle=new QComboBox(); 526 | 527 | QStringList sizeitems; 528 | sizeitems<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9"<<"10"<<"11"<<"12"<<"13"<<"14"<<"15"<<"16"<<"17"<<"18"<<"19"<<"20"; 529 | cbx_lineSize->addItems(sizeitems); 530 | btn_colorSelect->setStyleSheet("background-color: rgb(255, 0, 0);"); 531 | 532 | cbx_lineStyle->addItem(QIcon(":/pic/1.ico"),""); 533 | cbx_lineStyle->addItem(QIcon(":/pic/2.ico"),""); 534 | cbx_lineStyle->addItem(QIcon(":/pic/3.ico"),""); 535 | cbx_lineStyle->addItem(QIcon(":/pic/4.ico"),""); 536 | cbx_lineStyle->addItem(QIcon(":/pic/5.ico"),""); 537 | cbx_lineStyle->setIconSize(QSize(80,20)); 538 | shapeToolLayout->addWidget(cbx_lineSize); 539 | shapeToolLayout->addWidget(btn_colorSelect); 540 | shapeToolLayout->addWidget(cbx_lineStyle); 541 | shapeToolLayout->addStretch(); 542 | shapeToolLayout->setContentsMargins(0,0,0,0); //去除边框间隙 543 | shapeToolLayout->setSpacing(0); 544 | shapeToolBar->setLayout(shapeToolLayout); 545 | shapeToolBar->setVisible(false); 546 | 547 | //初始化拓展工具的初始状态 548 | cbx_lineSize->setCurrentText(QString::number(drawPen.width())); 549 | QColor color=drawPen.brush().color(); 550 | QString colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(color.red()).arg(color.green()).arg(color.blue()); 551 | btn_colorSelect->setStyleSheet(colorStyle); 552 | switch(drawPen.style()) 553 | { 554 | case Qt::SolidLine: 555 | cbx_lineStyle->setCurrentIndex(0); 556 | break; 557 | case Qt::DashLine: 558 | cbx_lineStyle->setCurrentIndex(1); 559 | break; 560 | case Qt::DotLine: 561 | cbx_lineStyle->setCurrentIndex(2); 562 | break; 563 | case Qt::DashDotLine: 564 | cbx_lineStyle->setCurrentIndex(3); 565 | break; 566 | case Qt::DashDotDotLine: 567 | cbx_lineStyle->setCurrentIndex(4); 568 | break; 569 | default: 570 | cbx_lineStyle->setCurrentIndex(0); 571 | break; 572 | } 573 | 574 | 575 | btn_colorText = new QPushButton(); 576 | btn_FontText = new QPushButton(tr("Font:10")); 577 | 578 | textcolor = QColor(Qt::red); 579 | textfont = QFont("Times", 10, QFont::Bold); 580 | 581 | colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(textcolor.red()).arg(textcolor.green()).arg(textcolor.blue()); 582 | btn_colorText->setStyleSheet(colorStyle); 583 | btn_FontText->setFont(textfont); 584 | 585 | textToolLayout->addWidget(btn_colorText); 586 | textToolLayout->addWidget(btn_FontText); 587 | textToolLayout->addStretch(); 588 | textToolLayout->setContentsMargins(0,0,0,0); //去除边框间隙 589 | textToolLayout->setSpacing(0); 590 | 591 | textToolBar = new QWidget(this); 592 | textToolBar->setLayout(textToolLayout); 593 | textToolBar->setVisible(false); 594 | 595 | QVBoxLayout *toolLayout=new QVBoxLayout(); 596 | toolLayout->addWidget(MainToolBar); 597 | toolLayout->addWidget(shapeToolBar); 598 | toolLayout->addWidget(textToolBar); 599 | toolLayout->setContentsMargins(0,0,0,0); //去除边框间隙 600 | toolLayout->setSpacing(0); 601 | toolbar->setLayout(toolLayout); 602 | toolbar->setVisible(true); //如果不显示的话,后面的width和height会计算不准 603 | toolBarWidth = toolbar->width(); 604 | toolBarHeight = toolbar->height(); 605 | //qDebug()<<"W:"<setVisible(false); 608 | 609 | btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);"); 610 | btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);"); 611 | btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);"); 612 | btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);"); 613 | 614 | hideToolBar(); 615 | 616 | connect(btn_drawLine,SIGNAL(clicked(bool)),this,SLOT(slt_drawLine())); 617 | connect(btn_drawRect,SIGNAL(clicked(bool)),this,SLOT(slt_drawRect())); 618 | connect(btn_drawEllipse,SIGNAL(clicked(bool)),this,SLOT(slt_drawEllipse())); 619 | connect(btn_drawText,SIGNAL(clicked(bool)),this,SLOT(slt_drawText())); 620 | connect(btn_cancel,SIGNAL(clicked(bool)),this,SLOT(slt_cancel())); 621 | connect(btn_saveClipboard,SIGNAL(clicked(bool)),this,SLOT(slt_saveClipboard())); 622 | connect(btn_saveFile,SIGNAL(clicked(bool)),this,SLOT(slt_saveFile())); 623 | 624 | //拓展工具信号槽 625 | connect(cbx_lineSize,SIGNAL(currentTextChanged(QString)),this,SLOT(slt_changePenWidth(QString))); 626 | connect(btn_colorSelect,SIGNAL(clicked(bool)),this,SLOT(slt_changePenColor())); 627 | connect(cbx_lineStyle,SIGNAL(currentIndexChanged(int)),SLOT(slt_changePenStyle(int))); 628 | 629 | connect(btn_colorText,SIGNAL(clicked(bool)),this,SLOT(slt_changeTextColor())); 630 | connect(btn_FontText,SIGNAL(clicked(bool)),SLOT(slt_changeTextFont())); 631 | } 632 | 633 | void Canvas::showToolBar() //显示工具条 634 | { 635 | qreal x,y; 636 | 637 | int bar_width=toolBarWidth;//toolbar->width(); 638 | int bar_height=toolBarHeight;//toolbar->height(); 639 | int offset=5; 640 | 641 | qDebug()<<"bottomLeft().x"<(bar_height+offset)) //下边距充足 657 | { 658 | y=shotArea.bottomLeft().y()+offset; 659 | } 660 | else if(shotArea.topLeft().y()>(bar_height+offset)) //上边距充足 661 | { 662 | y=shotArea.topLeft().y()-(bar_height+offset); 663 | } 664 | else if(shotArea.topLeft().y()<0) //上下边距都不够,且上边距在桌面外 665 | { 666 | y=offset; 667 | } 668 | else //上下边距都不够,且上边距在桌面内 669 | { 670 | y=shotArea.topLeft().y()+offset; 671 | } 672 | toolbar->move(x,y); 673 | toolbar->setVisible(true); 674 | } 675 | 676 | void Canvas::hideToolBar() //掩藏工具条 677 | { 678 | toolbar->setVisible(false); 679 | } 680 | 681 | void Canvas::refrashToolBar() 682 | { 683 | btn_drawLine->setText(tr("Line")); 684 | btn_drawRect->setText(tr("Rect")); 685 | btn_drawEllipse->setText(tr("Ellipse")); 686 | btn_drawText->setText(tr("Text")); 687 | btn_cancel->setText(tr("Quit")); 688 | btn_saveClipboard->setText(tr("Copy")); 689 | btn_saveFile->setText(tr("Save")); 690 | } 691 | 692 | void Canvas::slt_drawLine() 693 | { 694 | if(drawEditFlag!=1) 695 | { 696 | drawEditFlag=1; 697 | btn_drawLine->setStyleSheet("background-color: rgb(146, 189, 108);"); 698 | btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);"); 699 | btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);"); 700 | btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);"); 701 | shapeToolBar->setVisible(true); 702 | textToolBar->setVisible(false); 703 | toolbar->adjustSize(); 704 | showToolBar(); 705 | } 706 | else 707 | { 708 | drawEditFlag=0; 709 | btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);"); 710 | btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);"); 711 | btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);"); 712 | btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);"); 713 | shapeToolBar->setVisible(false); 714 | textToolBar->setVisible(false); 715 | toolbar->adjustSize(); 716 | showToolBar(); 717 | } 718 | } 719 | 720 | void Canvas::slt_drawRect() 721 | { 722 | if(drawEditFlag!=2) 723 | { 724 | drawEditFlag=2; 725 | btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);"); 726 | btn_drawRect->setStyleSheet("background-color: rgb(146, 189, 108);"); 727 | btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);"); 728 | btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);"); 729 | shapeToolBar->setVisible(true); 730 | textToolBar->setVisible(false); 731 | toolbar->adjustSize(); 732 | showToolBar(); 733 | } 734 | else 735 | { 736 | drawEditFlag=0; 737 | btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);"); 738 | btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);"); 739 | btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);"); 740 | btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);"); 741 | shapeToolBar->setVisible(false); 742 | textToolBar->setVisible(false); 743 | toolbar->adjustSize(); 744 | showToolBar(); 745 | } 746 | } 747 | 748 | void Canvas::slt_drawEllipse() 749 | { 750 | if(drawEditFlag!=3) 751 | { 752 | drawEditFlag=3; 753 | btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);"); 754 | btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);"); 755 | btn_drawEllipse->setStyleSheet("background-color: rgb(146, 189, 108);"); 756 | btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);"); 757 | shapeToolBar->setVisible(true); 758 | textToolBar->setVisible(false); 759 | toolbar->adjustSize(); 760 | showToolBar(); 761 | } 762 | else 763 | { 764 | drawEditFlag=0; 765 | btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);"); 766 | btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);"); 767 | btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);"); 768 | btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);"); 769 | shapeToolBar->setVisible(false); 770 | textToolBar->setVisible(false); 771 | toolbar->adjustSize(); 772 | showToolBar(); 773 | } 774 | } 775 | 776 | void Canvas::slt_drawText() 777 | { 778 | if(drawEditFlag!=4) 779 | { 780 | drawEditFlag=4; 781 | btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);"); 782 | btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);"); 783 | btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);"); 784 | btn_drawText->setStyleSheet("background-color: rgb(146, 189, 108);"); 785 | shapeToolBar->setVisible(false); 786 | textToolBar->setVisible(true); 787 | toolbar->adjustSize(); 788 | showToolBar(); 789 | } 790 | else 791 | { 792 | drawEditFlag=0; 793 | btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);"); 794 | btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);"); 795 | btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);"); 796 | btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);"); 797 | shapeToolBar->setVisible(false); 798 | textToolBar->setVisible(false); 799 | toolbar->adjustSize(); 800 | showToolBar(); 801 | } 802 | } 803 | 804 | void Canvas::slt_saveFile() 805 | { 806 | shootScreen(shotArea); 807 | QDateTime current_date_time = QDateTime::currentDateTime(); 808 | QString current_date = current_date_time.toString("yyyyMMdd_hhmmsszzz"); 809 | QString savefile="ScreenShot_"+current_date+".jpg"; 810 | 811 | QString savepath=OperateSet::readSetting("Setting","QuickSaveDir",".").toString(); 812 | 813 | QString fileName = QFileDialog::getSaveFileName(this, 814 | tr("Save File"), 815 | savepath+"/"+savefile, 816 | tr("JPEG File (*.jpg);;BMP File (*.bmp);;PNG File (*.png);;PPM File (*.ppm);;XBM File (*.xbm);;XPM File (*.xpm)") 817 | ); 818 | originalPixmap.save(fileName); 819 | 820 | QFileInfo fi = QFileInfo(fileName); 821 | //QString file_name = fi.fileName(); 822 | QString file_path = fi.absolutePath(); 823 | 824 | if(file_path!=savepath) 825 | { 826 | OperateSet::writeSetting("Setting","QuickSaveDir",file_path); 827 | } 828 | 829 | 830 | slt_cancel(); 831 | } 832 | 833 | void Canvas::slt_saveClipboard() 834 | { 835 | shootScreen(shotArea); 836 | clipboard->setPixmap(originalPixmap); 837 | slt_cancel(); 838 | } 839 | 840 | void Canvas::slt_cancel() 841 | { 842 | rectFlag=DrawStatus::waitDraw; 843 | this->close(); 844 | } 845 | 846 | void Canvas::slt_changePenWidth(QString s) 847 | { 848 | drawPen.setWidth(s.toInt()); 849 | } 850 | 851 | void Canvas::slt_changePenColor() 852 | { 853 | QColor color = QColorDialog::getColor(Qt::blue); 854 | if(color.isValid()) 855 | { 856 | //如果颜色有效的话,将colorFrame设置为选中的颜色 857 | //colorFrame->setPalette(QPalette(color)); 858 | //btn_colorSelect->setPalette(QPalette(color)); 859 | QString colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(color.red()).arg(color.green()).arg(color.blue()); 860 | btn_colorSelect->setStyleSheet(colorStyle); 861 | } 862 | drawPen.setColor(color); 863 | } 864 | 865 | void Canvas::slt_changePenStyle(int index) 866 | { 867 | switch(index) 868 | { 869 | case 0: 870 | drawPen.setStyle(Qt::SolidLine); 871 | break; 872 | case 1: 873 | drawPen.setStyle(Qt::DashLine); 874 | break; 875 | case 2: 876 | drawPen.setStyle(Qt::DotLine); 877 | break; 878 | case 3: 879 | drawPen.setStyle(Qt::DashDotLine); 880 | break; 881 | case 4: 882 | drawPen.setStyle(Qt::DashDotDotLine); 883 | break; 884 | default: 885 | drawPen.setStyle(Qt::SolidLine); 886 | break; 887 | } 888 | } 889 | 890 | void Canvas::slt_changeTextColor() 891 | { 892 | QColor color = QColorDialog::getColor(Qt::blue); 893 | if(color.isValid()) 894 | { 895 | textcolor = color; 896 | QString colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(textcolor.red()).arg(textcolor.green()).arg(textcolor.blue()); 897 | btn_colorText->setStyleSheet(colorStyle); 898 | } 899 | } 900 | 901 | void Canvas::slt_changeTextFont() 902 | { 903 | bool bRet = true; 904 | QFont font = QFontDialog::getFont(&bRet); 905 | if(bRet) 906 | { 907 | textfont = font; 908 | font.setPointSize(10); 909 | btn_FontText->setFont(font); 910 | btn_FontText->setText("Font"+QString::number(textfont.pointSize())); 911 | } 912 | } 913 | 914 | //通过任意两点构造一个矩形 915 | RectPaint Canvas::getRectF(QPointF p1, QPointF p2) 916 | { 917 | float x1,y1,x2,y2; 918 | if(p1.x()=t_rect.topLeft().y()+2) && (t_y<=t_rect.bottomRight().y()-2) ) //上下之间 981 | { 982 | pos=3; 983 | } 984 | else 985 | { 986 | pos=0; 987 | } 988 | } 989 | else if(qAbs(t_x-t_rect.bottomRight().x())<2) //右 990 | { 991 | if(qAbs(t_y-t_rect.topLeft().y())<2) //上 992 | { 993 | pos=4; 994 | } 995 | else if(qAbs(t_y-t_rect.bottomRight().y())<2) //下 996 | { 997 | pos=5; 998 | } 999 | else if( (t_y>=t_rect.topLeft().y()+2) && (t_y<=t_rect.bottomRight().y()-2) ) //上下之间 1000 | { 1001 | pos=6; 1002 | } 1003 | else 1004 | { 1005 | pos=0; 1006 | } 1007 | } 1008 | else if((t_x>=t_rect.topLeft().x()+2) && (t_x<=t_rect.bottomRight().x()-2)) //左右之间 1009 | { 1010 | if(qAbs(t_y-t_rect.topLeft().y())<2) //上 1011 | { 1012 | pos=7; 1013 | } 1014 | else if(qAbs(t_y-t_rect.bottomRight().y())<2) //下 1015 | { 1016 | pos=8; 1017 | } 1018 | else if( (t_y>=t_rect.topLeft().y()+2) && (t_y<=t_rect.bottomRight().y()-2) ) //上下之间 1019 | { 1020 | pos=9; 1021 | } 1022 | else 1023 | { 1024 | pos=0; 1025 | } 1026 | } 1027 | else 1028 | { 1029 | pos=0; 1030 | } 1031 | return pos; 1032 | } 1033 | -------------------------------------------------------------------------------- /canvas.h: -------------------------------------------------------------------------------- 1 | #ifndef CANVAS_H 2 | #define CANVAS_H 3 | 4 | /* 5 | * Author:qiuzhiqian 6 | * Email:xia_mengliang@163.com 7 | * Github:https://github.com/qiuzhiqian 8 | * Date:2017.07.23 9 | **/ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include 34 | #include 35 | 36 | #include "linepaint.h" 37 | #include "rectpaint.h" 38 | #include "textpaint.h" 39 | 40 | enum DrawStatus { 41 | waitDraw=0, 42 | drawing, 43 | drawed, 44 | }; 45 | 46 | 47 | class Canvas : public QWidget 48 | { 49 | Q_OBJECT 50 | public: 51 | explicit Canvas(QWidget *parent = 0); 52 | 53 | void setbgPixmap(QPixmap &px); 54 | void shootScreen(QRectF &rect); 55 | 56 | void canvasInit(); 57 | 58 | void initToolBar(); 59 | void showToolBar(); 60 | void hideToolBar(); 61 | void refrashToolBar(); 62 | 63 | //void initShapeToolBar(); 64 | //void showShapeToolBar(); 65 | //void hideShapeToolBar(); 66 | 67 | //void initShapeToolBar(); 68 | //void showShapeToolBar(); 69 | //void hideShapeToolBar(); 70 | 71 | quint8 caputerRect(QRectF t_rect,qreal t_x,qreal t_y); 72 | 73 | RectPaint getRectF(QPointF p1,QPointF p2); //通过两个坐标点生成矩形 74 | 75 | void changeLanguage(QString lan); 76 | 77 | signals: 78 | 79 | public slots: 80 | void slt_drawLine(); 81 | void slt_drawRect(); 82 | void slt_drawEllipse(); 83 | void slt_drawText(); 84 | void slt_saveFile(); //保存到文件 85 | void slt_saveClipboard(); //保存到剪切板 86 | void slt_cancel(); 87 | 88 | void slt_changePenWidth(QString s); 89 | void slt_changePenColor(); 90 | void slt_changePenStyle(int index); 91 | 92 | void slt_changeTextColor(); 93 | void slt_changeTextFont(); 94 | 95 | private: 96 | int screen_width=0,screen_height=0; 97 | quint8 okFlag=0; 98 | 99 | QPointF pointS; //鼠标绘制起点 100 | QPointF pointE; //鼠标绘制终点 101 | RectPaint shotArea; //截图区域 102 | 103 | QList lineList; //直线列表 104 | QList rectList; //矩形列表 105 | QList ellipseList; //椭圆列表 106 | QList textList; //文本列表 107 | 108 | QPointF pointDrag; //拖拽点 109 | 110 | DrawStatus rectFlag=DrawStatus::waitDraw; 111 | quint8 drawEditFlag=0; //绘图修改 112 | 113 | QPixmap fullPixmap; //原始全屏图片 114 | QPixmap originalPixmap; 115 | 116 | QWidget *toolbar; //工具条 117 | QPushButton *btn_saveFile; 118 | QPushButton *btn_saveClipboard; 119 | QPushButton *btn_cancel; 120 | QPushButton *btn_drawLine; //画直线 121 | QPushButton *btn_drawRect; //画矩形 122 | QPushButton *btn_drawEllipse; //画椭圆 123 | QPushButton *btn_drawText; //添加文本 124 | 125 | QPen drawPen; //线宽颜色风格 126 | QWidget *shapeToolBar; 127 | QWidget *textToolBar; 128 | 129 | int toolBarWidth; 130 | int toolBarHeight; 131 | 132 | QComboBox *cbx_lineSize; 133 | QPushButton *btn_colorSelect; 134 | QComboBox *cbx_lineStyle; 135 | 136 | QPushButton *btn_colorText; 137 | QPushButton *btn_FontText; 138 | QColor textcolor; 139 | QFont textfont; 140 | 141 | QClipboard *clipboard; 142 | 143 | quint8 cursorCaptureFlag=0; 144 | 145 | void mousePressEvent(QMouseEvent *event); 146 | void mouseMoveEvent(QMouseEvent *event); 147 | void mouseReleaseEvent(QMouseEvent *event); 148 | void paintEvent(QPaintEvent *e); 149 | }; 150 | 151 | #endif // CANVAS_H 152 | -------------------------------------------------------------------------------- /doc/1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/doc/1.gif -------------------------------------------------------------------------------- /doc/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/doc/1.jpg -------------------------------------------------------------------------------- /doc/2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/doc/2.gif -------------------------------------------------------------------------------- /doc/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/doc/2.jpg -------------------------------------------------------------------------------- /doc/3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/doc/3.gif -------------------------------------------------------------------------------- /doc/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/doc/3.jpg -------------------------------------------------------------------------------- /doc/4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/doc/4.gif -------------------------------------------------------------------------------- /en.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Canvas 6 | 7 | 8 | 9 | Quit 10 | Quit 11 | 12 | 13 | 14 | 15 | Copy 16 | Copy 17 | 18 | 19 | 20 | 21 | Save 22 | Save 23 | 24 | 25 | 26 | 27 | Line 28 | Line 29 | 30 | 31 | 32 | 33 | Rect 34 | Rect 35 | 36 | 37 | 38 | 39 | Ellipse 40 | Ellipse 41 | 42 | 43 | 44 | 45 | Text 46 | Text 47 | 48 | 49 | 50 | Font:10 51 | Font:10 52 | 53 | 54 | 55 | Save File 56 | Save File 57 | 58 | 59 | 60 | JPEG File (*.jpg);;BMP File (*.bmp);;PNG File (*.png);;PPM File (*.ppm);;XBM File (*.xbm);;XPM File (*.xpm) 61 | JPEG File (*.jpg);;BMP File (*.bmp);;PNG File (*.png);;PPM File (*.ppm);;XBM File (*.xbm);;XPM File (*.xpm) 62 | 63 | 64 | JPEG File (*.jpg) 65 | JPEG File (*.jpg) 66 | 67 | 68 | 69 | ScreenShotTool 70 | 71 | 72 | ScreenShotTool 73 | ScreenShotTool 74 | 75 | 76 | 77 | Global HotKey 78 | Global HotKey 79 | 80 | 81 | 82 | ShotCut Key 83 | ShotCut Key 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | Setting 92 | Setting 93 | 94 | 95 | 96 | Run on system startup 97 | Run on system startup 98 | 99 | 100 | 101 | Language 102 | Language 103 | 104 | 105 | English 106 | English 107 | 108 | 109 | 中文 110 | 中文 111 | 112 | 113 | 114 | 115 | About 116 | About 117 | 118 | 119 | 120 | 121 | Quit 122 | Quit 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /hotkeybar.cpp: -------------------------------------------------------------------------------- 1 | #include "hotkeybar.h" 2 | 3 | /* 4 | * Author:qiuzhiqian 5 | * Email:xia_mengliang@163.com 6 | * Github:https://github.com/qiuzhiqian 7 | * Date:2017.07.23 8 | * Description:这个类主要用来创建一个添加了一些自定义功能的LineEdit,这个LineEdit用来设置和显示当前的截图快捷键 9 | **/ 10 | 11 | HotKeyBar::HotKeyBar(QWidget *parent) : 12 | QLineEdit(parent) 13 | { 14 | keymod=Qt::ControlModifier; 15 | keyval=Qt::Key_F1; 16 | this->setFocusPolicy(Qt::FocusPolicy(Qt::TabFocus|Qt::ClickFocus)); 17 | this->setStyleSheet("QLineEdit{border:2px solid rgb(240, 240, 240);}"); 18 | } 19 | 20 | void HotKeyBar::setHotKey(Qt::Key t_key, Qt::KeyboardModifiers t_mod) 21 | { 22 | keyval=t_key; 23 | keymod=t_mod; 24 | setShowText(t_key,t_mod); 25 | } 26 | 27 | void HotKeyBar::setKeyString(KeyString *t_keystring) 28 | { 29 | keystring=t_keystring; 30 | } 31 | 32 | void HotKeyBar::setShowText(Qt::Key t_key, Qt::KeyboardModifiers t_mod) 33 | { 34 | QString showtext=keystring->Key2String(t_key,t_mod); 35 | setText(showtext); 36 | } 37 | 38 | void HotKeyBar::setShowText(QString keystr) 39 | { 40 | setText(keystr); 41 | } 42 | 43 | void HotKeyBar::focusInEvent(QFocusEvent *e) 44 | { 45 | QLineEdit::focusInEvent(e); 46 | this->setStyleSheet("QLineEdit{border:2px solid rgb(163, 219, 144);}"); 47 | } 48 | 49 | void HotKeyBar::focusOutEvent(QFocusEvent *e) 50 | { 51 | QLineEdit::focusOutEvent(e); 52 | this->setStyleSheet("QLineEdit{border:2px solid rgb(240, 240, 240);}"); 53 | } 54 | 55 | void HotKeyBar::keyPressEvent(QKeyEvent *event) 56 | { 57 | keymod=event->modifiers(); 58 | keyval=Qt::Key(event->key()); 59 | 60 | sgn_hotKeyChanged(keyval,keymod); 61 | 62 | setShowText(keyval,keymod); 63 | 64 | 65 | } 66 | -------------------------------------------------------------------------------- /hotkeybar.h: -------------------------------------------------------------------------------- 1 | #ifndef HOTKEYBAR_H 2 | #define HOTKEYBAR_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "KeyString.h" 9 | 10 | /* 11 | * Author:qiuzhiqian 12 | * Email:xia_mengliang@163.com 13 | * Github:https://github.com/qiuzhiqian 14 | * Date:2017.07.23 15 | **/ 16 | 17 | class HotKeyBar : public QLineEdit 18 | { 19 | Q_OBJECT 20 | public: 21 | HotKeyBar(QWidget *parent = 0); 22 | void setHotKey(Qt::Key t_key, Qt::KeyboardModifiers t_mod); 23 | void setShowText(Qt::Key t_key, Qt::KeyboardModifiers t_mod); 24 | void setShowText(QString keystr); 25 | void setKeyString(KeyString *t_keystring); 26 | 27 | private: 28 | void focusInEvent(QFocusEvent *e); //获取焦点 29 | void focusOutEvent(QFocusEvent *e); //失去焦点 30 | void keyPressEvent(QKeyEvent *event); 31 | 32 | Qt::KeyboardModifiers keymod; 33 | Qt::Key keyval; 34 | 35 | KeyString *keystring; 36 | 37 | signals: 38 | void sgn_hotKeyChanged(Qt::Key t_key,Qt::KeyboardModifiers t_mod); 39 | }; 40 | 41 | #endif // HOTKEYBAR_H 42 | -------------------------------------------------------------------------------- /linepaint.cpp: -------------------------------------------------------------------------------- 1 | #include "linepaint.h" 2 | #include 3 | 4 | LinePaint::LinePaint() 5 | { 6 | 7 | } 8 | 9 | LinePaint::LinePaint(const QPoint &p1, const QPoint &p2) 10 | :QLine(p1,p2) 11 | { 12 | } 13 | 14 | LinePaint::LinePaint(int x1, int y1, int x2, int y2) 15 | :QLine(x1,y1,x2,y2) 16 | { 17 | } 18 | 19 | void LinePaint::setPen(QPen pen) 20 | { 21 | drawPen=pen; 22 | // qDebug()< 5 | #include 6 | #include 7 | #include 8 | 9 | class LinePaint : public QLine 10 | { 11 | public: 12 | LinePaint(); 13 | LinePaint(const QPoint &p1, const QPoint &p2); 14 | LinePaint(int x1, int y1, int x2, int y2); 15 | 16 | void setPen(QPen pen); 17 | QPen getPen(); 18 | 19 | private: 20 | QPen drawPen; 21 | }; 22 | 23 | #endif // LINEPAINT_H 24 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "screenshottool.h" 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | /* 8 | * Author:qiuzhiqian 9 | * Email:xia_mengliang@163.com 10 | * Github:https://github.com/qiuzhiqian 11 | * Date:2017.07.23 12 | **/ 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | QApplication a(argc, argv); 17 | 18 | a.setQuitOnLastWindowClosed(false); //防止窗体全部掩藏后意外退出 19 | ScreenShotTool w; 20 | w.hide(); 21 | 22 | return a.exec(); 23 | } 24 | -------------------------------------------------------------------------------- /operateSet.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/operateSet.cpp -------------------------------------------------------------------------------- /operateSet.h: -------------------------------------------------------------------------------- 1 | #ifndef OPERATESET_H 2 | #define OPERATESET_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /* 9 | * Author:qiuzhiqian 10 | * Email:xia_mengliang@163.com 11 | * Github:https://github.com/qiuzhiqian 12 | * Date:2017.07.23 13 | * Description:这个类是由一些静态方法组成,主要完成对ini文件的读写 14 | **/ 15 | 16 | class OperateSet{ 17 | public: 18 | static void writeSetting(const QString &group, const QString &key, const QVariant &value) //写ini文件 19 | { 20 | QSettings settings("./setting.ini", QSettings::IniFormat); // 当前目录的INI文件 21 | settings.beginGroup(group); 22 | settings.setValue(key,value); 23 | } 24 | 25 | static QVariant readSetting(const QString &group, const QString &key, const QVariant &defaultvalue) //读ini文件 26 | { 27 | QVariant val; 28 | QString keypos=group+"/"+key; 29 | QSettings settings("./setting.ini", QSettings::IniFormat); // 当前目录的INI文件 30 | val=settings.value(keypos,defaultvalue); 31 | return val; 32 | } 33 | }; 34 | 35 | #endif //OPERATESET_H 36 | -------------------------------------------------------------------------------- /pic/1.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/pic/1.ico -------------------------------------------------------------------------------- /pic/2.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/pic/2.ico -------------------------------------------------------------------------------- /pic/3.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/pic/3.ico -------------------------------------------------------------------------------- /pic/4.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/pic/4.ico -------------------------------------------------------------------------------- /pic/5.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/pic/5.ico -------------------------------------------------------------------------------- /rectpaint.cpp: -------------------------------------------------------------------------------- 1 | #include "rectpaint.h" 2 | 3 | RectPaint::RectPaint() 4 | { 5 | 6 | } 7 | 8 | RectPaint::RectPaint(const QPointF &topLeft, const QSizeF &size) 9 | :QRectF(topLeft,size) 10 | { 11 | } 12 | RectPaint::RectPaint(const QPointF &topLeft, const QPointF &bottomRight) 13 | :QRectF(topLeft,bottomRight) 14 | { 15 | } 16 | 17 | RectPaint::RectPaint(qreal x, qreal y, qreal width, qreal height) 18 | :QRectF(x,y,width,height) 19 | { 20 | } 21 | 22 | RectPaint::RectPaint(const QRect &rectangle) 23 | :QRectF(rectangle) 24 | { 25 | 26 | } 27 | 28 | void RectPaint::setPen(QPen pen) 29 | { 30 | drawPen=pen; 31 | } 32 | 33 | QPen RectPaint::getPen() 34 | { 35 | return drawPen; 36 | } 37 | -------------------------------------------------------------------------------- /rectpaint.h: -------------------------------------------------------------------------------- 1 | #ifndef RECTPAINT_H 2 | #define RECTPAINT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | class RectPaint : public QRectF 11 | { 12 | public: 13 | RectPaint(); 14 | RectPaint(const QPointF &topLeft, const QSizeF &size); 15 | RectPaint(const QPointF &topLeft, const QPointF &bottomRight); 16 | RectPaint(qreal x, qreal y, qreal width, qreal height); 17 | RectPaint(const QRect &rectangle); 18 | 19 | 20 | void setPen(QPen pen); 21 | QPen getPen(); 22 | 23 | private: 24 | QPen drawPen; 25 | }; 26 | 27 | #endif // RECTPAINT_H 28 | -------------------------------------------------------------------------------- /screenshottool.cpp: -------------------------------------------------------------------------------- 1 | #include "screenshottool.h" 2 | #include "ui_screenshottool.h" 3 | 4 | #include 5 | #include 6 | 7 | #include "operateSet.h" 8 | 9 | #include 10 | 11 | 12 | /* 13 | * Author:qiuzhiqian 14 | * Email:xia_mengliang@163.com 15 | * Github:https://github.com/qiuzhiqian 16 | * Date:2017.07.23 17 | * Description:这个类主要用来进行截图工具的设置 18 | **/ 19 | 20 | /* 21 | * 画图原理 22 | * 1\首先在全屏幕设置一个透明画布 23 | * 2\然后在透明画布上绘制矩形 24 | * 3\对画布上的矩形区域截图 25 | * 4\保存截图到文件夹或剪切板 26 | * 修正 27 | * 1\全屏截屏,同时生成一个暗色图片 28 | * 2\设置全屏画布,然后将暗色全屏图片显示到画布 29 | * 3\在暗色全屏图片上画矩形,矩形内显示亮色图片 30 | * 4\对矩形区域截图 31 | * 5\保存截图到文件夹或剪切板 32 | * */ 33 | 34 | /* 35 | * 截图区域调整策略 36 | * 左边--pointLT.x 37 | * 右边--pointRB.x 38 | * 上边--pointLT.y 39 | * 下边--pointRB.y 40 | * 41 | * 为简化操作,将角调整转化为同时两个边的调整 42 | * 左上--同时作用左边、上边 43 | * 右下--同时作用右边、下边 44 | * 左下--同时作用左边、下边 45 | * 右上--同时作用右边、上边 46 | * */ 47 | 48 | ScreenShotTool::ScreenShotTool(QWidget *parent) : 49 | QWidget(parent), 50 | ui(new Ui::ScreenShotTool) 51 | { 52 | ui->setupUi(this); 53 | 54 | QWidget * xparent = new QWidget; //设置一个空的父对象,从而达到掩藏任务栏图标的目的 55 | 56 | setParent(xparent); 57 | 58 | this->setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint|Qt::WindowStaysOnTopHint); 59 | this->setWindowTitle(tr("Setting")); 60 | this->setWindowIcon(QIcon(":/ss.ico")); 61 | 62 | keystring=new KeyString(); 63 | 64 | sc_set=new HotKeyBar(); 65 | sc_set->setKeyString(keystring); //传递keystring指针 66 | sc_set->setReadOnly(true); 67 | ui->gridLayout->addWidget(sc_set,0,1); 68 | connect(sc_set,SIGNAL(sgn_hotKeyChanged(Qt::Key,Qt::KeyboardModifiers)),this,SLOT(slt_changeHotKey(Qt::Key,Qt::KeyboardModifiers))); 69 | connect(ui->checkBox,SIGNAL(stateChanged(int)),this,SLOT(slt_auto_run(int))); 70 | connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(slt_language_set(int))); 71 | 72 | initTray(); //托盘显示 73 | 74 | QString keystr=OperateSet::readSetting("HotKey","ScreenShot","Ctrl+F1").toString(); 75 | sc_set->setShowText(keystr); //设置快捷键显示条内容 76 | 77 | isAutoRun=bool(OperateSet::readSetting("Setting","AutoRun",0).toInt()); 78 | ui->checkBox->setChecked(isAutoRun); 79 | setAutoRun(isAutoRun); 80 | 81 | QString language=OperateSet::readSetting("Setting","Language","en").toString(); 82 | changeLanguage(language); 83 | if(language=="en") 84 | { 85 | ui->comboBox->setCurrentIndex(0); 86 | } 87 | else 88 | { 89 | ui->comboBox->setCurrentIndex(1); 90 | } 91 | 92 | this->setFixedSize(this->size()); 93 | 94 | //设置快捷键 95 | shortcut = new QxtGlobalShortcut(this); 96 | shortcut->setShortcut(QKeySequence(keystr)); 97 | connect(shortcut, SIGNAL(activated()),this,SLOT(slt_doShot())); 98 | } 99 | 100 | ScreenShotTool::~ScreenShotTool() 101 | { 102 | //unregisterHotKey(key,mods); 103 | delete ui; 104 | } 105 | 106 | void ScreenShotTool::ss_start() //开始截屏 107 | { 108 | QScreen *screen = QGuiApplication::primaryScreen(); 109 | fullPixmap = screen->grabWindow(0); 110 | 111 | screenCanvas=new Canvas(0); //创建画布 112 | 113 | screenCanvas->setbgPixmap(fullPixmap); //传递全屏背景图片 114 | 115 | if(ui->comboBox->currentIndex()==0) 116 | { 117 | screenCanvas->changeLanguage("en"); 118 | } 119 | else if(ui->comboBox->currentIndex()==1) 120 | { 121 | screenCanvas->changeLanguage("zh_cn"); 122 | } 123 | } 124 | 125 | void ScreenShotTool::initTray() //初始化托盘 126 | { 127 | m_systemTray = new QSystemTrayIcon(this); 128 | m_systemTray->setIcon(QIcon(":/ss.ico")); 129 | m_systemTray->setToolTip("ScreenShot"); 130 | 131 | setAction=new QAction(tr("Setting"),this); 132 | aboutAction=new QAction(tr("About"),this); 133 | exitAction=new QAction(tr("Quit"),this); 134 | 135 | QMenu *trayMenu=new QMenu(this); 136 | trayMenu->addAction(setAction); 137 | trayMenu->addAction(aboutAction); 138 | trayMenu->addSeparator(); 139 | trayMenu->addAction(exitAction); 140 | 141 | m_systemTray->setContextMenu(trayMenu); 142 | 143 | m_systemTray->show(); 144 | 145 | connect(m_systemTray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(slt_clickTray(QSystemTrayIcon::ActivationReason))); 146 | connect(setAction,SIGNAL(triggered(bool)),this,SLOT(slt_setAction())); 147 | connect(aboutAction,SIGNAL(triggered(bool)),this,SLOT(slt_aboutAction())); 148 | connect(exitAction,SIGNAL(triggered(bool)),this,SLOT(slt_exitAction())); 149 | } 150 | 151 | void ScreenShotTool::slt_clickTray(QSystemTrayIcon::ActivationReason reason) 152 | { 153 | switch(reason) 154 | { 155 | case QSystemTrayIcon::Trigger: 156 | //单击托盘图标,开始截图 157 | ss_start(); 158 | break; 159 | case QSystemTrayIcon::DoubleClick: 160 | //双击托盘图标 161 | //双击后显示设置窗口 162 | this->show(); 163 | break; 164 | default: 165 | break; 166 | } 167 | } 168 | 169 | void ScreenShotTool::slt_setAction() 170 | { 171 | this->show(); 172 | } 173 | 174 | void ScreenShotTool::slt_aboutAction() 175 | { 176 | 177 | } 178 | 179 | void ScreenShotTool::slt_exitAction() 180 | { 181 | closeFlag=true; 182 | this->close(); 183 | QApplication::quit(); //程序退出 184 | } 185 | 186 | void ScreenShotTool::closeEvent(QCloseEvent *event) 187 | { 188 | if(closeFlag==true) 189 | { 190 | event->accept(); 191 | } 192 | else 193 | { 194 | this->hide(); 195 | event->ignore(); 196 | } 197 | } 198 | 199 | void ScreenShotTool::setHotKey() 200 | { 201 | key=Qt::Key_F1; 202 | mods=Qt::ControlModifier; 203 | //registerHotKey(key,mods); 204 | } 205 | 206 | void ScreenShotTool::slt_changeHotKey(Qt::Key t_key, Qt::KeyboardModifiers t_mod) //快捷键修改 207 | { 208 | //unregisterHotKey(key,mods); 209 | key=t_key; 210 | mods=t_mod; 211 | //registerHotKey(key,mods); 212 | 213 | QString strval=keystring->Key2String(key,mods); 214 | 215 | OperateSet::writeSetting("HotKey","ScreenShot",strval); 216 | 217 | qDebug()<setShortcut(QKeySequence(strval)); 219 | } 220 | 221 | void ScreenShotTool::slt_auto_run(int states) 222 | { 223 | if(states == Qt::Checked) 224 | { 225 | isAutoRun=true; 226 | OperateSet::writeSetting("Setting","AutoRun",1); 227 | } 228 | else 229 | { 230 | isAutoRun=false; 231 | OperateSet::writeSetting("Setting","AutoRun",0); 232 | } 233 | 234 | setAutoRun(isAutoRun); 235 | } 236 | 237 | void ScreenShotTool::slt_language_set(int index) 238 | { 239 | if(index==0) //en 240 | { 241 | changeLanguage("en"); 242 | } 243 | else if(index==1) //zh_cn 244 | { 245 | changeLanguage("zh_cn"); 246 | } 247 | } 248 | 249 | void ScreenShotTool::setAutoRun(bool sta) 250 | { 251 | QSettings *reg=new QSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat); 252 | if(sta == true) 253 | { 254 | reg->setValue("ScreenShotTool",QApplication::applicationFilePath().replace("/","\\")); 255 | } 256 | else 257 | { 258 | reg->remove("ScreenShotTool"); 259 | } 260 | } 261 | 262 | void ScreenShotTool::changeLanguage(QString lan) 263 | { 264 | QTranslator translator; 265 | bool sta=false; 266 | 267 | if(lan=="zh_cn") //简体中文 268 | { 269 | sta=translator.load("zh_cn.qm"); 270 | OperateSet::writeSetting("Setting","Language","zh_cn"); 271 | } 272 | else if(lan=="en") 273 | { 274 | sta=translator.load("en.qm"); 275 | OperateSet::writeSetting("Setting","Language","en"); 276 | } 277 | 278 | if(sta) 279 | { 280 | QApplication::installTranslator(&translator); 281 | ui->retranslateUi(this); //刷新ui语言翻译 282 | reFrash(); //刷新自定义语言翻译 283 | } 284 | } 285 | 286 | void ScreenShotTool::slt_doShot(void) 287 | { 288 | qDebug()<<"doShot"; 289 | ss_start(); 290 | } 291 | 292 | void ScreenShotTool::reFrash() 293 | { 294 | this->setWindowTitle(tr("Setting")); 295 | setAction->setText(tr("Setting")); 296 | aboutAction->setText(tr("About")); 297 | exitAction->setText(tr("Quit")); 298 | } 299 | -------------------------------------------------------------------------------- /screenshottool.h: -------------------------------------------------------------------------------- 1 | #ifndef SCREENSHOTTOOL_H 2 | #define SCREENSHOTTOOL_H 3 | 4 | /* 5 | * Author:qiuzhiqian 6 | * Email:xia_mengliang@163.com 7 | * Github:https://github.com/qiuzhiqian 8 | * Date:2017.07.23 9 | **/ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | 20 | #include 21 | 22 | #include "canvas.h" 23 | #include "hotkeybar.h" 24 | 25 | #include "qxtglobalshortcut.h" 26 | 27 | namespace Ui { 28 | class ScreenShotTool; 29 | } 30 | 31 | class ScreenShotTool : public QWidget 32 | { 33 | Q_OBJECT 34 | 35 | public: 36 | explicit ScreenShotTool(QWidget *parent = 0); 37 | ~ScreenShotTool(); 38 | 39 | void initTray(); //初始化托盘 40 | void ss_start(); //开始截图 41 | 42 | void setHotKey(); 43 | 44 | void setAutoRun(bool sta); 45 | 46 | void changeLanguage(QString lan); 47 | void reFrash(); 48 | 49 | private: 50 | Ui::ScreenShotTool *ui; 51 | 52 | Canvas *screenCanvas; 53 | 54 | QPixmap fullPixmap; 55 | 56 | QSystemTrayIcon *m_systemTray; //系统托盘 57 | QAction *setAction; 58 | QAction *aboutAction; 59 | QAction *exitAction; 60 | 61 | void closeEvent( QCloseEvent * event ); //重写退出 62 | bool closeFlag=false; 63 | 64 | HotKeyBar *sc_set; 65 | 66 | Qt::Key key; 67 | Qt::KeyboardModifiers mods; 68 | KeyString *keystring; 69 | 70 | bool isAutoRun=false; 71 | 72 | QxtGlobalShortcut *shortcut; 73 | 74 | signals: 75 | void appQuit(); 76 | 77 | 78 | private slots: 79 | 80 | void slt_clickTray(QSystemTrayIcon::ActivationReason reason); 81 | void slt_setAction(); 82 | void slt_aboutAction(); 83 | void slt_exitAction(); 84 | 85 | void slt_changeHotKey(Qt::Key t_key, Qt::KeyboardModifiers t_mod); 86 | 87 | void slt_auto_run(int states); 88 | void slt_language_set(int index); 89 | 90 | void slt_doShot(void); 91 | }; 92 | 93 | #endif // SCREENSHOTTOOL_H 94 | -------------------------------------------------------------------------------- /screenshottool.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | ss.ico 4 | pic/1.ico 5 | pic/2.ico 6 | pic/3.ico 7 | pic/4.ico 8 | pic/5.ico 9 | 10 | 11 | -------------------------------------------------------------------------------- /screenshottool.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | ScreenShotTool 4 | 5 | 6 | 7 | 0 8 | 0 9 | 316 10 | 209 11 | 12 | 13 | 14 | ScreenShotTool 15 | 16 | 17 | 18 | 19 | 20 | Global HotKey 21 | 22 | 23 | 24 | 25 | 26 | ShotCut Key 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | Setting 37 | 38 | 39 | 40 | 41 | 42 | Run on system startup 43 | 44 | 45 | 46 | 47 | 48 | 49 | true 50 | 51 | 52 | 53 | 54 | 55 | false 56 | 57 | 58 | 59 | 60 | 61 | 62 | Language 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | English 71 | 72 | 73 | 74 | 75 | 中文 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /setting.ini: -------------------------------------------------------------------------------- 1 | [Hot%20key] 2 | ScreenShot=Ctrl+F1 3 | 4 | [Setting] 5 | AutoRun=0 6 | Language=zh_cn 7 | -------------------------------------------------------------------------------- /ss.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuzhiqian/ScreenShotTool/566484bcebb75d90b59cef8e7a90b2405823a9aa/ss.ico -------------------------------------------------------------------------------- /textpaint.cpp: -------------------------------------------------------------------------------- 1 | #include "textpaint.h" 2 | #include 3 | 4 | TextPaint::TextPaint(QWidget *parent):QLineEdit(parent) 5 | { 6 | //setSizeIncrement(); 7 | connect(this,SIGNAL(textChanged(QString)),this,SLOT(slt_autoResize())); 8 | connect(this,SIGNAL(editingFinished()),this,SLOT(slt_editEnd())); 9 | } 10 | 11 | void TextPaint::slt_autoResize(void) 12 | { 13 | //QStaticText tempText(this->text()); 14 | //this->setFixedWidth(tempText.size().width()+6); 15 | //this->setFixedHeight(tempText.size().height()+6); 16 | QFontMetrics fm(this->font()); 17 | //this->setFixedWidth(fm.boundingRect(this->text()).width()); 18 | this->setFixedWidth(fm.width(this->text())+6); 19 | this->setFixedHeight(fm.height()+2); 20 | //this->adjustSize(); 21 | } 22 | 23 | void TextPaint::slt_editEnd(void) 24 | { 25 | this->setDisabled(true); 26 | //this->setReadOnly(true); 27 | this->setStyleSheet("background:transparent;border-width:0;border-style:outset"); 28 | //this->setStyleSheet("color:red");//文本颜色 29 | } 30 | -------------------------------------------------------------------------------- /textpaint.h: -------------------------------------------------------------------------------- 1 | #ifndef TEXTPAINT_H 2 | #define TEXTPAINT_H 3 | 4 | #include 5 | 6 | class TextPaint : public QLineEdit 7 | { 8 | Q_OBJECT 9 | public: 10 | TextPaint(QWidget *parent = Q_NULLPTR); 11 | 12 | private: 13 | void autoResize(void); 14 | 15 | public slots: 16 | void slt_autoResize(void); 17 | void slt_editEnd(void); 18 | }; 19 | 20 | #endif // TEXTPAINT_H 21 | -------------------------------------------------------------------------------- /zh_cn.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Canvas 6 | 7 | 8 | 9 | Quit 10 | 退出 11 | 12 | 13 | 14 | 15 | Copy 16 | 复制 17 | 18 | 19 | 20 | 21 | Save 22 | 保存 23 | 24 | 25 | 26 | 27 | Line 28 | 直线 29 | 30 | 31 | 32 | 33 | Rect 34 | 矩形 35 | 36 | 37 | 38 | 39 | Ellipse 40 | 椭圆 41 | 42 | 43 | 44 | 45 | Text 46 | 文本框 47 | 48 | 49 | 50 | Font:10 51 | Font:10 52 | 53 | 54 | 55 | Save File 56 | 保存文件 57 | 58 | 59 | 60 | JPEG File (*.jpg);;BMP File (*.bmp);;PNG File (*.png);;PPM File (*.ppm);;XBM File (*.xbm);;XPM File (*.xpm) 61 | JPEG 文件 (*.jpg);;BMP 文件 (*.bmp);;PNG 文件 (*.png);;PPM 文件 (*.ppm);;XBM 文件 (*.xbm);;XPM 文件 (*.xpm) 62 | 63 | 64 | JPEG File (*.jpg) 65 | JPEG文件 (*.jpg) 66 | 67 | 68 | 69 | ScreenShotTool 70 | 71 | 72 | ScreenShotTool 73 | ScreenShotTool 74 | 75 | 76 | 77 | Global HotKey 78 | 全局热键 79 | 80 | 81 | 82 | ShotCut Key 83 | 截图热键 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | Setting 92 | 设置 93 | 94 | 95 | 96 | Run on system startup 97 | 开机自启动 98 | 99 | 100 | 101 | Language 102 | 语言 103 | 104 | 105 | English 106 | English 107 | 108 | 109 | 中文 110 | 中文 111 | 112 | 113 | 114 | 115 | About 116 | 关于 117 | 118 | 119 | 120 | 121 | Quit 122 | 退出 123 | 124 | 125 | 126 | --------------------------------------------------------------------------------