Qt 實(shí)現(xiàn)桌面雪花飄落代碼
代碼很簡(jiǎn)單, 貼個(gè)主要的實(shí)現(xiàn)過(guò)程吧. 理應(yīng)支持windows和linux桌面版的, 但是linux下就暫時(shí)不測(cè)試了. 懶得重啟. 有空測(cè)試一下.
系統(tǒng)資源消耗: 我在1.65GHz 雙核CPU, 4G RAM, 32bit Win7 下, 19M左右的內(nèi)存消耗, 6%-7%左右的CPU消耗.
全部源碼在后面的鏈接.
#include "widget.h"
#include "ui_widget.h"
#include <QDesktopWidget>
#include <QPalette>
#include <QBrush>
#include <time.h>
#ifdef Q_OS_LINUX
#include <X11/extensions/shape.h>
#endif
#ifdef Q_OS_WIN
#include <windows.h>
#endif
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setGeometry(0, 0, qApp->desktop()->width(), qApp->desktop()->height());
setWindowFlags(windowFlags()
|Qt::FramelessWindowHint //去邊框
|Qt::X11BypassWindowManagerHint //linux下脫離任務(wù)管理器
|Qt::WindowStaysOnBottomHint //最低層顯示
|Qt::Tool //不在任務(wù)欄顯示
);
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
#ifdef Q_OS_WIN
SetWindowLong(winId(), GWL_EXSTYLE, GetWindowLong(winId(), GWL_EXSTYLE) |
WS_EX_TRANSPARENT | WS_EX_LAYERED);
#endif
int i=0;
pixmapList[i++].load(":/snowIcons/11.png");
pixmapList[i++].load(":/snowIcons/03.png");
pixmapList[i++].load(":/snowIcons/06.png");
pixmapList[i++].load(":/snowIcons/08.png");
pixmapList[i++].load(":/snowIcons/10.png");
pixmapList[i++].load(":/snowIcons/12.png");
pixmapList[i++].load(":/snowIcons/13.png");
pixmapList[i++].load(":/snowIcons/16.png");
pixmapList[i++].load(":/snowIcons/17.png");
pixmapList[i++].load(":/snowIcons/18.png");
pixmapList[i++].load(":/snowIcons/19.png");
for(i = 0; i < MAX_PICS; i++)
{
picLabel[i] = new QLabel(this);
picLabel[i]->setGeometry(-128, -128, 64, 64);
}
startTimer(150);
}
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)
{
qsrand(::time(NULL));
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 = (qrand()%64)+16;
picLabel[i]->resize(size, size);
//init place
int x = (qrand()%this->width());
picLabel[i]->move(x, 10-picLabel[i]->height());
//repaint label's backgroud
int imgId = (qrand()%MAX_PIXMAP);
SetLabelBG(pixmapList[imgId], picLabel[i]);
}
else
{
//snow flow down
WidgetFlowDown(picLabel[i]);
}
}
}
void Widget::WidgetFlowDown(QWidget *widget, bool bRandom)
{
if(!widget) return;
int downY = widget->y()+5;
if(bRandom)
{
downY = widget->y()+qrand()%(this->height() - widget->y());
}
if(downY > (this->height())) downY = -widget->height();
widget->move(widget->x(), downY);
}
接下來(lái)上個(gè)截屏吧:
最后是全部源碼了, 是個(gè)Qt creator 工程:
工程壓縮包下載: http://xiazai.jb51.net/201312/yuanma/DesktopSnow(jb51.net).zip
相關(guān)文章
Eclipse對(duì)printf()不能輸出到控制臺(tái)的快速解決方法
Eclipse對(duì)printf()不能輸出到控制臺(tái)的快速解決方法。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10C語(yǔ)言超市管理系統(tǒng)設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言超市管理系統(tǒng)設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02C語(yǔ)言實(shí)現(xiàn)3*3數(shù)組對(duì)角線之和示例
今天小編就為大家分享一篇C語(yǔ)言實(shí)現(xiàn)3*3數(shù)組對(duì)角線之和示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12C++的STL中accumulate函數(shù)的使用方法
這篇文章主要介紹了C++的STL中accumulate的使用方法,accumulate作用是累加求和即自定義類(lèi)型數(shù)據(jù)處理,下文具體的操作方法需要的小伙伴可以參考一下2022-03-03c語(yǔ)言獲取用戶輸入字符串是scanf和gets的區(qū)別詳解
今天小編就為大家分享一篇c語(yǔ)言獲取用戶輸入字符串是scanf和gets的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07