#include "mainwindow.h"
#include <qpainter.h>
#include <QDebug>
#include <QPainterPath>
#include <QtWinExtras>
#include <QRectF>
#include <QDesktopWidget>
#include <QApplication>
#include <QStyle>
MainWindow::MainWindow(QWidget *parent)
: QPushButton(parent)
{
//实体列表 窗体背景透明
this->setWindowFlags(windowFlags() | Qt::FramelessWindowHint);//无边框
this->setAttribute(Qt::WA_TranslucentBackground, true);//窗体背景全透明
QColor color(255, 0, 255,125);
QPalette pal(palette());
pal.setColor(QPalette::Window, color);
setAutoFillBackground(true);
setPalette(pal);
this->resize(200,300);
connect(this,&QPushButton::clicked,this,[&](){
c+=2;
qDebug()<<"23你好按钮"<<c;
});
}
MainWindow::~MainWindow() {}
void MainWindow::paintEvent(QPaintEvent *event)
{
// 0:绘制矩形和文字
// 1:绘制椭圆
// 2:画线
// 3:画面(有圆角)
// 4:画图片
// 5:毛玻璃
switch(5){
case 0:
{
QPainter painter(this);
painter.setPen(QPen(Qt::darkYellow,2)); // 设置当前画笔的颜色,和画笔大小,单位像素
QRect rect = QRect(20,20,100,50); // 设置要绘制的矩形起始点位置,以及宽度和高度
painter.drawRect(rect); // 使用当前设置的画笔设置矩形
QFont font = this->font(); // 创建字体
font.setPointSize(30); // 设置字体大小,pix
painter.setFont(font); // 设置当前画家的字体
painter.drawText(rect,Qt::AlignHCenter|Qt::TextWordWrap,"你好"); // Qt::AlignHCenter:居中对齐 ,Qt::TextWordWrap:文字换行
painter.setPen(QPen(Qt::green,2)); // 重新设置画笔
auto bRect = painter.boundingRect(rect,Qt::AlignHCenter|Qt::TextWordWrap,"世界"); // 返回绘制字体所需要的矩形(矩形信息)
painter.drawRect(bRect); // 绘制返回的矩形
}
break;
case 1:
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true); // 如果启用为true,则在绘制器上设置给定的渲染提示;否则将清除渲染提示。(反锯齿,生效)
painter.setPen(QPen(Qt::red,5)); // 设置画笔
painter.drawEllipse(painter.brushOrigin(),5,5); // 绘制椭圆 ,painter.brushOrigin():返回当前设置的笔刷原点。绘制位于半径为rx(5)和ry(5)的中心的椭圆
painter.translate(50,50); // 将坐标系平移给定的偏移量
painter.drawEllipse(painter.brushOrigin(),5,5);
painter.setBrushOrigin(100,100); // 将笔刷的原点设置为点(x,y)
painter.drawEllipse(painter.brushOrigin(),5,5);
}
break;
case 2:
{
auto rect = event->rect(); // 获取到当前控件的矩形信息
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true); // 开启抗锯齿
painter.setPen(QPen(Qt::red,5)); // 设置画笔和画笔大小
painter.setClipRect(rect.adjusted(20,20,-20,-20)); // 设置要裁剪的矩形区域
painter.drawLine(rect.topLeft(),rect.bottomRight()); // 获取矩形的左上点坐标和右下点坐标,将其设置为要画的线的起始坐标。
}
break;
case 3:
{
auto rect = event->rect();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true); // 设置抗锯齿
painter.setBrush(QColor(32,78,100,60));// 设置笔刷颜色
QPainterPath path;
path.addRoundedRect(rect.adjusted(20,20,-20,-20),60,60); // 添加圆角矩形
painter.setClipPath(path);
painter.drawRect(rect);
}
break;
case 4:
{
QImage origin("E:/Project/Qtstudy/00_painter/studyPainter/resource/arrow_pressed_top.png");
QRect rect{0,0,this->width(),this->height()};
QPainter painter(this);
painter.drawImage(rect,origin);
}
case 5:
{
QPainter painter(this);
QRect rect{0,0,this->width(),this->height()};
if(dim ==false)
{
QScreen *screen = QGuiApplication::primaryScreen();
QImage image = screen->grabWindow(0,this->pos().x(),this->pos().y(),this->width(),this->height()).toImage();
for(int i=0;i<30;i++)
{
for(int y=0;y<image.height();y++)
{
for(int x=0;x<image.width();x++)
{
if(y<image.height()-2&&x<(image.width()-2))
{
// 修复左侧
QColor cor1 = image.pixel(1,y);
QColor cor2 = image.pixel(1,y+1);
QColor coor1((cor1.red()+cor2.red())/2,
(cor1.green()+cor2.green())/2,
(cor1.blue()+cor2.blue())/2);
//qDebug()<<"获取到的颜色 = "<<color.red()<<","<<color.red()<<","<<color.red()<<","<<color.alpha();
image.setPixelColor(0,y,coor1);
// 修复底部
QColor cor3 = image.pixel(x,image.height()-1);
QColor cor4 = image.pixel(x+1,image.height()-1);
QColor coor2((cor3.red()+cor4.red())/2,
(cor3.green()+cor4.green())/2,
(cor3.blue()+cor4.blue())/2);
image.setPixelColor(x,image.height()-1,coor2);
// 修复顶端
QColor cor5 = image.pixel(x,0);
QColor cor6 = image.pixel(x+1,0);
QColor coor3((cor5.red()+cor6.red())/2,
(cor5.green()+cor6.green())/2,
(cor5.blue()+cor6.blue())/2);
image.setPixelColor(x,0,coor3);
// 修复右侧
QColor cor7 = image.pixel(image.width()-2,y);
QColor cor8 = image.pixel(image.width()-2,y+1);
QColor coor4((cor7.red()+cor8.red())/2,
(cor7.green()+cor8.green())/2,
(cor7.blue()+cor8.blue())/2);
image.setPixelColor(image.width()-1,y,coor4);
QColor color1 = image.pixel(x,y);
QColor color2 = image.pixel(x+1,y);
QColor color3 = image.pixel(x+2,y);
QColor color4 = image.pixel(x,y+1);
QColor color5 = image.pixel(x+2,y+1);
QColor color6 = image.pixel(x,y+2);
QColor color7 = image.pixel(x+1,y+2);
QColor color8 = image.pixel(x+2,y+2);
QColor color((color1.red()+color2.red()+color3.red()+color4.red()+color5.red()+color6.red()+color7.red()+color8.red())/8,
(color1.green()+color2.green()+color3.green()+color4.green()+color5.green()+color6.green()+color7.green()+color8.green())/8,
(color1.blue()+color2.blue()+color3.blue()+color4.blue()+color5.blue()+color6.blue()+color7.blue()+color8.blue())/8,25);
//qDebug()<<"获取到的颜色 = "<<color.red()<<","<<color.red()<<","<<color.red()<<","<<color.alpha();
image.setPixelColor(x+1,y+1,color);
}
}
}
}
painter.drawImage(rect,image);
m_image = image;
//qDebug()<<"窗口转屏幕坐标="<<this->pos().x()<<","<<this->pos().y();
dim = true;
}
else
{
painter.drawImage(rect,m_image);
}
painter.setRenderHint(QPainter::Antialiasing,true); // 设置抗锯齿
painter.setBrush(QColor(37,40,40,100));// 设置笔刷颜色
QPainterPath path;
path.addRoundedRect(rect.adjusted(20,20,-20,-20),60,60); // 添加圆角矩形
painter.setClipPath(path);
painter.drawRect(rect);
painter.setBrush(QColor(255,255,255,80));// 设置笔刷颜色
painter.drawRect(rect);
}
break;
default:
break;
}
}
void MainWindow::moveEvent(QMoveEvent *event)
{
//dim = false;
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
//只允许左键拖动 持续的动作
if(event->buttons()&Qt::LeftButton) //buttons处理的是长事件,button处理的是短暂的事件
{
//窗口跟随鼠标移动
//窗口的新位置=鼠标当前位置-差值
move(event->globalPos()-m_pt);
}
}
void MainWindow::mousePressEvent(QMouseEvent *ev)
{
// 如果鼠标左键按下 单击
if(ev->button()&Qt::LeftButton)
{
//求差值=鼠标当前位置-窗口左上角点
m_pt=ev->globalPos()-this->geometry().topLeft(); //geometry()是矩形窗口,topLeft()是左上角的信息。
}
}