#include "screencapturedlg.h"
#include "ui_screencapturedlg.h"
ScreenCaptureDlg::ScreenCaptureDlg(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ScreenCaptureDlg)
{
ui->setupUi(this);
//初始化数据
Init_Data();
}
ScreenCaptureDlg::~ScreenCaptureDlg()
{
delete ui;
}
//初始化数据
ScreenCaptureDlg::Init_Data()
{
this->setWindowFlag(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setFocusPolicy(Qt::StrongFocus);
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &ScreenCaptureDlg::MouseShape);
m_timer->start(200);
// 置顶
::SetWindowPos(HWND(this->winId()), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
//开始截图
{
QScreen *pScreen = QGuiApplication::primaryScreen();
m_pixmap = pScreen->grabWindow(0);
this->setGeometry(0, 0, m_pixmap.width(), m_pixmap.height());
}
//先定义一个QAction的对象.
QAction *action_Save_to_clipboard = new QAction(this);
//设置触发QAction对象的快捷操作.
action_Save_to_clipboard->setShortcut(tr("Ctrl+x")); //全选
//把这个QAction的对象加入到tableWidget
addAction(action_Save_to_clipboard);
//连接信号与槽.连接好了以后,当你按下ctrl+o时,就会调用槽函数,也就是这里自定义的openfile()函数;
connect(action_Save_to_clipboard, &QAction::triggered, this, &ScreenCaptureDlg::addAction_clicked);
}
void ScreenCaptureDlg::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QRect rect;
rect.setWidth(this->geometry().width());
rect.setHeight(this->geometry().height());
painter.drawPixmap(rect, m_pixmap);
//画一圈红色的外框
painter.setPen(QPen(QColor(255, 0, 0)));
painter.drawRect(m_selectRect);
painter.setBrush(QBrush(QColor(0, 0, 0, 150)));
painter.setPen(Qt::NoPen);
//鼠标绘制的矩形框外为半透明灰色,框内为白色
QPainterPath path;
path.setFillRule(Qt::OddEvenFill);
path.addRect(rect);
path.addRect(m_selectRect);
painter.drawPath(path);
//显示截取区域信息
{
if(PressFalg == 1)
{
//截取区域大小信息
QString pixmap_size = QString("%1").arg(Unsigned_selectRect.width()).replace("-","")
+ " X " + QString("%1").arg(Unsigned_selectRect.height()).replace("-","")
+ "(按ESC退出截图,按Enter保存截图,按Ctrl+X复制到剪切板)";
QColor information_color = QColor(0, 0, 0, 150);
painter.setPen(QColor(Qt::red));
QFont font("黑体", 14, QFont::Bold, true);
//使用字体
painter.setFont(font);
QFontMetrics fm(painter.font());
QRect fm_rect = fm.boundingRect(pixmap_size);
QRect pixmap_size_rect(Unsigned_selectRect.topLeft().x(), Unsigned_selectRect.topLeft().y() -25, fm_rect.width() + 10, 25);
painter.fillRect(pixmap_size_rect, information_color);
painter.drawText(pixmap_size_rect,Qt::AlignCenter, pixmap_size);
}
}
if(PressFalg == 1)
{
//中间画一个小矩形来模拟数据位置
painter.setPen(QPen(QColor(255, 0, 0)));
painter.drawRect(m_selectRect.x() + m_selectRect.width()/2,m_selectRect.y()-10,20,20);
painter.drawRect(m_selectRect.x()-10,m_selectRect.y() + m_selectRect.height()/2,20,20);
painter.drawRect(m_selectRect.x() + m_selectRect.width()/2,m_selectRect.y() + m_selectRect.height()-10,20,20);
painter.drawRect(m_selectRect.x() + m_selectRect.width()-10,m_selectRect.y() + m_selectRect.height()/2,20,20);
}
//绘制结束
painter.end();
}
void ScreenCaptureDlg::resizeEvent(QResizeEvent *event)
{
}
void ScreenCaptureDlg::mousePressEvent(QMouseEvent *event)
{
PressFalg = 1;
m_pressPos = event->pos();
if(DrawFalg == 0)
{
m_selectRect.setTopLeft(event->pos());
m_selectRect.setWidth(0);
m_selectRect.setHeight(0);
update();
}
if(DrawFalg == 1)
{
if((m_pressPos.x() > m_selectRect.x()+10 && m_pressPos.x()< m_selectRect.x() + m_selectRect.width()-10) &&
(m_pressPos.y() > m_selectRect.y()+10 && m_pressPos.y() < m_selectRect.y() + m_selectRect.height()-10))
{
setCursor(Qt::OpenHandCursor);
MoveFalg = 0;
selectRect_Point.setX(m_selectRect.x());
selectRect_Point.setY(m_selectRect.y());
movePoint = event->globalPos() - selectRect_Point;
}
//上
if(((m_pressPos.x() >= m_selectRect.x() + m_selectRect.width()/2 - 10
&& m_pressPos.x()<= m_selectRect.x() + m_selectRect.width()/2 + 10)
&&((m_pressPos.y() >= m_selectRect.y()-10 && m_pressPos.y()<= m_selectRect.y()+10))))
{
setCursor(Qt::SizeVerCursor);
UppeFalg = 1;
}
//左
if(((m_pressPos.x() >= m_selectRect.x()-10 && m_pressPos.x()<= m_selectRect.x()+10)
&&((m_pressPos.y() >= m_selectRect.y() + m_selectRect.height()/2-10
&& m_pressPos.y()<= m_selectRect.y() + m_selectRect.height()/2+10))))
{
setCursor(Qt::SizeHorCursor);
LeftFalg = 1;
}
//下
if(((m_pressPos.x() >= m_selectRect.x() + m_selectRect.width()/2 -10 && m_pressPos.x()<= m_selectRect.x() + m_selectRect.width()/2 +10)
&&((m_pressPos.y() >= m_selectRect.y() + m_selectRect.height()-10
&& m_pressPos.y()<= m_selectRect.y() + m_selectRect.height()+10))))
{
setCursor(Qt::SizeVerCursor);
lowerFalg = 1;
}
//右
if(((m_pressPos.x() >= m_selectRect.x() + m_selectRect.width()-10 && m_pressPos.x()<= m_selectRect.x() + m_selectRect.width()+10)
&&((m_pressPos.y() >= m_selectRect.y() + m_selectRect.height()/2-10
&& m_pressPos.y()<= m_selectRect.y() + m_selectRect.height()/2+10))))
{
setCursor(Qt::SizeHorCursor);
RightFalg = 1;
}
}
}
void ScreenCaptureDlg::mouseMoveEvent(QMouseEvent *event)
{
Unsigned_selectRect = m_selectRect;
if(m_width < 0)
{
QPoint mypoint = QPoint(m_selectRect.x() + m_selectRect.width(), m_selectRect.y());
Unsigned_selectRect.setTopLeft(mypoint);
Unsigned_selectRect.setWidth(QString("%1").arg(m_width).replace("-","").toInt());
update();
}
if(m_height < 0)
{
QPoint mypoint = QPoint(m_selectRect.x(), m_selectRect.y() + m_selectRect.height());
Unsigned_selectRect.setTopLeft(mypoint);
Unsigned_selectRect.setHeight(QString("%1").arg(m_height).replace("-","").toInt());
update();
}
if(DrawFalg == 0)
{
m_width = event->pos().x() - m_pressPos.x();
m_height = event->pos().y() - m_pressPos.y();
m_selectRect.setWidth(m_width);
m_selectRect.setHeight(m_height);
update();
}
if(DrawFalg == 1)
{
if(MoveFalg == 0)
{
QPoint movePos = event->globalPos();
QPoint m_Point = movePos-movePoint;
if((m_Point.x() >= 0 && m_Point.y() >= 0) && (m_Point.x() + m_selectRect.width() <= m_pixmap.width() && m_Point.y() + m_selectRect.height() <= m_pixmap.height()))
{
m_selectRect.setTopLeft(m_Point);
m_selectRect.setWidth(m_width);
m_selectRect.setHeight(m_height);
update();
}
}
if(UppeFalg == 1)
{
m_Point = QPoint(m_s
评论0