如果没有记错,应该学自刘大神的案例
国内站点:[飞扬青云 (feiyangqingyun) - Gitee.com](飞扬青云 (feiyangqingyun) - Gitee.com)
主要思路
1、主窗口 无背景显示,并全屏显示
2、使用QLabel 显示图片
3、主窗口控制 QLabel 移动
4、不在任务栏显示窗口任务,在进程中 也无程序名称,GPU 任务会在桌面窗口管理器中
5、右键 雪花 可退出当前程序
6、多个雪花程序 可以累加
代码
#include "widget.h"
#include "ui_widget.h"
#include <QDesktopWidget>
#include <QPalette>
#include <QBrush>
#include <QtMath>
#include <time.h>
#include <QKeyEvent>
#include <QMouseEvent>
#ifdef Q_OS_LINUX
#include <X11/extensions/shape.h>
#endif
#if 0
//#ifdef Q_OS_WIN
#include <windows.h>
#endif
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
srand(::time(NULL));
setGeometry(0, 0, qApp->desktop()->width(), qApp->desktop()->height());
setWindowFlags(windowFlags()
|Qt::FramelessWindowHint //去边框
|Qt::X11BypassWindowManagerHint //linux下脱离任务管理器
|Qt::WindowStaysOnBottomHint //最低层显示
|Qt::Tool //不在任务栏显示
);
setAttribute(Qt::WA_TranslucentBackground);
setWindowState( Qt::WindowNoState //不激活
|Qt::WindowFullScreen //全屏
);
setFocusPolicy(Qt::NoFocus);
setWindowOpacity(WINDOW_OPACITY);
#ifdef Q_OS_LINUX
XShapeCombineRectangles(QX11Info::display(), winId(), ShapeInput, 0,
0, NULL, 0, ShapeSet, YXBanded);
#endif
#if 0
//#ifdef Q_OS_WIN
SetWindowLong(winId(), GWL_EXSTYLE, GetWindowLong(winId(), GWL_EXSTYLE) |
WS_EX_TRANSPARENT | WS_EX_LAYERED);
#endif
for (int i = 0; i < 12; ++i) {
pixmapList[i].load(QString(":/%1.png").arg (i));
}
for(int i = 0; i < MAX_PICS; i++)
{
picLabel[i] = new QLabel(this);
picLabel[i]->resize(64, 64);
}
startTimer(1000/20);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent */*e*/)
{
const int timeinit = 10;
static int timeCount = timeinit;
static int initLabel = MAX_PICS;
if(--timeCount <= 0)
{
timeCount = timeinit;
if(initLabel > 0)
{
--initLabel;
picLabel[initLabel]->move(0, -picLabel[initLabel]->height());
}
}
FlashSnow();
}
void Widget::SetLabelBG(const QPixmap &pixmap, QLabel *label)
{
if(!label || pixmap.isNull()) return;
QPixmap map = pixmap.scaled(label->size());
if(map.isNull()) return;
label->setPixmap(map);
}
void Widget::FlashSnow()
{
int i;
for(i = 0; i < MAX_PICS; i++)
{
if(picLabel[i] == NULL) continue;
if(picLabel[i]->y() == -picLabel[i]->height())
{
//resize label
int size = (rand()%64)+16;
picLabel[i]->resize(size, size);
//init place
int x = (rand()%this->width());
picLabel[i]->move(x, 10-picLabel[i]->height());
//repaint label's backgroud
int imgId = (rand()%MAX_PIXMAP);
SetLabelBG(pixmapList[imgId], picLabel[i]);
}
else
{
//snow flow down
WidgetFlowDown(picLabel[i],true);
}
}
}
void Widget::WidgetFlowDown(QWidget *widget, bool bRandom)
{
if(!widget) return;
int downY = widget->y () + 5;
int downX = widget->x ();
if(bRandom)
{
downY = widget->y () + 5;
}
if(downY > (this->height()))
downY = -widget->height();
widget->move(downX, downY);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button () == Qt::RightButton)
{
QMetaObject::invokeMethod (qApp,"quit");
}
}