#include "imagewidget.h"
#include <QShortcut>
#include <Windows.h>
#include <vector>
std::vector<QRect> allWindowRect; //用于存储所有的窗口
std::vector<HWND> allWindowHwnd; //用于存储所有的窗口句柄
std::vector<MyRect> myRectRestlt; // 找到所有包含 鼠标当前移动点的矩形,并保存其到各边的距离之和。
//声明回调函数
bool CALLBACK MyEnumWindowsProc(HWND hwnd,LPARAM lParam);
QString str;
ImageWidget::ImageWidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
//用于获取窗口大小
QDesktopWidget *dtw = QApplication::desktop();
isPainting = false;
isPressed = false;
isDragging = false;
isDrawText = false;
isSelectRect = true;
isResizeRect = true;
isFristMove = true;
canMove = true;
dx = 0;
dy = 0;
//打开鼠标 跟踪
setMouseTracking(true);
//遍历窗口 获得各个窗口的大小
::EnumWindows((WNDENUMPROC)MyEnumWindowsProc,0);
//获得 整个屏幕
pixmapTemp = pixmapTemp.grabWindow(QApplication::desktop()->winId(),0,0,dtw->width(),dtw->height());
pixmapOrg = pixmapTemp;
textContent = new QLineEdit(this);
textContent->hide();
connect(captureMenu_.ui.cancelPushButton,SIGNAL(clicked()),this,SLOT(slotCloseMyself()));
connect(captureMenu_.ui.savePushButton,SIGNAL(clicked()),this,SLOT(slotSaveToFile()));
connect(captureMenu_.ui.addLinePushButton,SIGNAL(clicked()),this,SLOT(slotPaintLine()));
connect(captureMenu_.ui.addRectPushButton,SIGNAL(clicked()),this,SLOT(slotPaintRect()));
connect(captureMenu_.ui.addCiclePushButton,SIGNAL(clicked()),this,SLOT(slotPaintCicle()));
connect(captureMenu_.ui.penPushButton,SIGNAL(clicked()),this,SLOT(slotPaintFree()));
connect(captureMenu_.ui.redPushButton,SIGNAL(clicked()),this,SLOT(slotRedPen()));
connect(captureMenu_.ui.whitePushButton,SIGNAL(clicked()),this,SLOT(slotWhitePen()));
connect(captureMenu_.ui.blackPushButton,SIGNAL(clicked()),this,SLOT(slotBlackPen()));
connect(captureMenu_.ui.bluePushButton,SIGNAL(clicked()),this,SLOT(slotBluePen()));
connect(captureMenu_.ui.yellowPushButton,SIGNAL(clicked()),this,SLOT(slotYellowPen()));
connect(captureMenu_.ui.purposePushButton,SIGNAL(clicked()),this,SLOT(slotPurplePen()));
connect(captureMenu_.ui.noPushButton,SIGNAL(clicked()),this,SLOT(slotGrayPen()));
connect(captureMenu_.ui.greenPushButton,SIGNAL(clicked()),this,SLOT(slotGreenPen()));
connect(captureMenu_.ui.fontAPushButton,SIGNAL(clicked()),this,SLOT(slotPenA()));
connect(captureMenu_.ui.fontBPushButton,SIGNAL(clicked()),this,SLOT(slotPenB()));
connect(captureMenu_.ui.fontCPushButton,SIGNAL(clicked()),this,SLOT(slotPenC()));
connect(captureMenu_.ui.addTextPushButton,SIGNAL(clicked()),this,SLOT(slotPaintText()));
connect(textContent,SIGNAL(returnPressed()),this,SLOT(slotSetText()));
captureMenu_.ui.currentColorLabel->setStyleSheet("background-color:black");
QShortcut *send_access= new QShortcut(QKeySequence(tr("esc")), this);
connect(send_access, SIGNAL(activated()), this,SLOT(slotCloseMyself()));
paintType = MOVE;
}
ImageWidget::~ImageWidget(){}
void ImageWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen;
pen.setWidth(4);
pen.setColor(QColor(0,255,255,127));
painter.setPen(pen);
QPixmap tempNew(pixmapOrg.size());
QPainter p(&tempNew);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.drawPixmap(0, 0, pixmapOrg);
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.fillRect(tempNew.rect(), QColor(50, 50, 50, 120));
painter.drawPixmap(0,0,tempNew);//把图片调 暗 以显示截图全屏
if (isSelectRect)//如果是选定区域
{
if (isDragging)//手选区域大小
{
dragPaint(&painter);
}
else //自动选定区域大小
{
autoPaint(&painter);
}
}
else//如果不是选定区域 也就是 画画、移动区域 或者是 改变区域大小
{
if (isPainting)//画画
{
paintSomeThing(&painter);
}
else if (isResizeRect)//改变区域大小
{
resizeSelectedRect(&painter);
}
else if(isFristMove)//移动区域
{
moveTheRect(&painter);
}
}
QPixmap pix(92,36);
pix.fill(QColor(0,0,0,120));
QPainter px(&pix);
QPen penRect(QColor(255,255,255,255));
px.setPen(penRect);
QString temp;
temp = tr("大小:%1×%2").arg(selectedRect.width).arg(selectedRect.height);
px.drawText(0,15,temp);
temp = tr("位置:%1,%2").arg(selectedRect.ptStart_.x()+dx).arg(selectedRect.ptStart_.y()+dy);
px.drawText(0,30,temp);
QPainter ptemp(this);
ptemp.drawPixmap(selectedRect.ptStart_.x()+selectedRect.width-95 +dx,selectedRect.ptStart_.y()-40+dy,pix);
QPen penPoint;
penPoint.setColor(QColor(0,0,255,255));
penPoint.setWidth(8);
QPainter painterPoint(this);
painterPoint.setPen(penPoint);
if (isResizeRect)
{
painterPoint.drawPoint(selectedRect.ptStart_);
painterPoint.drawPoint(selectedRect.ptStart_.x()+selectedRect.width/2,selectedRect.ptStart_.y());
painterPoint.drawPoint(selectedRect.ptStart_.x()+selectedRect.width,selectedRect.ptStart_.y());
painterPoint.drawPoint(selectedRect.ptStart_.x(),selectedRect.ptStart_.y()+selectedRect.height/2);
painterPoint.drawPoint(selectedRect.ptEnd_.x(),selectedRect.ptStart_.y()+selectedRect.height/2);
painterPoint.drawPoint(selectedRect.ptStart_.x(),selectedRect.ptStart_.y()+selectedRect.height);
painterPoint.drawPoint(selectedRect.ptStart_.x()+selectedRect.width/2,selectedRect.ptStart_.y()+selectedRect.height);
painterPoint.drawPoint(selectedRect.ptStart_.x()+selectedRect.width,selectedRect.ptStart_.y()+selectedRect.height);
}
else if(isFristMove && canMove)
{
painterPoint.drawPoint(selectedRect.ptStart_.x() + dx ,selectedRect.ptStart_.y() + dy);
painterPoint.drawPoint(selectedRect.ptStart_.x() + dx +selectedRect.width/2,selectedRect.ptStart_.y() + dy);
painterPoint.drawPoint(selectedRect.ptStart_.x() + dx +selectedRect.width,selectedRect.ptStart_.y() + dy);
painterPoint.drawPoint(selectedRect.ptStart_.x() + dx ,selectedRect.ptStart_.y() + dy +selectedRect.height/2);
painterPoint.drawPoint(selectedRect.ptEnd_.x() + dx ,selectedRect.ptStart_.y() + dy +selectedRect.height/2);
painterPoint.drawPoint(selectedRect.ptStart_.x() + dx ,selectedRect.ptStart_.y() + dy +selectedRect.height);
painterPoint.drawPoint(selectedRect.ptStart_.x() + dx +selectedRect.width/2,selectedRect.ptStart_.y() + dy +selectedRect.height);
painterPoint.drawPoint(selectedRect.ptStart_.x() + dx +selectedRect.width,selectedRect.ptStart_.y() + dy +selectedRect.height);
}
}
void ImageWidget::mousePressEvent(QMouseEvent *event)
{
if (isSelectRect)
{
pStart_.setX(event->x());
pStart_.setY(event->y());
}
else
{
if(isPainting)
{
paintStart.setX(event->x());
paintStart.setY(event->y());
paintMove.setX(event->x()); //保证 乱画时不会连接 其他地方isPressed
paintMove.setY(event->y());
selectedRect.selectedPixmap_ = pixmapTemp;
if (isDrawText)
{
textContent->clear();
textContent->move(event->x(),event->y());
textContent->show();
isDrawText = false;
paintType = TEXT;
}
}
else if (isFristMove) //如果在 矩形框内按下鼠标 表示想要移动矩形
{
MoveStart.setX(event->x());//保存按下的第一个鼠标点,用于计算dx,dy
MoveStart.setY(event->y());
if(event->y() < selectedRect.ptEnd_.y() - 5 && event->y() > selectedRect.ptStart_.y()+5 && event->x() > selectedRect.ptStart_.x()+5 && event->x() < selectedRect.ptEnd_.x() - 5)
{
isResizeRect = false;
setCursor(Qt::SizeAllCursor);
}
}
else if(isResizeRect){}
}
isPressed = true;
}
void ImageWidget::mouseMoveEvent(QMouseEvent *event)
{
QFile file("log.txt");
// if(!file.open(QIODevice::WriteOnly |QIODevice::Text))
// QMessageBox::information(this,tr("aa"),tr("bb"));
int i = 0;
if (isSelectRect)
{
if (isPressed) //如果按下 鼠标 开始 区域截图
{
isDragging = true;
pMove_.setX(event->x());
pMove_.setY(event->y());
}
else //如果没有按下鼠标 开始自动寻找合适窗口 //、应该改为 找到距离最近的 矩形块 。。。!!!
评论26
最新资源