Qt implements the dynamic desktop background snowflake program, qt snowflake
I once received a gift, a snowflake program, and thought it was amazing. Through the previous articles, we have mastered some Paster skills, now you can implement it on your own (of course, you must first have the basic knowledge of qt signal and slot). Let's first look at the effect and then analyze how to implement it.
:
1: // set the program to no border
2: setWindowFlags( Qt::FramelessWindowHint );
3: // make the background transparent
4: setAttribute(Qt::WA_TranslucentBackground, true);
2. Establish a link between snowflake pool, falling, and recycling to form a circulation system
1: connect(&m_snowWait, SIGNAL(falling(SNOWLIST*)), &m_snowFalling, SLOT(AcceptSnow(SNOWLIST*)));
2: connect(&m_snowFalling, SIGNAL(snowLander(SNOWLIST*)), &m_snowLander, SLOT(LanderSnow(SNOWLIST*)));
3: connect(&m_snowLander, SIGNAL(snowRecycle(SNOWLIST*)), &m_snowWait, SLOT(SnowRecycle(SNOWLIST*)));
3. We can use a button to carry each snowflake and define the falling behavior for each snowflake.
Snowflake node header file:
1: #include <QToolButton>
2: #include <QPropertyAnimation>
3: class QPixmap;
4: class QPaintEvent;
5: class QPoint;
6: class QSize;
7:
8: class SnowNode : public QToolButton
9: {
10: public:
11: static const int MAXSWOW ;
12: SnowNode(QWidget *parent = 0);
13: QSize GetSnowSize();
14: bool IsLander();
15: void InitSnow();
16: void FallingAnimation();
17:
18: protected:
19: void paintEvent(QPaintEvent *event);
20: private:
21: QString GetImgFileName();
22: private:
23: QPixmap m_pixmap;
24: QPropertyAnimation *m_animation;
25: QSize m_areaSize;
26: };
Snowflake node implementation file:
1: #include "pubbase.h"
2: #include "snownode.h"
3: #include <QSize>
4: #include <QPoint>
5: #include <QBitmap>
6: #include <QPixmap>
7: #include <QToolButton>
8: #include <QApplication>
9: #include <QDesktopWidget>
10: #include <QGraphicsScene>
11: #include <QGraphicsView>
12: const int SnowNode::MAXSWOW = 19;
13:
14: SnowNode::SnowNode(QWidget *parent):
15: QToolButton(parent),m_animation(new QPropertyAnimation(this, "geometry"))
16: {
17: // you must set it to borderless. Otherwise, the visible area and the image area do not overlap.
18: setWindowFlags( Qt::FramelessWindowHint );
19: resize(GetSnowSize());
20: // scale the image
21: m_pixmap.load(GetImgFileName());
22: m_pixmap = m_pixmap.scaled(this->size(),Qt::IgnoreAspectRatio);
23: setHidden(true);
24: m_areaSize.setWidth(QApplication::desktop()->width());
25: m_areaSize.setHeight(QApplication::desktop()->height());
26: }
27:
28: // initialize snowflake
29: void SnowNode::InitSnow()
30: {
31: this->move(qrand() % m_areaSize.width(), -32);
32: }
33:
34: // set snowflake Animation
35: void SnowNode::FallingAnimation()
36: {
37: int x = qrand()% m_areaSize.width();
38: // full snow falling time
39: m_animation->setDuration(8000);
40: m_animation->setStartValue(QRect( pos(), size()));
41: m_animation->setEndValue(QRect( QPoint(x,m_areaSize.height()), size()));
42: m_animation->start();
43: }
44:
45: // return whether the snowflake has landed
46: bool SnowNode::IsLander()
47: {
48: if(this->pos().y() >= m_areaSize.height()
49: && m_animation->state() == QAbstractAnimation::Stopped
50: )
51: {
52: return true;
53: }
54: return false;
55: }
56:
57: void SnowNode::paintEvent(QPaintEvent *event)
58: {
59: // draw a background image
60: this->setIcon(QIcon(m_pixmap));
61: this->setIconSize(size());
62: // set the transparent part of the png Image to penetrate
63: this->setMask(m_pixmap.mask());
64: // draw
65: QToolButton::paintEvent(event);
66:
67: }
68:
69: // the size of each snowflake. It is randomly generated.
70: QSize SnowNode::GetSnowSize()
71: {
72: int x = qrand() % 10;
73: return x >= 6 ? QSize(32,32) : x >= 3 ? QSize(24,24) : QSize(16,16);
74: }
75:
76: // get the snowflake file name
77: QString SnowNode::GetImgFileName()
78: {
79: return QString().sprintf(":/image/_%d.png", qrand()% MAXSWOW);
80: }
81:
4. Replace a desktop background
1. Call the windows API function to set the desktop background.
2: ::SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, (void*)imgFileName, SPIF_UPDATEINIFILE)
5. Define a timer and place the Snowflake on a regular basis.
If you want to learn more, you can leave your email address. I will send you the complete source code.