Qt 實現(xiàn)桌面雪花飄落代碼
代碼很簡單, 貼個主要的實現(xiàn)過程吧. 理應(yīng)支持windows和linux桌面版的, 但是linux下就暫時不測試了. 懶得重啟. 有空測試一下.
系統(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);
}
接下來上個截屏吧:
最后是全部源碼了, 是個Qt creator 工程:
工程壓縮包下載: http://xiazai.jb51.net/201312/yuanma/DesktopSnow(jb51.net).zip
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(一)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(二)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改